The second type of iteration we will look at is the while iteration. This type of conditional loop tests for terminating condition at the beginning of the loop. In this case no action is performed at all if the first test causes the terminating condition to evaluate as false.
The syntax is;
WHILE (a condition is true)
A statement or block of statements
ENDWHILE
Example 1: A program segment to print out each character typed at a keyboard until the character ‘q’ is entered.
WHILE letter <> ‘q’
ACCEPT letter
DISPLAY “The character you typed is”, letter
ENDWHILE
Example 2: Write a program that will output the square root of any number input until the number input is zero.
In some cases, a variable has to be initialized before execution of the loop as shown in the following example.
Use variable: number of type real
DISPLAY “Type in a number or zero to stop”
ACCEPT number
WHILE number <> 0
Square = number * number
DISPLAY “The square of the number is”, square
DISPLAY “Type in a number or zero to stop”
ACCEPT number
ENDWHILE