# While Loop

Computers are great at doing boring tasks without complaining. Programmers aren’t, but they are good at getting computers to do repetitive work for them—by using loops. A loop runs the same block of code over and over again. There are several different types of loop.

print(0)
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
1
2
3
4
5
6
7
8
9
10

using while loop

i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

# Using While Loop

  • while loop always do the job when condition is True and always stop when condition is False
i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4
Debugging process
 




i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

Currently, i = 0



 



i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=0, i<10 => while(True)




 


i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=0, print(i) => print(0)





 

i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=0, i=i+1 => i=0+1 => i=1


keep going and repeat while loop


 



i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=1, i<10 => while(True)




 


i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=1, print(i) => print(1)





 

i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=1, i=i+1 => i=1+1 => i=2


keep going and repeat while loop


 



i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=2, i<10 => while(True)




 


i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=2, print(i) => print(2)





 

i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=2, i=i+1 => i=2+1 => i=3


keep going and repeat while loop... skip to i=9


 



i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=9, i<10 => while(True)




 


i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=9, print(i) => print(9)





 

i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=9, i=i+1 => i=9+1 => i=10


keep going and repeat while loop...


 



i = 0
while( i<10 ):
	print(i)
	i = i+1
1
2
3
4

i=10, i<10 => while(False)


output

OUTPUT

0 1 2 3 4 5 6 7 8 9

i = 0
while( i<10 ):
	print(i)
	i = i+2
1
2
3
4
output

OUTPUT

0 2 4 6 8

i = 1
while( i<10 ):
	print(i)
	i = i*2
1
2
3
4
output

OUTPUT

1 2 4 8

# Break and Continue

  • If break is activated, the loop would be stopped.
  • If continue is activated, the loop would be skipped. The current state will jump to the next state.
i=0
while( i<10 ):
	if(i > 5):
		break
	print(i) 	
	i=i+1
1
2
3
4
5
6

OUTPUT

0 1 2 3 4 5

i=0
while( i<10 ):
    i+=1
    if(i < 5):
        continue
    print(i)
1
2
3
4
5
6

OUTPUT

5 6 7 8 9 10

# Let's start coding together

  • Note command

OUTPUT

Hello! add note or show note: today my plan is ... add note or show note: and next ... add note or show note: show ['today my plan is ...', 'and next ...'] add note or show note: sleep add note or show note: show ['today my plan is ...', 'and next ...', 'sleep'] add note or show note: exit

Solution
print("Hello!")
data=[]
while(True):
    line=input("add note or show note: ")
    if(line=="show"):
        print(data)
    else:
        if(line=="exit"):
            break
        else:
            data.append(line)
1
2
3
4
5
6
7
8
9
10
11

# Exercise Book 11.While Loop