0Pricing
Python Academy · Lesson

while Loops and Loop Control

Use while loops with break, continue, and else.

while Loops and Loop Control is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

while loops repeat as long as a condition is True. break, continue, and else give fine-grained control over loop flow.

Basic while Loop

while condition: runs until condition becomes False. Always ensure the condition eventually becomes False to avoid infinite loops.
x = 0
while x < 5:
    print(x)
    x += 1

Infinite Loop with break

while True: runs forever until break is hit. Common for interactive menus and event loops.
count = 0
while True:
    count += 1
    if count >= 3:
        break
print(count)

continue

continue skips the rest of the current iteration and jumps back to the condition check.
for i in range(10):
    if i % 2 == 0:
        continue
    print(i)

break

break exits the innermost loop immediately. The else clause (if present) does NOT run after a break.
for i in range(10):
    if i == 5:
        break
print('stopped at', i)

while with else

while condition: ... else: runs the else only if the loop exited normally (condition became False, no break).
n = 7
i = 2
while i < n:
    if n % i == 0:
        break
    i += 1
else:
    print(n, 'is prime')

Input Validation Loop

while True: try to get valid input; break on success; else print error. This is the canonical input validation pattern.
# while True:
#     try: x = int(input('Enter a number: ')); break
#     except ValueError: print('Invalid')
print('pattern demo')

Countdown Pattern

while n > 0: n -= 1 is a simple countdown. range() is preferred for loops with known counts.
n = 5
while n > 0:
    print(n)
    n -= 1

Sentinel Value Pattern

Read input until a sentinel (e.g., 'quit') is received. while (x := input()) != 'quit': process(x)
data = ['a', 'b', 'quit', 'c']
for val in data:
    if val == 'quit':
        break
    print(val)

Nested Loop Break

break only exits the innermost loop. To break out of nested loops, use a flag variable or place loops in a function.
found = False
for i in range(3):
    for j in range(3):
        if i == 1 and j == 1:
            found = True
            break
    if found: break
print('found at', i, j)

pass Statement

pass is a no-op placeholder. Use it when a loop body is required syntactically but you have nothing to do yet.
for _ in range(5):
    pass  # placeholder
print('done')

Quick Check

What happens to the else clause of a while loop when break is executed?

Recap

while loops: run until condition is False. break exits early (skips else), continue skips current iteration, pass is a no-op placeholder.

Keep Going

Great work! Move on to the next lesson to continue building your skills.

Frequently asked questions

Is the “while Loops and Loop Control” lesson free?

Yes — the full text of “while Loops and Loop Control” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “while Loops and Loop Control”?

Use while loops with break, continue, and else. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “while Loops and Loop Control” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. if / elif / else Statements
  2. for Loops and range()
  3. while Loops and Loop Control
  4. Nested Loops and Loop Patterns
← Back to Python Academy