Loops Overview
Looping is a type of programming technique that allows a set of instructions or code to be repeated multiple times.
Python supports looping constructs through for
and while
loops.
In the above example, we used a for loop to iterate through the elements of a list. The equivalent while loop is as shown below;
The break statement
The break
statement is used inside a loop to prematurely terminate the loop.
Once a break statement is encountered in either a for loop or a while loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
The break statement is typically used together with a conditional block so that when the condition is met, program execution will break out of the loop.
break with for loops
We can use the break statement inside a for loop to terminate the loop when a condition has been met.
In the above example:
- The for loop iterates through the elements of the list
seq
- We define a condition that if the current element is equal to five, the loop should break.
- When the break statement is encountered the loop simply moves to the next statement just after the loop.
break with while loops
Just like in for loops, we can use the break statement inside of a while loop to prematurely terminate it.
In the above example:
- The
while
loop iterates through the elements of a list of integers. - We defined the condition such that if the current element is even, the break statement is executed causing the loop to terminate.
- The statement just after the loop is executed when the loop terminates.
break with nested loops
A nested loop is when a loop appears inside the body of another loop. When a break statement is encountered in the inner loop, only the inner loop is terminated and the outer loop continues to execute.
In the above example:
- the break statement is inside the inner for loop.
- During each iteration the break statement is encountered when the value of j becomes equal to 'C++'.
- When the inner loop terminates, the next iteration of the outer loop immediately begins.
The continue statement
The break
statement as we have seen makes the loop to terminate before the loop condition fails. The continue
statement, on the other hand, makes the loop execution to skip the current iteration and continue with the next iteration.
Once the continue
statement is encountered, the loop execution ignores the rest of the statements in the current iteration and immediately begins the next iterations.
Just like the break
statement, the continue
statement is also typically placed inside a conditional block so that it can execute only when certain conditions are met.
continue with for loop
We can use continue
statement with for
loops to skip the current loop iteration.
In the above example:
- The for loop iterates through the elements of the list
- We defined the condition so that if the current list element is equal to 20, the
continue
statement is executed. - When the continue statement is encountered, the next iteration begins and the print statement is, therefore, not executed.
continue with while loop
We can also use the continue statement with a while loop.
Multiple continue statements
The continue
statement can be executed more than once in a loop, this is unlike the break
statement which can only be used once to exit a loop.
In the above example, whenever an odd number is encountered, the continue
statement gets executed making the loop to move to the next iteration without executing the print statement.