You are on page 1of 6

WHILE – DO LOOP

INDEFINIT
E LOOP The while loop repeats a set of statements for
an unspecified number of times until a given
condition (a statement that works out to be true
or false) is FALSE.

✔ When you do not know beforehand how


many times statements within a loop are to
be repeated, a WHILE loop may be used.

● In the WHILE construct, the condition is


WHILE tested; if it is true, the instructions within
the WHILE and ENDWHILE are executed
Condition until the condition becomes false and the
loop is then exited.
DO
● Statements/instructions before the
WHILE construct are carried out ONCE.
ENDWHILE The while loop is executed until the
condition becomes false, which forces the
Sentinel Value loop to stop. The statements after the
ENDWHILE are carried out ONCE.
Dummy Value
● If, after carrying out the instructions
Terminating Value before the loop, the condition in the
WHILE loop is tested and is FALSE, the
computer skips the instructions within the
WHILE loop and continues with the
statements after ENDWHILE.
❖ A sentinel value is a lookout value such that if the
data being entered ever becomes equal to this value,
the computer exits the loop.

❖ The trigger that causes the loop to stop is a value


that is entered for the input data. This value is
read in within the loop and it is called a dummy
value, a terminating value or sentinel value. It
must not be one of the values in the list of data to
be processed. E.g. When calculating the average
age of students in a class, a dummy value could be
INITIALIZATION (Optional)
Used to prompt for and

WHILE – OUTPUT STATEMENT receive data to test the


condition
DO
Construct INPUT STATEMENT

WHILE CONDITION DO

Statements to be repeated
Used to prompt for and
receive data to test the
condition. This is needed
OUTPUT STATEMENT again given that the similar
statements prior only execute

INPUT STATEMENT
once

ENDWHILE
BEGIN

DECLARE
Write a pseudocode algorithm to
accept a set of positive numbers,
terminated by 0. Calculate and print NUM SUM AS INTEGER
the sum of the numbers.

SUM 0

PRINT “ENTER A NUMBER, 0 TERMINATES”

READ NUM

WHILE NUM DO
<> 0

SUM SUM + NUM

PRINT “ENTER A NUMBER, 0 TERMINATES”

READ NUM

ENDWHILE

SUM PRINT “SUM OF THE NUMERS IS”


,
END
Write a pseudocode algorithm to enter the
age and count the number of students in a
class. Calculate the average age of the
group of students if the data entered is
terminated by 999. Print the number of
students in the class and the average age
of the students.

“ENTER STUDENT’S AGE, 999 TERMINATES” AGE COUNT


AS
BEGIN DECLARE TOTAL TOTAL
AS 0
END AGE AGE TOTAL PRINT ,
COUNT AGE
AGE TOTAL WHILE ,
INTEGER PRINT PRINT READ DO <>

“ENTER STUDENT’S AGE, 999 TERMINATES” READ


ENDWHILE 0 1
“NUMBER OF STUDENTS IS” + + COUNT
COUNT
REAL
“AVERAGE AGE OF STUDENTS”
/ 999 COUNT
AVG_AGE
PRINT AVG_AGE AVG_AGE
Write a pseudocode algorithm to read a
set of numbers terminated by 0; print
the number of positive and the number
of negative numbers.

R Birbal, M Taylor, Log on to IT 2nd Edition 2010

You might also like