‘WHILE’ LOOP CONSTRUCT vs ‘REPEAT…UNTIL’ LOOP CONSTRUCT
COMPARISON OF ‘WHILE’ LOOP AND ‘REPEAT...UNTIL’ LOOP
SIMPLE EXAMPLES OF ‘WHILE’ vs ‘REPEAT...
Eg.) Write an algorithm to accept an unspecified amount of integers.Calculate and print the sum of the numbers entered. The program should be terminated when the number 999 is entered.
Answer: ‘WHILE’ LOOP
Start
Print(‘Please enter a number’)
Read(num)
WHILE (num <> 999) DO
sum = sum + num
Print(‘Please enter a number’)
Read (num)
ENDWHILE
Print(‘The sum is: ‘, sum)
Stop
|
Answer: ‘REPEAT...UNTIL’ LOOP
Start
Print(‘Please enter a number’)
Read(num)
REPEAT
sum = sum + num
Print(‘Please enter a number’)
Read(num)
UNTIL (num = 999)
Print(‘The sum is: ‘, sum)
Stop
|
Comments (0)
You don't have permission to comment on this page.