pass break continue - ibrahimrifats/Back-End-development GitHub Wiki
In Python, break, pass, and continue are control flow statements that are commonly used in loops to alter the flow of execution. Here are examples of how they can be used in Python code along with their corresponding outputs:
Break:
break is used to terminate a loop prematurely when a certain condition is met. It is often used to exit a loop early if some condition is satisfied.
# Example using break
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
break
print(num)
print("Loop ended.")
Output:
1
2
Loop ended.
Pass:
pass is a null operation in Python. It acts as a placeholder when a statement is syntactically required but you want to do nothing.
# Example using pass
for i in range(5):
if i == 3:
pass
else:
print(i)
print("Loop ended.")
Output:
0
1
2
4
Loop ended.
Continue:
continue is used to skip the rest of the loop's body for the current iteration and move on to the next iteration.
# Example using continue
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
continue
print(num)
print("Loop ended.")
Output:
1
2
4
5
Loop ended.
These control flow statements can be useful in various situations, providing flexibility and control over loop execution in Python.