Loops (Iterations/Repetitions)
- Isa programming construct that is used to repeatedly execute some
statements as long as a specified condition is met.
As an example, if I were to collect the ages of 15 students, the way I collect 1st
student’s age is the same way that I collect the 2nd, 3rd ,4th and so on student’s age.
This means that the same action is repeated for each student. In this case, instead of
declaring 15 separate variables for collecting each student’s age, we declare 1
variable for collecting each student’s age at a time
3.
Three Types ofLoops
For Loop
A control structure that repeats a block of code a specific number of times, usually with a counter
that increments or decrements each iteration.
While Loop
A control structure that repeatedly executes a block of code as long as a specified condition
remains true. The condition is checked before each iteration.
Do-While Loop (or Repeat-Until Loop)
A loop that executes the block of code at least once and then continues to repeat it as long as the
specified condition is true (or until the condition becomes true). The condition is checked after
each iteration.
Nested Loop
A loop inside another loop, where the inner loop runs completely for each iteration of the outer
loop. Used to handle multi-dimensional data or repeated repeated tasks.
4.
For Loop
•Repeats ablock of code a specific number of times.
•Usually used when the number of iterations is known beforehand.
•Example: Loop through numbers 1 to 5.
for <initialization> <condition>
statement(s)
endfor
Write a pseudocode using a for Loop to display student
numbers from 1 to 5 Algoritghm
Start
int Number
for i = 1 =< 5
print "Student number ", i
Endfor
End
5.
While Loop
•Repeats ablock of code as long as a specified condition is true.
•Used when the number of iterations is not known and depends on a condition.
while <condition> <increment/decrement>
statement(s)
endwhile
Write a pseudocode using a while loop that prints
the value of a counter starting from 0 until it is less
than 5. Increment the counter by 1 after each
print.
Start
Int number
while counter < 5 ; ++1
print "Counter is ", counter
counter = counter + 1
Endwhile
End
OR
6.
Do-While Loop (orRepeat-Until Loop)
- Similar to the while loop, but the code block runs at least once before checking the
condition.
- The condition is checked after the code execution.
Dowhile <condition>
statement(s)
enddo
Example: Dowhile Loop through numbers 1 to 5.
Start
Int number
i = 1
DO
PRINT i
i = i + 1/ increment i by 1
WHILE i <= 5
enddo
End
7.
A nested loop
-is a loop inside another loop. In programming and pseudocode, it allows you to repeat a set of
instructions within another repeated set.
Nested Loop
Description
•The outer loop runs first.
•For each iteration of the outer loop, the inner loop runs completely.
•Nested loops are often used for tasks involving grids, tables, or multiple levels of iteration (like rows
and columns).
while <condition1>
statement(s)
while <condition2>
statement(s)
endwhile
8.
Question:
Write a nestedwhile loop in pseudocode to display a multiplication table for numbers 1 to 3. The
outer loop will represent the first number, and the inner loop will represent the second number.
Explanation:
•The outer loop controls the first number (i) from 1 to 3.
•The inner loop controls the second number (j) from 1 to 3 for each value of i.
•For each pair (i, j), the program prints the product i * j.
9.
SET i =1
WHILE i <= 3 DO
SET j = 1
WHILE j <= 3 DO
DISPLAY i + " x " + j + " = " + (i * j)
INCREMENT j BY 1
END WHILE
INCREMENT ++ 1 / increment i by 1
END WHILE