Loop Control Statements

Carson West

For Loop Examples

Loop Control Statements

Loop control statements in Python alter the flow of execution within loops (primarily for and while loops). They allow you to skip iterations, terminate the loop prematurely, or jump to a specific point within the loop.

The key loop control statements are:

for i in range(1, 11):
    if i == 5:
        break  # Exit loop when i is 5
    print(i) 
for i in range(1, 11):
    if i % 2 == 0:
        continue  # Skip even numbers
    print(i)
for i in range(1, 11):
    if i % 2 == 0:
        pass  # Do nothing for even numbers
    else:
        print(i)
for i in range(1, 11):
    if i == 15: #this condition will never be met
        break
    print(i)
else:
    print("Loop completed normally") #This will always print

for i in range(1, 11):
    if i == 5:
        break
    print(i)
else:
    print("Loop completed normally") #This will not print

Nested Loops
Break and Continue Statements Detailed For Loop Syntax While Loop Syntax