0% found this document useful (0 votes)
40 views10 pages

Loops - For, While and Repeat Until

The document explains loops in programming, focusing on their role in iteration and code efficiency. It categorizes loops into Counter Controlled Loops (like for loops) and Sentinel Controlled Loops (like while-do loops), detailing their structures and uses through pseudocode examples. Additionally, it compares different looping methods for calculating total and average hours worked by employees over multiple days.

Uploaded by

amanolamey1221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views10 pages

Loops - For, While and Repeat Until

The document explains loops in programming, focusing on their role in iteration and code efficiency. It categorizes loops into Counter Controlled Loops (like for loops) and Sentinel Controlled Loops (like while-do loops), detailing their structures and uses through pseudocode examples. Additionally, it compares different looping methods for calculating total and average hours worked by employees over multiple days.

Uploaded by

amanolamey1221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Loops in programming are control structures that enable the repeated execution of a set of

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.

The TWO CATEGORIES of loops are:


1. Counter Controlled Loops (controlled by a counter)
2. Sentinel Controlled loops (controlled by a condition)

The TYPES of loops in the two CATEGORIES are:


Sentinel Controlled loops Counter Controlled Loops
(controlled by a condition) (controlled by a counter)
while-do loop for loop
Repeats a statement or group of statements while a Executes a sequence of statements multiple
given condition is true. It tests the condition before times and abbreviates the code that manages
executing the loop body. the loop variable.
repeat-until loop
Like a while statement, except that it tests the
condition at the end of the loop body.
nested loops
You can use one or more loop inside any another while, for or repeat until loop.

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.

Counter Controlled Loop (FOR LOOP - controlled by a counter)


A FOR loop is controlled by an INTEGER counter/ loop variable. This counter will determine the
number of times a block of code or set of instructions is looped/repeated. The counter determines the
specified number loops and at when the loop will stop, then the next line of code will be executed.

A unique feature of this loop is that it abbreviates the code that manages the loop variable.

NOTE: Code is executed line by line in a downward


manner. When the FOR loop has ended (end for) the next
line of code will be executed: Print “Total hours …. ”
Consider this problem the original 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.

Declare day1, day2, day3, day4, day5, totalhrs, avghrs as


Real
BEGIN
day1, day2, day3, day4, day5 = 0

Print “Enter Hours Worked on Day 1”


Read day1
Print “Enter Hours Worked on Day 2”
Read day2
Print “Enter Hours Worked on Day 3”
Read day3
Print “Enter Hours Worked on Day 4”
Read day4
Print “Enter Hours Worked on Day 5”
Read day5

totalhrs = day1 + day2 + day3 + day4 + day5


avghrs = totalhrs / 5

Print “Total hours worked for 5 days is ”, totalhrs


Print “Average hours worked for 5 days is ”, avghrs

END.

USING A SIMPLE FOR LOOP

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.

Declare day, totalhrs, avghrs as Real


counter as Integer

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

Print “Total hours worked for the 5 days is ”, total


END.
Now let’s compare
WITHOUT LOOP WITH FOR LOOP
Declare day1, day2, day3, day4, day5, totalhrs, avghrs as Real Declare day, totalhrs, avghrs as Real
counter as Integer
BEGIN
day1, day2, day3, day4, day5 = 0
BEGIN
Print “Enter Hours Worked on Day 1” day, totalhrs, avghrs = 0
Read day1 counter = 0
Print “Enter Hours Worked on Day 2”
Read day2
FOR counter = 1 TO 5 DO 1
Print “Enter Hours Worked on Day 3”
Read day3 begin 1
Print “Enter Hours Worked on Day 4” Print “Enter Hours Worked on Day ”, counter 2
Read day4 Read day 3
Print “Enter Hours Worked on Day 5” total = total + day 4
Read day5
end for 1
totalhrs = day1 + day2 + day3 + day4 + day5
avghrs = totalhrs / 5 avghrs = total / counter 5

Print “Total hours worked for 5 days is ”, totalhrs


Print “Total hours worked for 5 days is ”, total
Print “Average hours worked for 5 days is ”, avghrs
Print “Average hours worked 5 days is ”, avghrs
END.
END.

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.

Declare day, totalhrs, avghrs as Real


