PYTHON LESSON 11 OF 32

While, break & continue

while repeats as long as a condition remains true. Update the condition so the loop can end. break exits the loop; continue skips to the next iteration. An infinite loop can freeze a program.

हिन्दी में समझें

while शर्त सही रहने तक चलता है। मान बदलें ताकि लूप रुके। break लूप रोकता है; continue अगले चक्कर पर जाता है। अनंत लूप प्रोग्राम अटका सकता है।

अभ्यास: while लूप से 1 से 5 तक प्रिंट करें।

Hinglish mein samjhein

while condition true rehne tak repeat karta hai. Value update karo warna infinite loop ho sakta hai. break loop rokta hai, continue next round par jata hai.

Practice: while loop 1 se 5 tak print kare.

A small Python example

count = 1
while count <= 3:
    print(count)
    count += 1

The browser playground runs real Python with Pyodide. Use sample variables instead of interactive input(). Browser package support differs from desktop Python.

Try this example in the Python playground →

Practise this idea

Make the while loop print 1 through 5.

Check answer & explanation
count = 1
while count <= 5:
    print(count)
    count += 1

The <= 5 condition includes 5. Incrementing count in the loop eventually makes the condition false at 6.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

What ends this loop?

  1. count becomes 4
  2. count becomes 3
  3. print ends it
Check the quiz answer

count becomes 4

At 4, count <= 3 is false.

References & corrections

Use the official Python documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.