In this tutorial you will learn about the Python Loop Control Statements and its application with practical example.
Python Loop Control Statements
In Python, we have loop control statements that can be used to alter or control the flow of loop execution based on specified conditions. Python loop statements gives you a way execute the block of code repeatedly. But sometimes, you may want to exit a loop completely or skip specific part of a loop when it meets a specified condition. It can be done using loop control mechanism.
Types of Loop Control Statements
In Python we have following three types of loop control statements :
- Break Statement
- Continue Statement
- Pass Statement
Python break statement
In Python, break statement inside any loop gives you way to break or terminate the execution of loop containing it, and transfers the execution to the next statement following the loop.
Syntax:-
1 |
break |
Example:-
1 2 3 4 5 6 7 8 9 10 |
count = 0 while count < 10: count += 1 if count == 5: break print("inside loop", count) print("out of while loop") |
In this above program, the variable count is initialized as 0. Then a while loop is executed as long as the variable count is less than 10.
Inside the while loop, the count variable is incremented by 1 with each iteration (count = count + 1).
Next, we have an if statement that checks the variable count is equal to 5, if it return TRUE causes loop to break or terminate.
Within the loop there is a print() statement that will execute with each iteration of the while loop until the loop breaks.
Then, there is a final print() statement outside of the while loop.
When we run this code, our output will be as follows –
Output:-
1 2 3 4 5 |
inside loop 1 inside loop 2 inside loop 3 inside loop 4 out of while loop |
Python continue statement
The continue statement gives you way to skip over the current iteration of any loop. When a continue statement is encountered in the loop, the python interpreter ignores rest of statements in the loop body for current iteration and returns the program execution to the very first statement in th loop body. It does not terminates the loop rather continues with the next iteration.
Syntax:-
1 |
continue |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
ctr = 0 for ctr in range(10): ctr = ctr + 1 if ctr == 5: print("5 is skipped") continue # continue here print ("This won't be printed too.") print('Number is ' + str(ctr)) print('Out of loop') |
Output:-
1 2 3 4 5 6 7 8 9 10 11 |
Number is 1 Number is 2 Number is 3 Number is 4 5 is skipped Number is 6 Number is 7 Number is 8 Number is 9 Number is 10 Out of loop |
As you can see when ctr == 5, continue statement is executed which causes the current iteration to end and the control moves on to the next iteration.
Python pass statement
In Python , the pass statement is considered as no operation statement, means it consumes the execution cycle like a valid python statement but nothing happens actually when pass is executed.The pass statement is much like a comment, but the python interpreter executes the pass statements like a valid python statements, while it ignores the comment statement completely. It is generally used to indicate “null” or unimplemented functions and loops body.
Syntax:-
1 |
pass |
Example:-
1 2 3 4 5 6 7 8 9 10 |
numbers = [ 1, 2, 4, 3, 6, 5, 7, 10, 9 ] #loop through the numbers for number in numbers: #check if the number is even if number % 2 == 0: #if even, then pass ( No operation ) pass else: #print the odd numbers print 'Current number is:', number |
Output:-
1 2 3 4 5 |
Current number is: 1 Current number is: 3 Current number is: 5 Current number is: 7 Current number is: 9 |