x, y as Integer
name as String

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

Print “Employee name is ”, total 3


Print “Total hours worked for 5 days is ”, total 4
Print “Average hours worked 5 days is ”, avghrs 4

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

Print “Employee name is ”, total 4


Print “Total hours worked for 5 days is ”, total 4
Print “Average hours worked 5 days is ”, avghrs 4
Now let’s compare
SIMPLE FOR LOOP NESTED FOR LOOP
Declare day, totalhrs, avghrs as Real Declare day, totalhrs, avghrs as Real
counter as Integer x, y as Integer
name as String
BEGIN
BEGIN
day, totalhrs, avghrs = 0
day, totalhrs, avghrs = 0
counter = 0
X, Y = 0
name = “ “
FOR counter = 1 TO 5 DO
begin
FOR X = 1 TO 5 DO 1
Print “Enter Hours Worked on Day ”, counter
Begin 1
Read day
Print “Enter Name of Employee ”, name 2
total = total + day
end for
FOR Y = 1 TO 5 DO 3
avghrs = total / counter
begin 3
Print “Enter Hours Worked on Day ”, Y 3
Print “Total hours worked for 5 days is ”, total
Read day 3
Print “Average hours worked 5 days is ”, avghrs
total = total + day 3
end for 3
END.
avghrs = total / y 3

Print “Employee name is ”, total 3


Print “Total hours worked for 5 days is ”, total 4
Print “Average hours worked 5 days is ”, avghrs 4

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

2. A name is requested in the outer loop.


3. The very first loop which runs 5 times is now required to run 5 times for each employee name
accepted.

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.

WHILE (condition) DO (something)


Where, condition is an expression whose value/result would be true or false and something is a
simple statement or group of statements within a begin ... end while block. The condition may be set
up in various ways.

USING A SIMPLE (LIMITED) WHILE LOOP

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.

Now let’s compare


SIMPLE FOR LOOP SIMPLE WHILE LOOP (LIMITED)
Declare day, totalhrs, avghrs as Real Declare day, totalhrs, avghrs as Real
counter as Integer X as Integer

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

Another way the WHILE loop could be set up


ORIGINAL WHILE LOOP ALTERNATE WHILE LOOP
Declare day, totalhrs, avghrs as Real Declare day, totalhrs, avghrs as Real
X as Integer X as Integer

BEGIN BEGIN
day, totalhrs, avghrs = 0 day, totalhrs, avghrs = 0
X=1 X=6

WHILE X < 6 DO WHILE X < > 1 DO


begin begin
Print “Enter Hours Worked on Day ”, X Print “Enter Hours Worked on Day ”, X-1
Read day Read day
total = total + day total = total + day
X=X+1 X=X-12
end while end while
avghrs = total / X avghrs = total / X

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.

USING A SIMPLE (UNLIMITED) WHILE LOOP

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.

Consider the requirements.

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

Print “Enter the weight of a pumpkin” 1


Read weight

WHILE weight < > 000 DO 2


begin
totalweight = totalweight + weight 3
pumpkins = pumpkins + 1 4

Print “Enter Weight of another pumpkin” 5


Read weight 5
end while

avgweight = totalweight / pumpkins 6

Print “Total pumpkins weighed is ”, pumpkins


Print “Total pumpkin weight is ”, totalweight
Print “Average pumpkin weight is ”, avgweight

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

This problem example is not limited.


The counter being used is pumpkins. Note initialization to 0.
1. Print “Enter the weight of a pumpkin” and Read weight are the first two statements executed as the next line
of code is WHILE weight < > 000 DO which is the test/condition that determines if the loop runs

ORIGINAL UNLIMITED MODIFIED UNLIMITED


weight, totalweight, avgweight = 0 weight, totalweight, avgweight = 0
pumpkins = 0 pumpkins = 0

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.

USING A SIMPLE REPEAT-UNTIL 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

avghrs = totalhrs / days

Print “Total hours worked for 5 days is ”, totalhrs


Print “Average hours worked for 5 days is ”, avghrs
END.

After being exposed to FOR loops, NESTED FOR loops, limited WHILE loops and unlimited WHILE
loops, a REPEAT-UNTIL is pretty self explanatory.

You might also like