Python break, continue and pass Statements with example

As you learn programming, there might be times when you want to stop a loop completely when something specific happens, or you want to skip a step in the loop and go to the next one. Python has some useful tools called “break,” “continue,” and “pass” statements that help you with these situations.

They give you more control over how your loops work and make your code more flexible.

The break Statement in Python

In Python, the break statement allows you to exit a loop prematurely. When the break statement is encountered within a loop, the loop is immediately terminated, and the program execution continues with the next statement after the loop.

Example 1

fruits = ["apple", "banana", "cherry", "date", "elderberry"]

for fruit in fruits:
    if fruit == "cherry":
        break
    print(fruit)

print("Loop ended.")

Output:

In this example, we have a list of fruits. The for loop iterates over each fruit. However, when it encounters the fruit “cherry”, the break statement is executed, causing the loop to exit. As a result, only “apple” and “banana” are printed. The program then continues with the next statement after the loop, which is print(“Loop ended.”).

apple
banana
Loop ended.

Example 2

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers:
    if number % 3 == 0:
        break
    print(number)

print("Loop ended.")

Output:

In this example, we have a list of numbers from 1 to 10. The for loop iterates over each number. When a number that is divisible by 3 is encountered (in this case, 3 itself), the break statement is executed, causing the loop to exit. As a result, only numbers 1 and 2 are printed. The program then continues with the next statement after the loop, which is print(“Loop ended.”).

1
2
Loop ended.

The continue Statement in Python

In Python, the continue statement is used within loops to skip the remaining code within the current iteration and move on to the next iteration. When the continue statement is encountered, it jumps back to the beginning of the loop and starts the next iteration without executing the remaining code below it.

Example 1

numbers = [1, 2, 3, 4, 5]

for number in numbers:
    if number == 3:
        continue
    print(number)

print("Loop ended.")

Output:

Let’s say we have a list of numbers: numbers = [1, 2, 3, 4, 5]. We want to print the numbers in the list, but we want to skip the number 3. We can do this using a for loop and the continue statement.

The for loop will iterate over each number in the list. When it encounters the number 3, the continue statement will be executed. This will skip the code inside the loop after the continue statement, which is the print(number) line. The loop will then immediately move on to the next iteration.

In this case, the continue statement will skip the printing of the number 3. The remaining numbers, 4 and 5, will be printed. The program then continues with the next statement after the loop, which is print(“Loop ended.”).

1
2
4
5
Loop ended.

Example 2

string = "shitus is a Blog"

for char in string:
    if char == "i":
        continue
    print(char)

print("Loop ended.")

Output:

In this example, we have a string “shitus is a Blog”. The for loop iterates over each character in the string. When the letter “i” is encountered, the continue statement is executed, skipping the remaining code within the loop for that character.

As a result, the letter “i” is not printed. The loop continues with the next iteration and prints all other characters in the string. The program then continues with the next statement after the loop, which is print(“Loop ended.”).

s
h
t
u
s
 
s
 
a
 
B
l
o
g
Loop ended.

The pass Statements in Python

The pass statement in Python is used as a placeholder when you need to have a statement syntactically but don’t want it to do anything. It acts as a no-op, indicating that there should be no action taken at that point in the code.

Example 1

numbers = [1, 2, 3, 4, 5]

for num in numbers:
    if num % 2 == 0:
        pass
    else:
        print(num)

print("Loop ended.")

Output:

In this example, we have a list of numbers. The for loop iterates over each number. When an even number is encountered (a number that is divisible by 2), the if condition is true, and the pass statement is executed.

As a result, no action is taken for even numbers, and the loop moves on to the next iteration. However, for odd numbers, the else block is executed, and the number is printed.

1
3
5
Loop ended.

Example 2

text = "shitus is a Blog"

for char in text:
    if char == "i":
        pass
    else:
        print(char)

print("Loop ended.")

Output:

In this example, we have a string “shitus is a Blog”. The for loop iterates over each character in the string. When the letter “i” is encountered, the if condition is true, and the pass statement is executed.

As a result, no action is taken for the letter “i”, and the loop moves on to the next character. For all other characters, the else block is executed, and the character is printed.

s
h
t
u
s
 
s
 
a
 
B
l
o
g
Loop ended.

Leave a Comment