Loops - For, While and Repeat Until
Loops - For, While and Repeat Until
instructions, code block or combination thereof as long as a specified condition is met. Loops are
fundamental to the concept of iteration in programming, enhancing code efficiency, readability and
promoting the reuse of code logic.
There are different types of loops used in programming and for pseudocodes at the CSEC level, you
will encounter three main types of loops which fall into TWO categories.
As was said earlier a loop is a structure that is used to ITERATE or LOOP a set of instructions, code
block or combination thereof as long as a specified condition is met loop, be it a Counter Controlled
Loop or Sentinel Controlled Loop.
A unique feature of this loop is that it abbreviates the code that manages the loop variable.
END.
NOW Consider the same problem, but using a FOR loop, as the secondary problem: Create a
pseudocode that will accept the hours worked for five (5) days by an employee. Calculate and
output the total hours worked, average hours worked.
BEGIN
day = 0
counter = 0
FOR counter = 1 TO 5 DO
begin
Print “Enter Hours Worked on Day ”, counter
Read day
total = total + day
end for
NOTE
1. FOR declares the loop about to be used. counter = 1 to 5 is setting the parameters for your FOR loop,
stating where the controlling counter starts and will stop. The controlling counter, IS ALWAYS an
integer variable. DO declares what will be looped. The block of code or set of instructions to be
looped/repeated is placed between the begin & end for tags
2. Print “Enter Hours Worked on Day ”, counter will simply print the text with the current value of the counter.
3. Read day Note there is no need for five variables as variables can be reused and assigned a new
value every time the loop repeats.
4. total = total + day an accumulator variable total. Each time the loop runs, the current value of day is
added to the existing value of total.
5. avghrs = total / counter after the completion of the loop, total would have the accumulated value of all the
entered hours, and is the most appropriate place to calculate the average, avghrs = total / counter .Note
also the division using counter because at the end of the loop, the counter will have the total number
of times the loop had run.
6. The Print “Total hours worked for 5 days is ”, total line of code will ONLY be executed AFTER the loop has
executed in its entirety
As stated earlier, a unique feature of the FOR loop is that it abbreviates the code that manages
the loop variable. The abbreviated code spoken of is the code that increments, adds 1 to the
controlling counter, counter each time the loop executes. Note there is no code for that, unlike total =
total + day.
USING A NESTED FOR LOOP
NOW Consider the secondary problem, with a twist, and using a NESTED FOR loop: Create a
pseudocode that will accept the hours worked for five (5) days for 5 employees. Calculate and
output the total hours worked, average hours worked for each employee.
BEGIN
day, totalhrs, avghrs = 0
X, Y = 0
name = “ “
FOR X = 1 TO 5 DO 1
Begin 1
Print “Enter Name of Employee ”, name 2
FOR Y = 1 TO 5 DO 3
begin 3
Print “Enter Hours Worked on Day ”, Y 3
Read day 3
total = total + day 3
end for 3
avghrs = total / y 3
end for 1
END.
In Blue is the loop that will prompt for and accept the name of 5 employees.
In Red is the very first loop created, to prompt for and accept the hours worked for five days
REMEMBER THIS:
NOTE: Code is executed line by line in a downward manner. When a FOR loop has ended (end for)
the next line of code will be executed:
end for 3
avghrs = total / y 3
end for 1
END.
NOTE
A controlling counter variable can have any name, as long as it’s an integer variable
1. FOR declares the loop about to be used. X = 1 to 5 is setting the parameters for your FOR loop, stating
where the controlling counter starts and will stop. The controlling counter, IS ALWAYS an integer
variable. DO declares what will be looped. The block of code or set of instructions to be
looped/repeated is placed between the begin & end for tags.
This loop takes an employee name and for every name taken, hours for five days is also taken. After
the hours for five days is taken, the inner loop would have been completed. As such the program
would now have a name, total & avghrs and can now print all the required information for an employee.
After which, the outer loop would now run for the second time, requesting a name and then executing
the inner loop once again
4. The original print statements which came with the original loop accepting 5 days of hours worked.
Sentinel Controlled Loops (WHILE-DO loop - controlled by a condition)
Repeats instructions, a code block or combination thereof, while a given condition is true. It tests the
condition before executing the loop body. A while-do loop statement does repetitive computations
till some test condition is satisfied. In other words, it repeatedly executes a target statement as long
as a given condition is true.
NOW Consider the same secondary problem, but using a WHILE loop instead of a FOR loop:
Create a pseudocode that will accept the hours worked for five (5) days by an employee.
Calculate and output the total hours worked, average hours worked.
BEGIN BEGIN
day, totalhrs, avghrs = 0 day, totalhrs, avghrs = 0
counter = 0 X=1
WHILE X < 6 DO 1
FOR counter = 1 TO 5 DO
begin 1
begin
Print “Enter Hours Worked on Day ”, X
Print “Enter Hours Worked on Day ”, counter
Read day
Read day
total = total + day total = total + day
end for X=X+12
avghrs = total / counter end while 1
avghrs = total / X
Print “Total hours worked for 5 days is ”, total
Print “Average hours worked 5 days is ”, avghrs Print “Total hours worked for 5 days is ”, total
Print “Average hours worked 5 days is ”, avghrs
END.
END.
NOTE
A controlling counter variable can have any name, as long as it’s an integer variable, X = 1. Note
initialization to 1.
1. WHILE declares the loop about to be used. X < 5 is the logical test /condition that sets the parameters
for the WHILE loop, stating the condition that must be true for the loop to execute. The controlling
counter, IS ALWAYS an integer variable. DO declares what will be looped. The block of code or set of
instructions to be looped/repeated is placed between the begin & end while tags.
2. X = X + 1 an accumulator variable X. Each time the loop runs, X , the counter, is incremented by 1.
Unlike in the FOR loop where the counter is incremented automatically
BEGIN BEGIN
day, totalhrs, avghrs = 0 day, totalhrs, avghrs = 0
X=1 X=6
Print “Total hours worked for 5 days is ”, total Print “Total hours worked for 5 days is ”, total
Print “Average hours worked 5 days is ”, avghrs Print “Average hours worked 5 days is ”, avghrs
END. END.
BOTH examples of the WHILE loops used/illustrated are limited. Limited due to fact it will never ever
loop beyond the set counter. WHILE loops can also run somewhat unlimited depending on their
setup.
Consider this problem, but using a somewhat unlimited WHILE loop: Mr. Brown grows and sells
pumpkins, which he sells by the pound. Create a pseudocode that will accept the weight of
individual pumpkins until 000 is entered as a weight. Calculate and output the total weight,
average weight of the pumpkins as well as the number of pumpkins weighed.
The pseudocode should accept the weight of any amount of pumpkins, as many as were harvested.
The pseudocode should also keep track of the number of pumpkins weighed.
The pseudocode should stop accepting the weight of pumpkins once 000 is entered as the weight of
a pumpkin.
Declare weight, totalweight, avgweight as Real
pumpkins as Integer
BEGIN
weight, totalweight, avgweight = 0
pumpkins = 0
END.
NOTE
There is no controlling counter.
The previous problem examples were limited by the test/condition of the loop.
LIMITED UNLIMITED
X=1 weight, totalweight, avgweight = 0
pumpkins = 0
WHILE X < 6 DO
begin Print “Enter the weight of a pumpkin”
Read weight
Print “Enter Hours Worked on Day ”, X
Read day WHILE weight < > 000 DO
total = total + day begin
totalweight = totalweight + weight
X=X+1
Print “Enter Weight of another pumpkin”
end while Read weight 5
end while
Print “Enter the weight of a pumpkin or 000 to exit” WHILE weight < > 000 DO
Read weight begin
Print “Enter Weight of pumpkin”
WHILE weight < > 000 DO Read weight 5
begin
totalweight = totalweight + weight totalweight = totalweight + weight
end while
Print “Enter Weight of another pumpkin”
Read weight
end while
NOTE the differences. Both examples will work because at no time is weight equal to 000.
2. WHILE weight < > 000 DO the logical test / condition returns TRUE and the loop is executed, as long as
the weight entered is not 0, so basically unlimited in the number of pumpkin weights that can be
recorded.
3. totalweight = totalweight + weight .The use of the accumulator, totalweight , again is increased by the
addition the value of the current value of the weight variable.
4. pumpkins = pumpkins + 1. The use of another accumulator, pumpkins, increased by the addition of 1 to
keep count of the number of pumpkins weighed so far.
5. Print “Enter Weight of another pumpkin”… Read weight. To ensure the loop runs again, the weight of another
pumpkin is gotten before returning to the test condition of the loop.
Unlike FOR and WHILE loops, which test the loop condition at the top of the loop, the repeat-until
loop checks its condition at the end of the loop. A repeat-until loop is similar to a while loop, except
that a repeat-until loop is guaranteed to execute at least one time.
NOW Consider the same secondary problem, but using a REPEAT-UNTIL loop instead of a WHILE
loop: Create a pseudocode that will accept the hours worked for five (5) days by an employee.
Calculate and output the total hours worked, average hours worked.
Declare hours, totalhrs, avghrs as Real
day as Integer
BEGIN
hours, totalhrs, avghrs = 0
days = 0
REPEAT
begin
Print “Enter Hours Worked on Day ”, days + 1
Read hours
totalhrs = totalhrs + hours
days = days + 1
end
UNTIL days = 5
After being exposed to FOR loops, NESTED FOR loops, limited WHILE loops and unlimited WHILE
loops, a REPEAT-UNTIL is pretty self explanatory.