Beruflich Dokumente
Kultur Dokumente
0 PROGRAMMING
8.5 Use of Control Structure
Learning Outcome
At the end of this topic, students should be able to:
8.5 Use of Control Structure
8.5.1 Sequence
8.5.2 Selection
8.5.3 Looping
Control Structure
Repetition
Repetition structure:
also called looping control structure
Repetition
int i =1;
while( i <=3)
{
/*Create the braces because statements
block are more
than one, otherwise it will create an
infinity result output*/
i=i+1;
Repetition
while construct
Problem statement
Create a program that will print Hello four times.
Pseudocode:
Start
1
2
End
set count = 1
while count <=4
Print Hello
3 count = count + 1
endwhile
Soucecode:
#include <iostream>
using namespace std;
int main()
{
int count =1; 1
while (count<=4) 2
{
cout<<Hello;
count = count + 1;
}
return 0;
}
6
for construct
Problem statement
Create a program that will print Hello four times.
Pseudocode:
Start
1
2
set count = 1
while count <=4
Print Hello
3 count = count + 1
endwhile
Soucecode:
#include <iostream>
using namespace std;
int main()
{
int count;
1
for(count=1;count<=4;count=count+1)
cout<<Hello;
End
return 0;
}
while Construct
Problem statement
Create a program that calculates and displays the pay of 5 employees given
hours worked and hourly pay rate.
Soucecode:
#include <iostream>
using namespace std;
int main()
{
int count =1, hours;
float rate, pay;
while (count<=5)
{
cout << Enter your hours worked;
cin>>hours;
cout << Enter your pay rate;
cin>>rate;
pay = hours*rate;
cout<<Your pay for this month is RM <<pay;
count = count + 1;
}
return 0;
}
for Construct
Problem statement
Create a program that calculates and displays the pay of 5 employees given
hours worked and hourly pay rate.
Soucecode:
#include <iostream>
using namespace std;
int main()
{
int count, hours;
float rate, pay;
for (count=1;count<=5; count = count + 1)
{
cout << Enter your hours worked;
cin>>hours;
cout << Enter your pay rate;
cin>>rate;
pay = hours*rate;
cout<<Your pay for this month is RM <<pay;
}
return 0;
}
while Construct
Problem statement
Create a program that calculates and displays the pay for a number of
employees given hours worked and hourly pay rate.
Soucecode:
#include <iostream>
using namespace std;
int main()
{
int count =1, hours, employees;
float rate, pay;
cout << Enter number of employees;
cin>>employees;
while (count<=employees)
{
cout << Enter your hours worked;
cin>>hours;
cout << Enter your pay rate;
cin>>rate;
pay = hours*rate;
cout<<Your pay for this month is RM <<pay;
count = count + 1;
}
return 0;
}
10
for Construct
Problem statement
Create a program that calculates and displays the pay for a number of
employees given hours worked and hourly pay rate.
Soucecode:
#include <iostream>
using namespace std;
int main()
{
int count, hours, employees;
float rate, pay;
cout << Enter number of employees;
cin>>employees;
for (count=1;count<=employees; count=count+1)
{
cout << Enter your hours worked;
cin>>hours;
cout << Enter your pay rate;
cin>>rate;
pay = hours*rate;
cout<<Your pay for this month is RM <<pay;
}
return 0;
}
11
while/for
construct
Concept
Running total:
also known as the total of a series of numbers
12
while/for
construct
Concept
while/for
construct
Pseudocode:
Start
set count to 1
set sum to 0
while count < 4
Read num
initialize
count, sum
test count
accumulate
sum
3
4
update
count
Note:
sum is the variable used
to accumulate values of
num
14
while/for
construct
Problem statement
Calculate the sum of three integer numbers.
#include <iostream>
using namespace std;
int main()
{
int count =1,sum=0, number;
while (count<=3)
{
cout<<Enter number;
cin>>number;
sum = sum + number;
count = count + 1;
}
cout<<The total is:<<sum;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int sum=0, number;
for(int count=1;count<=3;count=count+1)
{
cout<<Enter number;
cin>>number;
sum = sum + number;
}
cout<<The total is:<<sum;
return 0;
}
15
while/for
construct
Problem statement
Suppose you are writing a program that calculates a businesss total
sales for a week. The program would read the sales for each day as
input and calculate the total of those numbers.
16
while/for
construct
Pseudocode:
Start
set count to 1
set total sales to 0
while count <= 7
Read sales
initialize
count, sum
test count
accumulate
total sales
4
update
count
17
while/for
construct
{
int count, total=0, sales;
while (count<=7)
{
cout<<Enter sales;
cin>>sales;
total = total+ sales;
count = count + 1;
}
cout<<The total is:<<total;
return 0;
}
for(count=1;count<=7;count=count+1)
{
cout<<Enter sales;
cin>>sales;
total=total + sales;
}
cout<<The total is:<<total;
return 0;
}
18
Repetition
Sentinel-controlled
Repetition
Concept
19
while/for
construct
Problem statement
Create a program that calculates and displays the area of a circle. It allows
the user to enter the radius repeatedly, then 0 to end the program.
20
while/for
construct
Problem statement
Create a program that calculates and displays the area of a circle. It
allows the user to enter the radius repeatedly, then 0 to end the
program.
{
float radius,area;
cout<<Enter radius;
cin>>radius;
while (radius>0)
{
area = 3.142 *radius*radius;
cout<<The area is:<<area;
cout<<Enter radius;
cin>>radius;
}
return 0;
}
float radius,area;
for(cin>>radius;radius>0;cin>>radius)
{
area = 3.142 *radius*radius;
cout<<The area is:<<area;
cout<<Enter radius;
}
return 0;
}
21
while/for
construct
Problem statement
The owner of the Totally Sweet Shoppe hired part-time employees,
who earn RM12.50 per hour. The owner wants a program that
calculates and displays the pay based on hours worked for as many
employees as needed without having to run the program more than
once, and the hours worked will always be positive numbers and
greater than 0.
22
while/for
construct
Pseudocode:
Start
prime input
Read hours worked
test radius for
sentinel value
while hours worked >= 1
Calculate pay:
pay = 12.50 x hours worked
Display pay
update input
Read hours worked
endwhile
End
23
while/for
construct
While construct
int main()
{
int hours;
float pay;
For construct
int main()
{
int hours;
float pay;
while(hours>=0)
{
pay = hours*12.50;
for(cin>>hours;hours>=0;cin>>hours)
{
pay = hours*12.50;
24
while/for
construct
Problem statement
The Wheels & More store has several part-time employee; they are
paid RM10 per hour. The store manager wants a program that
calculates and displays the gross pay amount for as many employees
as needed without having to run the program more than once.
Because the number of hours an employee worked can be a positive
number only, the store manager will indicate that he is finished with
the program by entering a negative number (in this case -1) as the
number of hours.
25
Start
Read hours
while (hours is not equal to -1)
Calculate the gross pay
gross pay = hours x RM10
Print gross pay
Read hours
end while
End
Read hours
false
End
true
gross pay = hours x RM10
Print gross pay
Read hours
{
int hours, gross_pay;
cout <<" Enter hours worked ;
cin>> hours;
while (hours!=-1)
{
gross_pay = hours x 10 ;
cout <<" Your weekly gross pay is RM <<gross_pay;
cout <<" Enter hours worked ;
cin>> hours;
}
return 0;
}
cout<<gross_pay;