Lecture-4
Lecture-4
Lecture #4
Control Structures II (Repetitions)
while Looping (Repetition) Structure
Dr. Ala Alaker
1
Outline
1. Learn about repetition (looping) control structures.
sentinel-controlled,
flag-controlled
What if we
want to find the
average number
of calories burned
each day of a
month?
Is this a
better
alternative?
Update
Initialize Expression cout<<i<<“ “;
Iteration LCV
LCV i <= 20 Statements
i=i+5
0 <= 20
1 i=0 0 0+5=5
is true
5 <= 20
2 i=5 10 5 + 5 = 10
is true
10 <= 20
3 i=10 15 10 + 5 = 15
is true
15 <= 20
4 i=15 20 15 + 5 = 20
is true
20<= 20
5 i=20 25 20 + 5 = 25
is true
25 <= 20
6 i=25 The loop terminates
is false 9
10
Using while loop rewrite the program that finds the average number of
calories burned in a week.
11
Example:
Using Sentinel-Controlled while Loop write a program that compute
and outputs the total number of boxes of cookies sold and the
average number of boxes sold by each volunteer. However, the
program does not know the exact number of Volunteers. Therefore,
the program will prompts the user to enter the value for name as
long as name is not equal to End which is the SENTINEL.
13
14
Example:
Using Flag-Controlled while Loop write a program that asks the
user to enter numbers which are added together. To stop the
addition the user enters a negative number.
16