prev
next
Or press ESC to close.
To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content. To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content.

Python Programming Part 10

Rajjit Oct 09th, 2023 5 mins read
part-10

TOPIC: Python break, continue & pass statements

break statement
The break statement is a control flow statement used to exit or terminate a loop prematurely when a certain condition is met. It allows you to break out of the loop before it completes all its iterations. break is often used when you want to exit a loop as soon as a specific condition is satisfied, avoiding unnecessary iterations. It's commonly used in loops like while and for to implement conditional exit based on certain criteria.

Example

num = 10
while True:
    if num % 2 == 0 and num > 10:
        print(f"The first positive even number greater than 10 is {num}.")
        break
    num += 1

The break statement provides a way to control the flow of your loops and exit them when specific conditions are satisfied, making your code more efficient and responsive to certain situations.

continue statement
The continue statement is a control flow statement used to skip the current iteration of a loop and continue with the next iteration. It allows you to bypass specific iterations based on a condition without exiting the entire loop. continue is often used when you want to avoid executing certain code for a particular iteration of a loop but continue with the remaining iterations. It's commonly used in loops like for and while to implement conditional skipping of iterations.

Example

for num in range(1, 11):
    if num % 3 == 0:
        continue
    print(num)

The continue statement provides a way to selectively skip specific iterations within a loop while allowing the loop to continue with the next iteration. It is particularly useful for cases where you want to avoid certain processing steps under certain conditions.

pass statement
The pass statement is a null operation or a placeholder used when you need a syntactically correct statement but don't want any code to be executed. It serves as a way to indicate that something will be implemented in the future.pass is often used as a temporary placeholder when defining empty functions, classes, or code blocks that will be filled in later. It is useful in situations where you want to create a valid structure but delay the actual implementation.

Example


def my_function():
    pass  # Placeholder for future code

# You can define more functions or add code later

The pass statement allows you to create a valid and syntactically correct structure in your code while deferring the actual implementation. It is useful for situations where you want to outline the structure of your program before filling in the details.

Code as discussed in the video:

# break statement - example
# i = 1
# while i <= 10:
#     print(i, end=" ")
#     if i == 5:
#         break
#     i = i + 1
# print("\nThe loop is done")

# continue statement - example
# for i in range(1, 11):
#     if(i == 5):
#         continue
#     print(i, end = " ")
# print("\nThe loop is done")

# pass statement - example
for letter in 'HELLO':
    pass
    # print(letter, end = " ")
print("\nThe loop is done")

You can copy and run this code