Introduction
Loop control helps us manage how many times a loop runs. It stops loops from running forever and lets us skip or end loops early.
Jump into concepts and practice - no test required
Loop control helps us manage how many times a loop runs. It stops loops from running forever and lets us skip or end loops early.
break # stops the loop completely continue # skips to the next loop cycle pass # does nothing, placeholder
break stops the whole loop immediately.
continue skips the rest of the current loop step and moves to the next one.
for i in range(5): if i == 3: break print(i)
for i in range(5): if i == 3: continue print(i)
pass does nothing but lets the loop run normally.for i in range(3): pass # placeholder for future code print(i)
This program loops from 1 to 9. It skips even numbers, prints odd numbers, and stops completely when it finds 5.
print('Start loop') for number in range(1, 10): if number == 5: print('Found 5, stopping loop') break if number % 2 == 0: print(f'Skipping even number {number}') continue print(f'Odd number: {number}') print('Loop ended')
Without loop control, loops might run forever or do unwanted work.
Use break and continue carefully to keep code clear and easy to read.
Loop control helps manage how loops run and stop.
break stops loops early, continue skips steps.
They prevent infinite loops and make loops more useful.
break help stop loops early when needed.break to stop loops early.stop, exit, and skip are not valid loop control keywords.for i in range(5):
if i == 3:
break
print(i)i == 3.for i in range(4):
if i = 2:
continue
print(i)continue is valid in loops, and range(4) is correct.