Loops

Learn how to repeat code execution using for loops and while loops to automate repetitive tasks.

For Loops
Repeat code a specific number of times
Pattern:
for variable from start to end
# statements to repeat
endfor
The loop variable automatically counts from the start value to the end value (inclusive). Each iteration, the variable increases by 1.
While Loops
Repeat code while a condition is true
Pattern:
while condition
# statements to repeat
endwhile
The loop continues as long as the condition is true. Make sure to update variables inside the loop to eventually make the condition false.
When to Use Each Loop Type
Choosing the right loop for your situation

Use For Loops When:

  • You know exactly how many times to repeat
  • You need to count through a specific range
  • You want to process numbered items (1 to N)
  • You need the loop counter in your calculations
# Example: Print multiplication table
for i from 1 to 10
result is equal to 7 times i
print "7 x " plus i plus " = " plus result
endfor

Use While Loops When:

  • You don't know how many iterations you need
  • The condition depends on user input or calculations
  • You're searching for something specific
  • You need more complex stopping conditions
# Example: Find first number > 100
number is equal to 1
while number is less than or equal to 100
number is equal to number times 2
endwhile
print number
Nested Loops
Using loops inside other loops

Simple Nested Example

for row from 1 to 3
for col from 1 to 4
print "(" plus row plus "," plus col plus ")"
endfor
endfor
# Creates a 3x4 grid of coordinates

The inner loop runs completely for each iteration of the outer loop.

Multiplication Table

for i from 1 to 5
for j from 1 to 5
product is equal to i times j
print i plus " x " plus j plus " = " plus product
endfor
print ""
endfor

Creates a complete 5x5 multiplication table.

Common Loop Patterns
Frequently used loop structures and their applications

Accumulation Pattern

sum is equal to 0
for i from 1 to 100
sum is equal to sum plus i
endfor
print "Sum of 1 to 100: " plus sum

Building up a total by processing each item in sequence.

Counting Pattern

count is equal to 0
for i from 1 to 50
if i is greater than 25 then
increment count
endif
endfor
print "Numbers greater than 25: " plus count

Counting items that meet specific criteria.

Search Pattern

found is equal to false
target is equal to 42
counter is equal to 1
while found is equal to false
if counter is equal to target then
found is equal to true
print "Found " plus target plus " at position " plus counter
endif
increment counter
endwhile

Searching for something and stopping when found.

Validation Pattern

attempts is equal to 0
max_attempts is equal to 3
while attempts is less than max_attempts
print "Enter password:"
# password checking logic would go here
increment attempts
endwhile
print "Too many attempts"

Limiting the number of retry attempts.

Loop Variables and Control
Managing loop variables effectively

For Loop Variables

# The loop variable (i) is automatically managed
for i from 5 to 10
print "Current value: " plus i
# i increments automatically: 5, 6, 7, 8, 9, 10
endfor

For loop variables increment automatically and don't need manual updating.

While Loop Variables

# You must manually update the condition variable
countdown is equal to 5
while countdown is greater than 0
print countdown
decrement countdown # Essential!
endwhile
print "Blast off!"

While loop variables must be updated manually to avoid infinite loops.

Common Mistakes and Solutions
Error

Infinite while loops

Forgetting to update the condition variable inside a while loop.

# BAD: counter never changes
counter is equal to 1
while counter is less than 5
print counter # runs forever!
endwhile
Fix

Always update condition variables

# GOOD: counter changes each iteration
counter is equal to 1
while counter is less than 5
print counter
increment counter # Essential!
endwhile
Tip

Use descriptive loop variable names

Instead of "i", use names like "row", "student", "attempt" for clarity.

Tip

Be careful with loop ranges

"for i from 1 to 5" includes both 1 and 5. Double-check your start and end values.