Python File handling

Python break continue and pass

Python break, continue and pass are the transfer statement that help to change the flow of looping statement. This allow developer to terminates loop or change the flow based on some conditions.

Python break statement

The break statement terminates the loop containing it. Control of the program flow moves to the statement immediately after the body of the loop.

If break statement is inside a nested loop, break will terminate the inner loop.

Syntax

break
Python break

In this image shows the flow of break statement with for loop and while loop.

Example

In this example search specific value given by user in the list

lst=[10,20,30,40]
s=int( input ( "Enter Number that you want to search in list : " ))
for value in lst:
    if value == s:
        print(s," present in list ");
        break;
else:
    print(s," not present in list ");
#end of for loop
print("End of for");

Output

Enter Number that you want to search in list : 10
10 present in list
End of for

In this output, the value 10 is present in list so that’s why, if condition became True and execute the break statement so that statement terminates loop and go to the immediate next statement after for a loop.

In this example, try to print the list value till not found value greater than 20

lst=[15,17,12,21,12]
i=0
print(lst)
while i < len(lst):
    if lst[i] > 20:
        break
    print(lst[i])
    i=i+1
print("Outside while loop")

Output

[15, 17, 12, 21, 12]
15
17
12
Outside while loop

In this output value 21 is greater than 20 so the condition is True so that break the loop

Python continue statement

The continue statement is used to skip the rest of the code inside a loop for the current iteration only. The loop does not terminate but continues on to the next iteration.

Syntax

continue
python continue

In this image shows the program flow behavior with continue statement inside for loop and while loop

Example

In this example, print all the value of the list except 5

lst=[10,20,5,40]
print(lst)
for value in lst:
    if value == 5:
       continue
    print(value)

Output

[10, 20, 5, 40]
10
20
40

In this output, when the value is 5 then the condition became true that execute the continue statement that moves flow at the top of the loop and start next iteration that skip all statements after the continue statement inside the loop

lst=[15,17,5,21,12]
i=0
print(lst)
while i < len(lst):
    if lst[i] == 5:
        i=i+1
        continue
    print(lst[i])
    i=i+1

Output

[15, 17, 5, 21, 12]
15
17
21
12

Python pass statement

The pass is just an empty statement or null statement that doesn't create any effect on the program execution flow.

The pass statement is useful when we want to create an empty body for loops, function or class.

Syntax

pass

Here pass is a keyword of Python

Example

lst=[10,20,5,40]
print(lst)
for value in lst:
    if value == 5:
       pass
    print(value)

Output

[10, 20, 5, 40]
10
20
5
40

In this output, we observe that pass statement not make any effect on execution flow