Nested loops are loops inside other loops. The inner loop runs completely for each iteration of the outer loop, creating a powerful way to process 2D data, create patterns, and solve complex problems.
For outer = start1 To end1
# Outer loop code
For inner = start2 To end2
# Inner loop code (runs many times!)
Endfor
# More outer loop code
Endfor
Algorithm SimpleNested
Print "Nested loop demonstration:"
Print ""
For row = 1 To 3
Print "Outer loop - Row", row
For col = 1 To 3
Print " Inner loop - Column", col
Endfor
Print ""
Endfor
Endalgorithm
Nested loops are perfect for creating patterns:
Algorithm RectanglePattern
var rows = 4
var cols = 6
Print "Rectangle (", rows, "x", cols, "):"
Print ""
For row = 1 To rows
For col = 1 To cols
Print "* "
Endfor
Print "" # New line after each row
Endfor
Endalgorithm
Algorithm TrianglePattern
var height = 5
Print "Right Triangle:"
Print ""
For row = 1 To height
# Inner loop runs 'row' times
For col = 1 To row
Print "* "
Endfor
Print ""
Endfor
Endalgorithm
Algorithm NumberPyramid
var height = 5
Print "Number Pyramid:"
Print ""
For row = 1 To height
# Print row number, repeated 'row' times
For col = 1 To row
Print row, " "
Endfor
Print ""
Endfor
Endalgorithm
Algorithm MultiplicationTable
var size = Input "Enter table size (e.g., 10):"
Print ""
Print "=== Multiplication Table ==="
Print ""
For row = 1 To size
For col = 1 To size
var product = row * col
Print row, "×", col, "=", product
Endfor
Print "" # Blank line between rows
Endfor
Endalgorithm
Nested loops are essential for processing grid/table data:
Algorithm TemperatureGrid
Print "=== Weekly Temperature Data ==="
Print ""
# Simulate 7 days, 3 readings per day
var days = 7
var readings = 3
var totalSum = 0
var totalCount = 0
For day = 1 To days
Print "Day", day, ":"
var daySum = 0
For reading = 1 To readings
var temp = Input " Reading" + reading + ":"
daySum = daySum + temp
totalSum = totalSum + temp
totalCount = totalCount + 1
Endfor
var dayAverage = daySum / readings
Print " Day average:", dayAverage
Print ""
Endfor
var weekAverage = totalSum / totalCount
Print "=== Weekly Average:", weekAverage, "°C ==="
Endalgorithm
Algorithm InvertedTriangle
var height = 5
Print "Inverted Triangle:"
Print ""
For row = height To 1 Step -1
For col = 1 To row
Print "* "
Endfor
Print ""
Endfor
Endalgorithm
Algorithm DiamondPattern
var size = 5
Print "Diamond Pattern:"
Print ""
# Upper half (including middle)
For row = 1 To size
# Print spaces
For space = 1 To size - row
Print " "
Endfor
# Print stars
For star = 1 To (2 * row - 1)
Print "* "
Endfor
Print ""
Endfor
# Lower half
For row = size - 1 To 1 Step -1
# Print spaces
For space = 1 To size - row
Print " "
Endfor
# Print stars
For star = 1 To (2 * row - 1)
Print "* "
Endfor
Print ""
Endfor
Endalgorithm
Algorithm SimpleCalendar
var daysInMonth = Input "Enter days in month (28-31):"
var startDay = Input "Starting day of week (1=Mon, 7=Sun):"
Print ""
Print "=== MONTHLY CALENDAR ==="
Print ""
Print " Mon Tue Wed Thu Fri Sat Sun"
Print "-----------------------------"
# Print leading spaces for first week
For i = 1 To startDay - 1
Print " " # 4 spaces per day
Endfor
var currentDay = startDay
For day = 1 To daysInMonth
# Print day number (padded)
If day < 10 Then
Print " ", day, " "
Else
Print " ", day, " "
Endif
# New line after Sunday
If currentDay == 7 Then
Print ""
currentDay = 1
Else
currentDay = currentDay + 1
Endif
Endfor
Print ""
Print "-----------------------------"
Endalgorithm
Nested loops can get slow quickly! If the outer loop runs N times and inner loop runs M times, the total is N × M iterations.
Example:
Be careful with large numbers!