Featured

    Featured Posts

Python Break, Continue Statement With while Loop

The break Statement In Python:
§  break is a statement which is used to break (terminate) the loop execution and program’s control reaches to the next statement written after the loop body.
§  It brings control out of the loop

Take input from user infinite times but when user enter zero or negative value program will be terminated.

#infinite loop
while True:
    n=int(input('enter number'))
    if n<0 or n==0:
        print('Terminating the Loop..')
        break
    print('Number is: ',n)
print('Welcome OutSide the Loop')
Output:
enter number10
Number is:  10
enter number20
Number is:  20
enter number30
Number is:  30
enter number0
Terminating the Loop..
Welcome OutSide the Loop

Write a python program to successively removes items from a list using .pop() until it is empty:
langs_list=['java','python','c','sql','angular']
while True:
    if not langs_list:
        break
    print(langs_list.pop())
Output
angular
sql
c
python
java
Note:-

§  When a becomes empty, not a becomes true, and the break statement exits the loop.


The continue Statement In Python:
§  The continue statement is used to skip the rest of the code inside a loop for the current iteration only.
§  The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.
§  Syntax:





Write a python program to skip all even number from 1 to n.
n=int(input('enter number'))
i=0
while i<=n:
    i+=1
    if i%2==0:
        continue
    print(i,end='')
print('\nwelcome outside loop!.')
Output
enter number10
1357911
welcome outside loop!.

author

Author Name

Author Description!

Get Free Email Updates to your Inbox!

Post a Comment

www.CodeNirvana.in

Powered by Blogger.

About

Site Links

Popular Posts

Translate

Total Pageviews