# 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)
2
3
4
5
6
7
8
9
10
using while loop
i = 0
while( i<10 ):
print(i)
i = i+1
2
3
4
# Using While Loop
whileloop always do the job when condition isTrueand always stop when condition isFalse
i = 0
while( i<10 ):
print(i)
i = i+1
2
3
4
Debugging process
i = 0
while( i<10 ):
print(i)
i = i+1
2
3
4
Currently, i = 0
i = 0
while( i<10 ):
print(i)
i = i+1
2
3
4
i=0, i<10 => while(True)
i = 0
while( i<10 ):
print(i)
i = i+1
2
3
4
i=0, print(i) => print(0)
i = 0
while( i<10 ):
print(i)
i = i+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
2
3
4
i=1, i<10 => while(True)
i = 0
while( i<10 ):
print(i)
i = i+1
2
3
4
i=1, print(i) => print(1)
i = 0
while( i<10 ):
print(i)
i = i+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
2
3
4
i=2, i<10 => while(True)
i = 0
while( i<10 ):
print(i)
i = i+1
2
3
4
i=2, print(i) => print(2)
i = 0
while( i<10 ):
print(i)
i = i+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
2
3
4
i=9, i<10 => while(True)
i = 0
while( i<10 ):
print(i)
i = i+1
2
3
4
i=9, print(i) => print(9)
i = 0
while( i<10 ):
print(i)
i = i+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
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
2
3
4
output
OUTPUT
0
2
4
6
8
i = 1
while( i<10 ):
print(i)
i = i*2
2
3
4
output
OUTPUT
1
2
4
8
# Break and Continue
- If
breakis activated, the loop would be stopped. - If
continueis 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
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)
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:
add note or show note:
add note or show note:
['today my plan is ...', 'and next ...']
add note or show note:
add note or show note:
['today my plan is ...', 'and next ...', 'sleep']
add note or show note:
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)
2
3
4
5
6
7
8
9
10
11