Break, Continue, pass, else Statements in Python

 

Break, Continue, pass, else Statements in Python
 

The break Statement in Python:

The break statement is used in for loop and while loop in Python. This statement is used to terminate the loop immediately. It is used to control the loop. When this statement is used the remaining code in the loop is not executed.

The break keyword is used to break the loop.

For example,

list1=[1,2,4,7,9,0,10]

for i in list1:

    if i==0:

        break

    print(i)

Output:

1

2

4

7

9

In above program the loop is used to access the list elements. In loop break statement is used to check the list element exactly equals to 0. Once the compiler find this value due to the break statement the loop will terminate and other values after 0 will not be executed and displayed.

 

The continue Statement:

The continue statement is used to skip a particular iteration in loop. This statement helps to stop the current iteration and proceeds to the next iteration in loop.

For Example,

list1=[20,10,3,6,9,8]

for i in list1:

    if i==3:

        continue

    print(i)

Output:

20

10

6

9

8

In above example for loop is applied on list and one test condition is written to check the number is exactly equals to 3. So when compiler will find this number due to the continue statement compiler will skip this iteration in loop and will continue the next remaining iterations. So final output is list elements excluding 3 all elements displayed on screen.


Pass Statement:

The pass statement in Python is null statement it means it is used to do nothing. To create loop or function empty.

For example,

def sample():

          pass

Compiler will allow you to write empty function with the help of pass statement. We can also use pass statement in loop statements or conditional statements to not do anything in that block.

For example

# Pass statement

n=int(input("Enter any number"))

if n>=20:

    pass

else:

    print(f"{n} is smaller than 20")

 

Output:

Enter any number12

12 is smaller than 20

In above program pass statement is used in if statement. So this statement will not do anything if n is greater than or equals to 20. Otherwise it will print else block statement.

# Pass statement

n=int(input("Enter any number"))

if n>=20:

    pass

else:

    print(f"{n} is smaller than 20")

Output:

Enter any number20

​So here user input for n is 20 so condition satisfy and compiler will not do any thing.


The else Statement:

The else statement is used in loops as well as conditional statements. When else is used with loop, this block is executed only when if loop terminated normally i.e. without break statement.

For example,

# Use of else

for i in range(5):

    print(i)

else:

    print("End of loop")

Output:

0

1

2

3

4

End of loop

In above program for loop is used to print 5 numbers. In for loop else is used. So compiler will execute the else block statement after executing loop statement.

# Use of else

for i in range(5):

    if i==4:

        break

    print(i)

else:

    print("End of loop")

Output:

0

1

2

3

Here else block is not executed because loop is not terminated normally.



टिप्पणी पोस्ट करा

0 टिप्पण्या