While Loop0%

While Loop

Beginner8 min readUpdated: 2026-09-03
Study Materials

While Loop

A while loop repeatedly executes a block of code as long as its condition remains True.


Key Concepts & Detailed Explanation

undefined


Code Examples & Output

Python
count = 1
 
while count <= 5:
print(f"Iteration #{count}")
count += 1 # Crucial: update counter!
 
print("Loop finished!")

Expected Output:

Output
Iteration #1
Iteration #2
Iteration #3
Iteration #4
Iteration #5
Loop finished!

Best Practices & Common Pitfalls

Always ensure the while loop's condition will eventually evaluate to False, or the program will freeze in an infinite loop.


Practice Quiz

1. What happens if a while loop's condition never becomes False?

  • A) The computer shuts down
  • B) It runs indefinitely (infinite loop)
  • C) Python raises InfiniteError
  • D) It runs 1000 times then stops
Answer: B Explanation: It creates an infinite loop that continues until terminated by the OS (Ctrl+C).

2. Which statement increments a counter 'i' by 1 inside a while loop?

  • A) i++
  • B) i += 1
  • C) increment(i)
  • D) i =+ 1
Answer: B Explanation: Python does not have 'i++'; use 'i += 1'.

Practice Challenge

Write a while loop that prints all even numbers between 2 and 20.

Next Lesson

For Loop

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Project: Grading SystemFor Loop

Practice Quiz

Test your understanding of this lesson with 1 questions. Each question has one correct answer.

PrevNext