Sunteți pe pagina 1din 29

8.

0 PROGRAMMING
8.5 Use of Control Structure

Prepared by Computer Science Unit

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

Write a program segment by using appropriate control


structure

while, for construct

Counter-controlled looping and sentinel-controlled


looping
2

Control Structure

Repetition

A series of statements in a loop that are repeated


until a certain condition is met

Repetition structure:
also called looping control structure

Repetition
int i =1;
while( i <=3)

The while Construct


i)initialization Initialize a variable (loop control
variable), before the loop begins
ii)Test the loop control variable in
the while condition; if the condition
is true, the loop begins executing

{
/*Create the braces because statements
block are more
than one, otherwise it will create an
infinity result output*/
i=i+1;

Process items inside


looping are based on
condition given

iii)Update the value of the loop control


variable counter increment

Repetition

The for Construct

It has the following standard form.


1

for (initialization; test; update)


statement block;
1
2

initialization Initialize a variable (loop control variable), before


the loop begins
Test the loop control variable in the while condition; if the
condition is true, the loop begins executing, otherwise the loop
will be terminated.
Update the value of the loop control variable typically
increments (or decrements) the loop index - performed every time
through the loop iteration used to increment any variable(s) that
may need to be incremented.

while construct

Using Counter: Example #1

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

Using Counter: Example #1

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

Using Counter: Example #2

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

Using Counter: Example #2

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

Using Counter: Example #3

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

Using Counter: Example #3

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

Calculating a Running Total


Using Counter-controlled Loop

Concept

A running total is a sum of numbers that


accumulates with each iteration of a loop. The
variable used to keep the running total is called an
accumulator.

Running total:
also known as the total of a series of numbers

12

while/for
construct

Calculating a Running Total


Using Counter-controlled Loop

Concept

Programs that calculate the total of a series of


numbers typically use two elements:
(1) A loop that reads each number in the series.
(2) A variable that accumulates the total of the
numbers as they are read.
13

while/for
construct

Accumulating Using Counter-controlled


Loop : Example #1 - Calculate the sum
of three integer numbers.

Pseudocode:
Start
set count to 1
set sum to 0
while count < 4
Read num

initialize
count, sum
test count

accumulate
sum

sum = sum + num


count = count + 1
endwhile
Print sum
End

3
4
update
count

Note:
sum is the variable used
to accumulate values of
num

14

while/for
construct

Accumulating Using Countercontrolled Loop: Example #1

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

Accumulating Using Countercontrolled Loop: Example #2

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

Accumulating Using while Loop:


Example #2

Pseudocode:
Start
set count to 1
set total sales to 0
while count <= 7
Read sales

initialize
count, sum
test count

total sales = total sales + sales


count = count + 1
endwhile
Print total sales
End

accumulate
total sales

4
update
count

17

while/for
construct

Accumulating Using while Loop:


Example #2

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.
{
int count =1,total=0,sales;

{
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

A sentinel-controlled loop uses a sentinel value to


stop a loop.

19

while/for
construct

Using Sentinel: Example #1

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

Using Sentinel: Example #1

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

Using Sentinel: Example #2

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

Using Sentinel: Example #2

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

Using Sentinel: Example #2

While construct
int main()
{
int hours;
float pay;

For construct
int main()
{
int hours;
float pay;

cout << Enter your hours worked;


cin>>hours;

cout << Enter your hours worked;

while(hours>=0)
{
pay = hours*12.50;

for(cin>>hours;hours>=0;cin>>hours)
{
pay = hours*12.50;

cout<<Your pay for this month is RM <<pay;

cout<<Your pay for this month is RM <<pay;

cout << Enter your hours worked;


cin>>hours;

cout << Enter your hours worked;


}

24

while/for
construct

Using Sentinel: Example #3

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

Pseudocode & Flow chart


Start

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

while (hours not


equal to -1)

true
gross pay = hours x RM10
Print gross pay
Read hours

Source code & Pseudocode


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

{
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;

Exercise: Change this pseudocode to source


code C++ (while and for construct)
Begin
Read monthly sales
while (monthly sales is greater than 0)
if (monthly sales greater than or equal 10000.00)
Income = 300.00 + 0.05 * monthlysales
else
Income = 100.00 +0.01 * monthlysales
Print Income
Read monthly sales
end while
End

Exercise : Write C++ program,


To find maximum number between three integer
numbers.
To find maximum number for 100 integer numbers.
To find a maximum mark of nth number of students.
That enable user to enter 5 numbers. Display odd,
even or zero number based on number entered by
user. Calculate and display the quantity/frequency of
even or odd number from the user.
29

S-ar putea să vă placă și