Module 14: While Loops

Repeat actions based on conditions. Let's practice!

Anatomy of a `while` Loop

A `while` loop repeats code as long as a condition is true. Click the parts below to learn about them.

count = 0 # Initialize a counter
while count < 3: print(count)
count = count + 1 # IMPORTANT: Update the counter!

Identify all 4 parts!

Complete the `while` Loop

Fill in the blanks below to create a `while` loop that prints numbers from 1 up to (and including) 4.

num = 1
num :
print(num)
num = num 1

Simulate a `while` Loop

Write a `while` loop that prints "Looping..." exactly 3 times. Remember to initialize a counter and increment it inside the loop!

Output will appear here...

Trace the Loop

Read the code below carefully. What will be the final value printed? Type your prediction and check your answer.

x = 10
while x > 5:
print(x) x = x - 2 # Loop finishes, then this line runs
print("Final x:", x)

`while` Loop Missions!

Apply your `while` loop knowledge to solve these challenges. Remember the condition and the update step!

Mission 1: Count Down

Write a `while` loop to print numbers counting down from 5 to 1 (inclusive). Output should be: 5, 4, 3, 2, 1.

Mission 2: Summing Evens

Initialize `total = 0` and `i = 2`. Use a `while` loop to add all even numbers from 2 up to 10 (inclusive) to `total`. Finally, print the `total`.

Module 14 Practice Complete!

Excellent work with `while` loops! You're getting the hang of conditional repetition.
Ready for more? Try the Advanced Practice for Module 14!