Session 3: For & While Loops
Loops make computers repeat code. Without loops, you'd write the same code over and over. Computers are powerful because they never get tired!
A for loop repeats code a specific number of times. Perfect when you know exactly how many repetitions you need.
Output: 1, 2, 3, 4, 5
range(1, 6) means start at 1, go up to (but not including) 6
Output:
7 × 1 = 7
7 × 2 = 14
... (continues to 7 ×
10 = 70)
A while loop repeats while a condition is true. Perfect when you don't know how many repetitions you need.
Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Blastoff!
A common use: repeat until user says to stop.
Loop continues until user types "quit"
Control loop flow with special keywords:
Loops inside loops! Useful for creating patterns.
Output: A 3×3 multiplication table
Your programs can now do repetitive tasks
Next Chapter: How AI Actually Works!