Loops and Repetition
Learn how to repeat actions using loops. Master `for` and `while` loops to write efficient code.
Learning Objectives:
- β’Understand what loops are and why they matter
- β’Write simple `for` loops
- β’Write simple `while` loops
- β’Count and iterate over ranges
Why Loops?
Imagine you want to print "Hello" 100 times. You could write:
pythonprint("Hello") print("Hello") print("Hello") # ... 97 more times
But that's tedious and error-prone. Loops let you repeat code automatically.
The for Loop
A for loop repeats a block of code a specific number of times:
pythonfor i in range(5): print(i)
This prints:
plaintext0 1 2 3 4
The variable i starts at 0 and increments each time, up to (but not including) 5.
The while Loop
A while loop repeats while a condition is true:
pythoncount = 0 while count < 5: print(count) count = count + 1
This produces the same output as the for loop above. The loop continues as long as count < 5.
Nested Loops
You can put loops inside loops to create patterns:
pythonfor i in range(3): for j in range(2): print(f"i={i}, j={j}")
Output:
plaintexti=0, j=0 i=0, j=1 i=1, j=0 i=1, j=1 i=2, j=0 i=2, j=1
Key Syntax in Different Languages
Python:
pythonfor i in range(5): print(i) while count < 5: count = count + 1
C/C++/Java:
cfor (int i = 0; i < 5; i++) { printf("%d\n", i); } while (count < 5) { count = count + 1; }
JavaScript:
javascriptfor (let i = 0; i < 5; i++) { console.log(i); } while (count < 5) { count = count + 1; }
Practice Exercises
Exercise 2.1 β Count to 10
Objective: Use a loop to print numbers 1 through 10, each on a new line.
Output:
plaintext1 2 3 4 5 6 7 8 9 10
Exercise 2.2 β Multiplication Table
Objective: Print the 5 times table (5Γ1, 5Γ2, ..., 5Γ10).
Output:
plaintext5 10 15 20 25 30 35 40 45 50
Exercise 2.3 β Rectangle Pattern
Objective: Use nested loops to draw a rectangle of * characters.
For a 4Γ3 rectangle (4 wide, 3 tall):
plaintext**** **** ****
Hints
Stuck? Try these hints:
- Free: Nested loops have an outer loop and an inner loop. The inner loop completes fully before the outer loop moves to the next iteration.
- Hint 2 (5 XP): Check your loop bounds β are you starting and ending at the right numbers?
- Hint 3 (10 XP): For the rectangle, the outer loop controls rows, the inner loop controls columns.
Key Takeaways
forloops repeat a fixed number of timeswhileloops repeat while a condition is true- Nested loops let you create 2D patterns and complex repetitions
- Loops are fundamental to solving real programming problems
Progress: 2/8 chapters complete in Level 1 β
Exercises
Exercise 1: Simple Loop
Use a loop to repeat an instruction the requested number of times.
Exercise 2: Loop Counter
Print the numbers from 1 through 10, one per line.
Exercise 3: Nested Loops
Use nested loops to print the multiplication table for numbers 1 to 3.
Exercise 4: Rectangle Pattern
Print a rectangle of stars with 5 columns and 3 rows.
Exercise 5: Triangle Pattern
Print a right triangle of stars with 5 rows.
Exercise 6: Number Pyramid
Print a centered number pyramid with 5 rows.