There are two types of loops you can use in Python: for loops and while loops. You can use them for repetitive tasks. As a result, repetitive tasks will happen automatically, making the process more efficient. Unfortunately, you may run into some problems with your loops. Sometimes your program may run into problems that require it to skip a part of the loop or exit the loop altogether. Or you may need it to ignore external factors that affect the program. If this is something to add to your program, you will need to use break, continue, and pass statements.
break
The break statement is responsible for terminating the loop that uses it. If the break statement is used in a nested loop, the current loop will terminate and the stream will continue to execute the code that follows the loop.
Flowchart of the break statement.
Steps involved in the flowchart.
- Step 1) Loop execution starts.
- Step 2) If the loop condition is true, it will execute step 2, where the body of the loop will be executed.
- Step 3) If the body of the loop has a break statement, the loop will exit and go to step 6.
- Step 4) After the loop condition is executed and completed, it will go to the next iteration in step 4.
- Step 5) If the loop condition is false, it will exit the loop and go to step 6.
- Step 6) The loop ends.
while loop
Output.
When a break is encountered, the while loop is jumped out directly.
for loop
Output content.
When a break is encountered, the for loop is jumped out directly.
Multi-level for loops
Output.
Break will only jump out one layer.
continue
When the continue statement is executed in the loop structure, it does not exit the loop structure, but immediately ends the current loop and starts the next loop again, that is, it skips all statements in the loop body after the continue statement and continues the next loop.
Flowchart of the continue statement.
Steps involved in the flowchart.
- Step 1) Loop execution begins.
- Step 2) Execution of the code within the loop will complete. If there is a continue statement in the loop, the control will return to step 4, the start of the next iteration of the loop.
- Step 3) Execution of the code within the loop will complete.
- Step 4) If there is a continue statement, or if the execution of the loop within the body completes, it will call the next iteration.
- Step 5) When the loop execution is complete, the loop will exit and go to step 7.
- Step 6) If the loop condition in step 1 fails, it will exit the loop and go to step 7.
- Step 7) The loop ends.
while loop
Output.
for loop
Output.
Multi-level for loops
Output.
pass
The Python pass statement is used as a placeholder in loops, functions, classes, and if statements, and does nothing; it is a null operation.
Usage scenario: Suppose you have a function or an empty class. You plan to write code in the future. If the Python interpreter encounters an empty object, it will throw an error. pass statements can be used inside function bodies or class bodies. During execution, when the interpreter encounters a pass statement, it ignores it and continues execution without giving any errors.