Monday, 27 May 2019

while loop

#Display numbers from 1 to 10
i=1
while i<=5:
    print(i)
    i+=1
"""output:
1
2
3
4
5
"""
while is a keyword, while suite are executed until while condition becomes false.

While loop is useful to execute a group of statements several times until condition becomes false.

Syntax:
while condition:
     Statements

Here statements represents one statement or a suite of statements.



Related Video: https://www.youtube.com/watch?v=hFcv96-p4OE&feature=youtu.be


Difference between while and for loop:


Repeated code can be executed for every element in a sequence in for loop. Where as in while loop: Repeated code can be executed as long as condition is True.
#write a program to prompt user to enter some name until enter Python
a=""
while a!="Python":
    a=input("enter name:")
print("Thanks for confirmation")
"""output:
enter name:python
enter name:Python
Thanks for confirmation
>>> """
Next Topic: Nested loops
Previous Topic: for loop

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...