🔁 Loops in iPseudo

Master repetition and iteration with FOR, WHILE, and REPEAT loops!

🔄 FOR Loop

Repeat code a specific number of times:

pseudocode
Algorithm SimpleForLoop

Print "Counting from 1 to 5:"

For i = 1 To 5
    Print "Number:", i
Endfor

Print "Done!"

Endalgorithm

⚡ FOR Loop with Step

Algorithm ForLoopWithStep

Print "Even numbers from 2 to 10:"

For i = 2 To 10 Step 2
    Print i
Endfor

Print ""
Print "Countdown from 10:"

For i = 10 To 1 Step -1
    Print i
Endfor

Print "Blast off!"

Endalgorithm

🌀 WHILE Loop

Repeat code while a condition is true:

Algorithm SimpleWhileLoop

var count = 1

Print "Counting with WHILE loop:"

While count <= 5 Do
    Print "Count:", count
    count = count + 1
Endwhile

Print "Done!"

Endalgorithm

🎯 Choosing the Right Loop

Quick Guide:
  • Use FOR when you know how many times to repeat
  • Use WHILE when you don't know the iteration count
  • Use REPEAT-UNTIL when loop must execute at least once

🎮 Real-World Example: Factorial

pseudocode
Algorithm FactorialCalculator

var n = Input "Enter a number:"
var factorial = 1

Print ""
Print "Calculating", n, "! ..."

For i = 1 To n
    factorial = factorial * i
    Print i, "! =", factorial
Endfor

Print ""
Print "Final result:", n, "! =", factorial

Endalgorithm

📋 Loop Quick Reference

# Basic FOR loop
For i = 1 To 10
    # code
Endfor

# FOR with Step
For i = 0 To 100 Step 10
    # code
Endfor

# WHILE loop
While condition Do
    # code
    # Update condition!
Endwhile