While
Loop
There are two main types of
loops:
❑ Counter controlled loop
❑ Event controlled loop
EVENT CONTROLLED LOOP
1. These are loops controlled by some
condition within the body of loop, which
causes the repetition to stop. Examples
of “event controlled” loop are:
2. • Pre-test
3. • Post test
Pre-test
In this type of repetition, the condition is
evaluated prior to the performance of the loop
statements. If the condition is evaluated “true”,
the statement(s) within the block are executed.
These statements are done over and over until
the condition evaluates to false. An example of
the pre-test is the WHILE loop.
SENTINEL CONTROLLED
WHILE loops are considered to be indefinite. By
indefinite we mean you are unable to tell in
advance how many times they will loop. They just
keep looping until “something” happens. The
“something” being referred to here is an input
value that will decide when this loop will end.
SENTINEL CONTROLLED
A sentinel value is a special value whose sole
purpose is to stop the loop. Sentinel values would
not be considered apart of the normal data to be
processed, but when “read” or “inputted “will
indicate that the end of the loop is reached.
SENTINEL CONTROLLED
In example 1 above, the
value of product is printed
and multiplied by 2 as long
as product’s value is less
than 10, the sentinel value
is therefore 10
HOW IS THE “SENTINEL VALUE”
USED?
a) You pick a value that you would never encounter for
normal data or one you would deliberately want to use
to end your program.
b) The user enters normal data as long as desired. When
the decision to end the loop is made, then the value in
(a) above, (sentinel value) is entered.
c) The loop would stop when the sentinel value is
encountered.
IMPLEMENTING THE EVENT
CONTROLLED LOOP USING THE “WHILE”
GENERAL FORMAT OF THE WHILE LOOP
Every WHILE loop will contain the following elements:
PRIME READ //Priming
WHILE <Condition is true> DO //Testing
<Statements to be repeated> //statement block
<Update Statement> // Updating
END WHILE
IMPLEMENTING THE EVENT
CONTROLLED LOOP USING THE “WHILE”
The priming statement is the statement that will initialize the
value of variable that will then be tested in the condition.
The Updating statement written in the body of loop that will
constantly update the value of the variable being tested.
The loop variable that is being tested in the control
statement to determine if the looping should be done.
Priming Statement
Loop Variable – Num
Loop Condition- (Num<>0)
Updating Statement
Exercise 1
A minibus charges $1.50 per passenger. Write a structured
algorithm to input the number of passengers for each day
terminated by 0. Calculate the daily revenue and output the
number of passengers for each day and the daily revenue.
Solution
Algorithm Minibus While (NumPass<>0) Do
Const Charge:Real1.50 DrevenueNumPass*Charge
Var Print ‘Number of passenger is’, NumPass
NumPass:Integer Print ‘Daily Revenue is’, Drevenue
Drevenue:Real
Start Print ‘Please enter the passengers.
Num0 To terminate the loop enter 0’
Drevenue0.0 Read NumPass
Print ‘Enter the passengers. Endwhile
To terminate the loop enter 0’ stop
Read NumPass
Exercise 2
Write a pseudocode algorithm which prompts the user to
enter the name of each student in a class terminated by
“END”. Read their names and output the message “Good
day!” together with their name on the same line of output.
Solution
Algorithm Students
VAR
Name : String
START
Name ‘ ‘
Print ‘Enter a student name. To terminate the loop enter END’
Read Name
WHILE Name < > ‘END’ DO
Print ‘Good Day ‘, Name
Print ‘Enter a student name. To terminate the Loop enter END’
Read Name
ENDWHILE
STOP