LOOPS
Problem Solving
Grade 11
OBJECTIVES
By the end of the lesson students should
be able to:
State the difference between definite and
indefinite loops
Differentiate among the three types of loop
structures ( For loop, While loop and repeat until)
Classify each loop structure as definite and
indefinite.
Include for loops structures in a pseudocode
algorithm.
WHAT ARE LOOPS?
Loops are constructs used for repeating
parts of a program. That is, they will
repeatedly execute a section of a program until
a condition is satisfied.
LOOPS SIMPLIFIED
https://www.youtube.com/watch?v=w
xds6MAtUQ0
THE TWO TYPES OF LOOPS
Definite loops: With this type of loop you
know the exact amount of times you will
need to repeat the statements in a loop
structure.
Indefinite: With this type of loop you don’t
know how many times you will need to
repeat the statements in a loop structure.
THREE COMMON LOOP
STRUCTURES
Three common loop structures are:
FOR
While-Do
Repeat Until
STRUCTURE OF A LOOP
The basic structure of a loop is:
Initialization
Loop Header
Loop statements/ Loop body /Loop block
Conclusion
INITIALIZATION
Loops use a variable to control the amount
of iterations. We need to initialize the variable
to a start value or read an initial value into the
variable.
LOOP HEADER
It states the type of loop as well as a logical
test against the controlling variable. It may
have the initialization if it is a FOR loop.
LOOP BLOCK
We must specify what statements are to be
repeated by the computer. The repetitive
statements are normally placed in the loop
block.
CONCLUSION
This is simple keywords to signify the end of
the loop structure.
FOR LOOP
The FOR loop is usually used to represent
definite loops.
FOR LOOP
The FOR Loop syntax is shown below:
FOR variable <start value> TO <end
value> DO
Statement 1
Statemnet 2
…
END FOR
SCENARIO:
You and your friends (9) have decided to put
together to collect money for a welfare
program at your school. The leader of the
group has wrapped the collection box which
will be used to store (accumulate) your
donations. How would you find the
accumulated amount at the end of
collecting? Find the average amount.
EXAMPLE 1
A program is required to read the ages
of 20 students. Calculate sum and
average age.
EXAMPLE 1
DECLARE count, sum, num AS INTEGER
Avg as real
sum 0
Avg0
num 0 { initialization }
START
FOR count 1 TO 10 DO { Header}
PRINT “Enter the age”
Read age
sum sum + age { accumulating}
END FOR
avgsum/count
Print “The sum is”, Sum
Print “The average is”, Avg
STOP
FOR LOOP FLOWCHART WITH
EXAMPLE
WHILE LOOP AND REPEAT UNTIL
The While-Do and the Repeat-Until can be
used to represent both indefinite & definite
loops. The Repeat until is a loop that tests a
condition at the end of the loop so the
statements in the loop will always be
executed at least once.
The Repeat until differs from the While-Do in
that the While-Do will execute until the
condition becomes false while the Repeat-
Until will execute until a condition becomes
true.
THE WHILE LOOP- INDEFINITE
The “While” loop is an example of an
indefinite loop, it facilitates the repetition
of a block of instructions as long as a
condition is true. No one knows exactly how
many times the block statements
(instructions) will be carried out. The trigger
that causes the while loop to stop is called a
terminating value or sentinel value.
WHILE-DO LOOP
The While Loop syntax is shown
below:
While variable <> sentinel value Do
Statement(s)
End While
THE WHILE LOOP- PASCAL
The While Loop syntax is shown below:
While Variable <> sentinel value Do
Begin
Statement(s);
End;
EXAMPLE 1
Write a pseudocode algorithm that will
accept the age of persons in a class . The
loop is terminated when the age -1 is
entered.. The pseudocode should find the
average age and output it.
PSEUDOCODE EXAMPLE 1
Algorithm Class WHILE age <> -1 DO
DECLARE age, sum, sum sum + age
cnt AS INTEGER cnt cnt + 1
DECLARE Avg as real
PRINT “Enter an age
START or -1 to end loop”
sum 0 READ age
cnt 0 END WHILE
avg 0 avg sum/cnt
PRINT “Enter an PRINT “The Average of
age or -1 to end” the numbers is ”, avg
STOP
READ age
EXAMPLE 2
This program will read 10 numbers and find
the sum of those numbers.
DECLARE counter, Sum, num AS INTEGER
sum num counter 0
START
WHILE counter <= 10 DO
PRINT “Enter the number”
Read num
Sum Sum + num
counter counter + 1
END WHILE
Print “The sum is”, Sum
STOP
EXAMPLE 3
This program will read some numbers and find the sum of
those numbers. The program terminates when 999 is
entered.
DECLARE counter, Sum, num AS INTEGER
sum num 0
START
PRINT “ Enter 9999 to end the program”
READ num
WHILE num <> 9999 DO
Sum Sum + num
PRINT “Enter the number”
Read num
END WHILE
Print “The sum is”, Sum
STOP
TRY
Write a program to accept the names of
students in a class. The program should
display a welcome message that must include
the name of the student. The program is
terminated when the word “end” is entered.
REPEAT-UNTIL LOOP
The repeat loop construct repeatedly
executes one or more statements as long as
the specified condition is false. Note that this
condition is tested at the end of the loop.
REPEAT-UNTIL LOOP
The general form of the Repeat-Until loop
REPEAT
Statement 1
Statemnet 2
…
UNTIL <condition statement>
SOLVE THE FOLLOWING
Write a pseudocode algorithm to set the
password to a door system to 3718. The
pseudocode should prompt the user for the
password until the correct password is
entered.
SOLVE THE FOLLOWING
Use the appropriate loop structure to write the following
pseudocodes:
Write an algorithm to accept the scores of sixty
students in a class. Calculate and print the average
scores.
Write an algorithm that finds the highest of twenty
values
Write a structured algorithm using pseudocode that
keeps prompting a person to enter her name until she
enters the word ‘quit’. Every time a person enters her
name, the computer should greet them. For example,
if the person entered her name as TiannaLee, the
program should output ‘Hi TiannaLee’.