Upcoming SlideShare
×

# Labsheet 5

398
-1

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
398
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
18
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Labsheet 5

1. 1. FP 201 – PROGRAMMING FUNDAMENTALSLAB 5 : PROGRAM CONTROLLearning OutcomeBy 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 statementsTheory/ Topics• Relational Operator (<, <=, >, >=)• Logical Operator (&&, ||)• Operator Precedence• Expression• Looping statements – for, while, do..while statementsProcedure :Step 1: Type the programs given belowStep 2: Save the program as _________________Step 3: Compile and run the program. Write the outputActivity 5A#include<iostream>using namespace std;void main(){ int x; for(x=1; x<=5; x++) { if (x==3) continue; cout<<x; } return 0;} 1
2. 2. FP 201 – PROGRAMMING FUNDAMENTALSActivity 5B#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; } 2
3. 3. FP 201 – PROGRAMMING FUNDAMENTALSActivity 5DThe following program illustrate the syntax of do..whilestatements.#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 : “; 3
4. 4. FP 201 – PROGRAMMING FUNDAMENTALS cout << “number 1 divide by number 2 : “ << n1/n2 << “, remainder is : “ << n1%n2 << endl; cout << “nDo another ? (y/n) => “; cin >> ch; }}Exercise 5.1Write programs to print the following patterns: 1 12 123 1234Exercise 5.2Write a program by using while loops to calculate the parking fee basedon parking rate RM2.00 per hour for every customer. When there areno more customers, your program should be able to sum the totalparking collection. Example of output: Enter parking hours: 2 Parking fee: RM 4 Continue? (y/n) => y Enter parking hours: 5 Parking fee: RM 10 Continue? (y/n) => n Total collection: 14 4