labsheet 5

5
FP 201 – PROGRAMMING FUNDAMENTALS LAB 5 : PROGRAM CONTROL Learning Outcome By the end of this lab, students should be able to : Describe the structure of for, while and do..while statements in C++ program Write program using looping statements Theory/ Topics Relational Operator (<, <=, >, >=) Logical Operator (&&, ||) Operator Precedence Expression Looping statements – for, while, do..while statements Procedure : Step 1: Type the programs given below Step 2: Save the program as _________________ Step 3: Compile and run the program. Write the output Activity 5A #include<iostream> using namespace std; void main() { int x; for(x=1; x<=5; x++) 1

Upload: rohassanie

Post on 22-May-2015

359 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Labsheet 5

FP 201 – PROGRAMMING FUNDAMENTALS

LAB 5 : PROGRAM CONTROL

Learning Outcome

By the end of this lab, students should be able to : Describe the structure of for, while and do..while statements in C+

+ program Write program using looping statements

Theory/ Topics

Relational Operator (<, <=, >, >=) Logical Operator (&&, ||) Operator Precedence Expression Looping statements – for, while, do..while statements

Procedure :Step 1: Type the programs given belowStep 2: Save the program as _________________Step 3: Compile and run the program. Write the output

Activity 5A #include<iostream>using namespace std;void main(){

int x;for(x=1; x<=5; x++){ if (x==3)

continue;cout<<x;

}return 0;

}

Activity 5B

1

Page 2: Labsheet 5

FP 201 – PROGRAMMING FUNDAMENTALS

#include<iostream>using namespace std;void main(){

int x;for(x=1; x<=5; x++){ if (x==3)

break;cout<<x;

}return 0;

}

Activity 5C

//Program that add an integer to itself for n of times.#include <iostream>using namespace std;void main() { int number1, number2; int total = 0; cout << "Please enter 2 numbers: "; cin >> number1 >> number2;

for(int i = 0; i < number2; ++i) { total += number1; } cout << "Total addition: " << total; }

Activity 5D

2

Page 3: Labsheet 5

FP 201 – PROGRAMMING FUNDAMENTALS

The following program illustrate the syntax of do..while statements.

#include <iostream>using namespace std;void main() { char ch; int n1, n2; do { cout << “Enter number 1 : “; cin >> n1; cout << “Enter number 2 : “; cout << “number 1 divide by number 2 : “ << n1/n2 << “, remainder is : “ << n1%n2 << endl; cout << “\nDo another ? (y/n) => “; cin >> ch;

} while (ch != ‘n’); }

Activity 5EThe following program illustrate the syntax of while statements.

#include <iostream>using namespace std;void main(){

char ch = ‘y’;int n1, n2;

while (ch != ‘n’){

cout << “Enter number 1 : “;cin >> n1;cout << “Enter number 2 : “;cout << “number 1 divide by number 2 : “ << n1/n2 << “, remainder is : “ << n1%n2 << endl;cout << “\nDo another ? (y/n) => “;cin >> ch;

3

Page 4: Labsheet 5

FP 201 – PROGRAMMING FUNDAMENTALS

} }

Exercise 5.1

Write programs to print the following patterns:

11 21 2 31 2 3 4

Exercise 5.2

Write a program by using while loops to calculate the parking fee based on parking rate RM2.00 per hour for every customer. When there are no more customers, your program should be able to sum the total parking collection.

Example of output:Enter parking hours: 2Parking fee: RM 4Continue? (y/n) => yEnter parking hours: 5Parking fee: RM 10

Continue? (y/n) => nTotal collection: 14

4