Decisions and loops
How programs choose what to do and repeat work: conditions, for and while loops, and loops inside loops.
About 7 hours of study
Learning objectives
- Choose between paths with if, elif and else
- Repeat code with for and while
- Combine loops to work with rows and columns
Lessons
- 1
Decisions with if
Compare values and let the program choose a path with if, elif and else.
- 2
Repeating with for loops
Run the same lines many times with for and range(), and watch the loop variable change.
- 3
Repeating with while
Repeat while a condition holds, stop early with break, and avoid loops that never end.
- 4
Nested loops
Put a loop inside a loop to build grids, combine choices, and see how break affects only the inner loop.
Module practice
- Even or oddEasy
- Largest of threeEasy
- Letter gradeMedium
- Leap yearMedium
- What kind of triangle?Hard
- Sum from 1 to nEasy
- Multiplication tableEasy
- Count the even numbersMedium
- Star triangleMedium
- Multiples of 3 or 5Hard
- Sum of digitsEasy
- Reverse a numberMedium
- Decimal to binaryHard
- Multiplication gridEasy
- A hollow squareMedium
- All the primes up to nHard