Python Programming - Python Control Statements

Python Programming – Python Control Statements

The control statements are used to alter the normal sequence of a loop. Python language provides three types of control statements given as in Table 4.3.

Statement

Description

BreakIt terminates the current execution of the loop and transfers the control to the statement instantly following the block of the loop.
ContinueIt immediately transfers the control to the beginning of the loop by skipping the rest of the statements following it.
passIt is used when a statement is required syntactically rather than executing any command or code.

Python break Statement

Alike the traditional break statement in C, the role and use of the break statement are similar in Python language. The break statement is used to break the loop immediately when a certain condition evaluates to true. It can be used in both for and while loop. In case, nested loops are used in a program, and if break encounters in the inner loop then, it terminates the inner loop, and execution control goes to the outer loop. On the other hand, if the break statement occurs in a single loop then it transfers the execution control to the statement immediately after the loop. The syntax of the break statement is given as under and the flow diagram is displayed in Fig. 4.6.

break

Python Programming - Python Control Statements chapter 4 img 1

The programming example to illustrate the use of break statements in Python is given in Code 4.17. It is shown that the range for the loop is set to 0 through 9. However, when if statement encounters variable i with value 5 then the break statement gets executed and transfers the control out of the loop. So, it prints 0, 1, 2, 3, 4 only, and eventually “exit for” gets printed. A similar programming example is given to represent the use of the break statement in the while loop in Code 4.18. Another programming use of break statements has been already discussed in Code 4.15.

Code: 4.17. Program to illustrate break statement in for loop.

# This program represents the significance of break statement

for i in range(0, 9):
if (i==5):
break
print(i)
print(‘exit for’)

Output:
0
1
2
3
4
exit for

Code: 4.18. Program to illustrate break statement in while loop.

# This program represents the use of break statement in while loop

count=1
while(count<= 10):
if(count == 5):
break
print(count)
count=count+1
print(‘exit while’)

Output:
0
1
2
3
4
exit while

Python continue Statement

The continue statement returns the control to the beginning of the loop. It overpasses (skips) all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. It can be used with both whiles as well as for loop. The syntax of the continue statement is given as under and the flow diagram is displayed in Fig. 4.7.

continue

Python Programming - Python Contro

The programming example of the continue control statement is given in Code: 4.19. This program is the same as the above example except the break statement has been replaced with continue. The program continues with the loop, if the value of i=5. We see that the execution control moves back to the beginning of the loop when i=5 triggers and skips rest of the statements. Thus, we see in the output that all the digits up to 9 get printed except 5.

Code: 4.19. Program to illustrate the use of continue statement.

# This program represents the significance of continue statement

for i in range(0, 9):
if (i==5):
continue
print(i)
print(‘exit for’)

Output:
0
1
2
3
6
7
9
exit for

Python pass Statement

Unlike C, C++, Java, Python language provides a new statement pass. The pass statement principally represents a null operation, which means nothing happens when it executes. It can be used when a certain statement is required only syntactically but the programmer does not wish to execute any code or command.

The pass is valuable in situations when the programmer wishes to write certain code at a later stage within the loop to perform some action. The syntax of the pass statement is given as under

pass

The programming example of the pass statement is given in Code: 4.20. In this program, a pass statement is used inside for loop. The program prints all the digits from 0 to 9. However, when the i=5 condition becomes true then the pass statement gets executed and it prints This is a pass statement. The difference between Python comment and pass is that the comments are entirely ignored by the interpreter but the pass is not ignored.

Note:

The difference between Python comment and pass is that the comments are entirely ignored by the interpreter but the pass is not ignored.

Code: 4,20. Program to illustrate the use of pass statement.

# This program represents the significance of pass statement
for i in range(0, 9):
if(i==5):
pass
print(‘This is a pass statement’)
print(i)
print(‘Exit for’)
Output
0
1
2
3
4
This is a pass statement
5
6
7
8
9
Exit for

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *