Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Python Loop Control: break, continue, and pass Statements Explained with Practical Examples

Loops are the backbone of most programming languages, enabling repetition over sequences such as lists, tuples, strings, dictionaries, and sets. In Python, two looping constructs—for and while—serve this purpose. The loop continues until its condition becomes false, but the flow can be altered using the break, continue, and pass statements.

What are Loop Control Statements?

These statements give developers fine‑grained control over loop execution, making code more efficient and readable.

break Statement

The break statement terminates the innermost loop in which it is executed. In nested loops, only the current loop is affected, allowing selective exit while keeping outer loops running.

How break Works (Flowchart)

Python Loop Control: break, continue, and pass Statements Explained with Practical Examples

The process is straightforward:

  1. Enter the loop.
  2. Evaluate the loop condition.
  3. If break is encountered, exit the loop.
  4. Otherwise, execute the loop body.
  5. Re‑evaluate the condition and repeat until it fails.

Examples

break in a for‑loop

my_list = ['Siya', 'Tiya', 'Guru', 'Daksh', 'Riya', 'Guru']

for name in my_list:
    print(name)
    if name == 'Guru':
        print('Found the name Guru')
        break

print('Loop is terminated')

Output:

Siya
Tiya
Guru
Found the name Guru
Loop is terminated

break in a while‑loop

my_list = ['Siya', 'Tiya', 'Guru', 'Daksh', 'Riya', 'Guru']
index = 0

while True:
    print(my_list[index])
    if my_list[index] == 'Guru':
        print('Found the name Guru')
        break
    index += 1

print('After while-loop exit')

Output:

Siya
Tiya
Guru
Found name Guru
After while-loop exit

break in nested loops

for i in range(4):
    for j in range(4):
        if j == 2:
            break
        print("The number is", i, j)

Output:

The number is 0 0
The number is 0 1
The number is 1 0
The number is 1 1
The number is 2 0
The number is 2 1
The number is 3 0
The number is 3 1

continue Statement

The continue keyword skips the remaining code in the current iteration and proceeds directly to the next loop cycle.

How continue Works (Flowchart)

Python Loop Control: break, continue, and pass Statements Explained with Practical Examples

Examples

continue in a for‑loop

for i in range(10):
    if i == 7:
        continue
    print("The Number is:", i)

Output:

The Number is: 0
The Number is: 1
The Number is: 2
The Number is: 3
The Number is: 4
The Number is: 5
The Number is: 6
The Number is: 8
The Number is: 9

continue in a while‑loop

i = 0
while i <= 10:
    if i == 7:
        i += 1
        continue
    print("The Number is:", i)
    i += 1

Output:

The Number is: 0
The Number is: 1
The Number is: 2
The Number is: 3
The Number is: 4
The Number is: 5
The Number is: 6
The Number is: 8
The Number is: 9
The Number is: 10

continue in nested loops

for i in range(4):
    for j in range(4):
        if j == 2:
            continue
        print("The number is", i, j)

Output:

The number is 0 0
The number is 0 1
The number is 0 3
The number is 1 0
The number is 1 1
The number is 1 3
The number is 2 0
The number is 2 1
The number is 2 3
The number is 3 0
The number is 3 1
The number is 3 3

pass Statement

The pass keyword is a no‑op placeholder used when a statement is syntactically required but no action is desired. It is commonly employed in empty function bodies, classes, loops, or conditional blocks that will be implemented later.

When to Use pass

Examples

pass in a function

def my_func():
    print('pass inside function')
    pass

my_func()

Output:

pass inside function

pass in a class

class My_Class:
    print("Inside My_Class")
    pass

Output:

Inside My_Class

pass in a loop

# Pass statement in for-loop
text = "Guru"
for char in text:
    if char == 'r':
        print('Pass executed')
        pass
    print(char)

Output:

G
u
Pass executed
r
u

pass in an if‑statement

a = 1
if a == 1:
    print('pass executed')
    pass

Output:

pass executed

Choosing between break and continue

Understanding the subtle difference ensures that loops run efficiently and behave exactly as intended.

Takeaway

Python

  1. Mastering Python Loop Control: break & continue
  2. Python Print() Function: A Practical Guide with Examples
  3. Master Python Loops: For, While, Break, Continue, and Enumerate Explained
  4. Master Python’s str.count(): How to Count Characters & Substrings with Examples
  5. Master Python `format()` Strings with Clear Examples
  6. Python round() Function Explained with Practical Examples
  7. Mastering Python's map() Function: Syntax, Examples, and Best Practices
  8. Python timeit() – Measuring Execution Time with Practical Examples
  9. Python list.count(): Expert Guide with Practical Examples
  10. Python Module Importing – A Practical Guide with Examples