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.
for loop
seq = ['Python', 'Java', 'C++']
for i in seq:
print(i)
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;
while loop
seq = ['Python', 'Java', 'C++']
i = 0
while i < len(seq):
print(seq[i])
i += 1
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.
break with for loop
seq = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in seq:
#define the condition
if i == 5:
break
print(i)
print('after the loop')
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.
break
with while
loop
seq = [ 1, 3, 5, 7, 9, 0, 2, 4, 6, 8]
i = 0
while i < len(seq):
#define the condition
if seq[i] % 2 == 0:
break
print(seq[i])
i += 1
print('after the loop')
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.
break with nested for loops
for i in range(5):
#the inner loop
for j in ['Python', 'Java', 'C++']:
if j == 'C++':
break
print(j, end = ' ')
print()
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.
continue
with for
loop
seq = [10, 20, 30, 40]
for i in seq:
#the condition
if i == 20:
continue
print(i)
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.
continue
with while
loop
data = ['Python', 'Java', 'C++', 'Ruby', 'PHP']
i = 0
while i < len(data):
#the condition
if data[i] == 'C++':
i += 1
continue
print(data[i])
i+=1
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.
#a for loop to print only the even numbers
for i in range(10):
#the condition
if i % 2 == 1:
continue
print(i)
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.