Python while Loop

while Loop

While: when something is true, Keep doing it, until it getting false.

 

This code will never end. it is a infinite loop

 
 ## while loops

num = 1
while num <4:
print("Hello world")
    
    output:
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    ............ it will never end. it is an infinite loop.
    

 

num = 1 is less than 4 and it always less than 4 that's why it run again and again.



it will run 3 times because num increment with num +=1

 
    num = 1
while num <4:
print("Hello world")
num += 1
    
    output:
    Hello world
    Hello world
    Hello world



Post a Comment (0)
Previous Post Next Post