break continue pass keywords in python.

 


break continue pass keywords in python.

In this we are going to learn about break, continue and pass keywords in Python. Let's start..!

1} pass :

This a simple keyword in Python is used to show any block as null. In other programming languages we use a curly brackets ( { } ) there to specify any block. but in python we are using indentation instead of curly brackets. So you create any block and keep it empty then that will raise an error. Because in Python you can't specify a empty block. 

[ if don't know what is indentation then click here to learn more about indentation in Python ]

But if any case you have created a block but you want to keep that block empty and how we will achieve this ? 

To achieve this we use a simple keyword that is pass. This pass keyword simply shows that the block in null or empty.

So whenever python interpreter finds a pass keyword then python interpreter will simply skip that block. 


Syntax : pass

We not use this keyword many times; but this keyword we use only when we want keep any block empty.

for ex. Suppose you are taking number from user then checking a condition that this number is even or odd. But you want print a message only when number is odd. If number is even don't want to print anything.

For this simply print a message for user "Enter a number :: " then create a variable named as 'number' and take number as  input from user and store it in variable. Check a condition that number is even or odd if number is even then don't do anything write pass in that block and if number is odd then simply print a message "Odd number !".

[ if don't know how to use if else then click here to learn more about if else in Python ]



# printing a message for user
print("Enter a number :: ")

# taking a input from user
number = int(input())

# checking > number is even or odd
if number % 2 == 0:
    # if number even skip
    # for this using keyword pass
    pass
else:
    # if number is odd
    # then printing a message
    print("Odd number !")

 

If you give input number even this will print anything; But you give input number odd then it will print odd number.


Output :: when input number is even.



Output :: when input number is odd.


 

This is how we use keyword pass to keep block empty.


2} break :

This keyword is actually used in loops to break the loop or to over the execution of loop. Simply the keyword break end the execution of loops. If you write a break keyword in loop the loop will execute till the break keyword . Whenever break keyword comes it will end the execution of loop.

This keyword used by programmers when they want to stop execution of any loop.

Syntax : break

for ex. Suppose you have a counter variable of value 1 ( count = 1) and you are printing this counter and after printing you are incrementing a counter by one. But you want to print the value of counter till 4 . whenever counter becomes 5 you want to stop execution of loop. For this we will use a keyword that is break.

For this create a variable for counter ( count = 1 ). Then simply write a while loop with condition ( count <= 10 ) So this loop can execute till 10. But you want to stop this loop when counter becomes 5. So simply write a if else with condition ( count == 5 ) in while block. If the condition is false ( means counter is not equal to 5 ) then just print the value of counter. And if condition s true ( means counter is equal to 5 ) then write a break keyword in that block.  At last in while block increment the counter.

[ if don't know how to use while loop then click here to learn more about while loop in Python ]


# using break keyword
# counter
count = 1
# checking a condition
while count <= 10:

    if count == 5:
        # if counter becomes 5
        # stop the execution
        # using break keyword
        break
    else
        # if counter not equal to 5
        # then print the counter
        print(count)

    # incrementing a counter
    count = count + 1


   

This will print 1, 2, 3, 4 because whenever counter becomes 5 break keyword will stop the execution.




This is how we use the keyword break in the loop. To stop the execution of loop. 


3} continue :

This is very use full keyword. This keyword simply skips a one entire iteration of loop. It means if you write continue keyword and after continue you write any statements. that statements will not be execute. The whole iteration will be skip.

The keyword continue we used when we want to skip a whole iteration of loop .

Syntax : continue

for ex. Suppose you are print a number 1 to 10 using range() in for loop. But you want to print only odd numbers. If there any even number comes then directly skip or ignore the whole process of even number. And print a number only when number is odd. 

For this simply use a for loop with range(1, 11) and inside for loop block check a condition that number is even or odd. If number is even then skip or ignore the process and directly jump on next number using the special keyword i.e. continue and if the number is odd then only simply print the number. 

[ if don't know what is range() then click here to learn more about about range() in Python ]

[ if don't know how to use for loop then click here to learn more about for loop in Python ]


# using continue keyword
# counter
count = 1

# using for loop with range()
for i in range(1,11):
    # checking number is even or odd
    if i % 2 == 0 :
        # if number is even 
        # skip the iteration or statements 
        continue
        print("using continue keyword ")
    else:
        # if number is odd
        # print a number
        print(i)

    # printing a message to
    # show that the continue 
    # keyword actually skipping the
    # whole iteration.
    print("odd number ")



This will print a number( odd number ) and message that is Odd number till 9 because 9 is last odd number till 10




This is how we use a keyword continue to skip a whole iteration of loop.

This is all about the keywords pass , break , and continue. Try to use this keyword in your own program. 


If you have any doubts in this post then please ask me in comment section below. I will try to solve it.


If you like these post and you have learned something from this then share this post with your friends and family.

Keep learning always.....!!! 😇😇😇




Previous Post  Next Post