< previous | Table of Contents | next > |
5.3 ALTERING THE CONTROL FLOW OF PROGRAMS
Control refers to the order in which Python evaluates and executes programs. break
and continue
statements offer ways to alter program control flow.
BREAK STATEMENT
The break
statement is used to exit from a loop intentionally but prematurely. It provides a way to terminate the while
or for
loop when a specific condition is met.
The break
statement is usually placed within an if
block. Once the program counters the break
statement, the program exits that loop. This means that any code within the loop that followed will also not be executed once a break
statement has previously been met. Subsequently, the program continues evaluating and executing the lines of code following the loop.
In the case of nested loops, meaning a loop within another loop, once we've hit the break
statement within the inner loop, only that loop is exited, and then execution continues from the outer loop instead.
In this example, if there were no break
within the if
block, the program would continue running indefinitely. However, the break
statement can also come in handy when dealing with finite loops.
To illustrate here's a flowchart of the break
statement. Notice how the loop is exited once a break statement is triggered within the loop. As you can see below, once a break statement is encountered within the body of the loop, the loop is immediately terminated and no subsequent iterations triggered. The program then continues execution from the statement immediately after the loop. As long as the break statement is not reached, the loop will continue executing as usual. NB! Note that in the flow chart by True
and False
for the break
statement we mean whether it was triggered or not. If the program encounters a break
statement it will always be triggered.
Flowchart. Break statement flowchart. geeksforgeeks.org
CONTINUE STATEMENT
Similarly to the break
statement, the continue
statement stops the execution of the current iteration and does not continue to execute the rest of the contents within the code block. However, instead of exiting the loop, the continue
statement the execution of the current iteration and starts the next iteration instead.
In this example, we've slightly altered the logic of summing all the individual numbers up until the entered number. If the value of i
is not equal to sum_until
, the program stops the execution of the current iteration and skips to the next iteration of the loop. This is repeated until i
equals the input value, during which the program exits the loop entirely thanks to the break
statement.
Here's a flowchart to give you a visual overview of the continue
statement. When the program encounters a continue
statement, the iteration is immediately stopped, and the control is passed to the beginning of the loop, where the subsequent condition is checked or a new iteration started. This means that no statements after the continue
statement will be executed during the iteration upon which the continue
statement was encountered. Notice how, in this case, the loop is not prematurely terminated, as was the case with the break
statement. If the program does not encounter a continue statement, it will continue executing the loop's body. NB! Note that in the flow chart by True
and False
for the continue
statement we mean whether it was triggered or not. If the program encounters a continue
statement it will always be triggered.
Flowchart. Continue statement flowchart. geeksforgeeks.org
KEY THINGS TO REMEMBER
- The
break
statement is used to exit a loop prematurely when a specific condition is met. - The loop terminates immediately when a
break
statement is encountered inside a loop (usually within an if statement). - The
continue
statement is used to skip the remainder of the loop's current iteration and proceed to the next iteration. - When the
continue
statement is executed, the current iteration of the loop ends immediately, disregarding any other code within the block that would've followed. The loop then starts again from the beginning.
SELF-CONTROL EXERCISES
< previous | Table of Contents | next > |