Professional Documents
Culture Documents
Lesson Notes On Loops
Lesson Notes On Loops
Control Constructs:
Sequence
Selection
Repetition/Loops/Iteration
Types of Loops:
1. For Loop
2. While Loop
3. Repeat Until Loop
This is a definite loop. The loop is repeated for a set number of times.
All the statements between the FOR and the ENDFOR are the loop block or compound statement. A FOR loop can be
used as a counter to count the number of times calculations are carried out. Once a FOR loop is being used, a counter
variable must be declared. The counter variable counts the number of times the loop is executed and thus should always
be declared as an integer. A counter variable can be a letter, the word count or counter, or your initial etc (A, count,
counter, FD etc.)
PSEUDOCODE FORMAT:
PASCAL FORMAT:
FOR countervariable := startvalue TO stopvalue DO
BEGIN
Compound statement/Actions to be repeated;
END;
FLOWCHART FORMAT:
Compound Statement(s)
(symbols will vary)
ACTIVITY:
Write a psuedocode to output the message “Are we there yet” 100 times.
Pseudocode Solution:
{This algorithm will output the message “Are we there Flowchart Solution
yet” 100 times}
Var C : integer
Var C : Integer
Start
C 0
C🡨0
Stop
Pascal Solution:
Program Message;
Uses crt;
Var C : integer;
Begin
C := 0;
FOR C := 1 to 100 DO
Begin
Writeln (‘Are we there yet’);
End;
End .
WHILE LOOP
This loop is used when you want to repeat instruction for an indefinite number of times. The While loop is a pre - tested
loop. This means that the condition is tested before the loop’s body is executed. An initial value is assigned to a variable
and the instructions are repeated until that variable reaches a certain value or point. If the test before the loop is false
then the body will not be executed. The loop is ended when the condition becomes false. The value that is used to end
the loop is call the sentinel value.
PSUEDOCODE FORMAT:
While condition Do
Compound statement/Actions to be repeated
Endwhile
PASCAL FORMAT:
While (condition) Do
BEGIN
Compound statement/Actions to be repeated;
End;
FLOWCHART FORMAT:
True
Compound Statement(s)
(symbols will vary depending on the statement(s)
This loop is also used when you want to loop for an indefinite number of times. The Repeat until loop is a post - tested
loop. The condition is tested after the loop’s body is executed. An initial value is assigned to a variable and the
instructions are repeated until that variable reaches a certain value or point. If the test after the loop is true then the
body will not be executed. The loop is ended when the condition becomes true.
ACTIVITY: Write an algorithm and flowchart to read a sequence of numbers terminated by 0 and print the sum of all
numbers.
PSEUDOCODE FORMAT:
Repeat
Compound statement/Actions to be repeated
Until Condition
PASCAL FORMAT:
Repeat
Compound statement/Actions to be repeated;
Until (Condition);
FLOWCHART FORMAT:
True
ACTIVITY:
Write an algorithm, pascal code and flowchart to read a sequence of numbers terminated by 0 and print the sum of all
numbers.
NB: Both While and Repeat Until can be used since the number of times the statements to be repeated is unknown.
While Loop Solution
{This program reads a sequence of numbers terminated
Pseudocode by 0. It will also print the sum of all the numbers}
Start num := 0;
sum := 0;
num 🡨 0
sum 🡨 0 Writeln (‘Enter a number. If you want to terminate
enter 0’);
Print ‘Enter a number. If you want to terminate enter 0” Read (num);
Read num
WHILE (num < > 0) DO
WHILE num < > 0 DO Begin
sum 🡨 sum + num sum := sum + num;
Print ‘Enter a number. If you want to terminate Writeln (‘Enter a number. If you want to
enter 0” terminate enter 0’);
Read num Read (num);
End;
ENDWHILE
Writeln (‘The sum of the numbers is’, sum);
Print ‘The sum of the numbers is’, sum
Stop
End .
While loop (continued)
Flowchart Solution
Pascal
Start
True
Read num
False Stop
WHILE num <
>0
Repeat Until Loop Solution
{This algorithm reads a sequence of numbers {This program reads a sequence of numbers terminated
terminated by 0. It will also print the sum of all the by 0. It will also print the sum of all the numbers}
numbers}
Program SequenceOfNumbers;
Algorithm SequenceOfNumbers
Uses crt;
Var num: integer
sum:integer Var num: integer;
sum:integer;
Start
Begin
num 🡨 0
sum 🡨 0 num := 0;
sum := 0;
Repeat
Repeat
Print ‘Enter a number. If you want to terminate Writeln (‘Enter a number. If you want to
enter 0’ terminate enter 0’);
Read num Read (num);
Print ‘The sum of the numbers is’, sum Writeln (‘The sum of the numbers is’, sum);
Stop
End .
Start
Var num: integer
sum:integer
num 0
sum 0
Read num
False
Stop