1# Python does not have a do-while loop. You can however simulate
2# it by using a while loop over True and breaking when a certain
3# condition is met.
4# Example:
5i = 1
6while True:
7 print(i)
8 i = i + 1
9 if(i > 3):
10 break
1# A while loop is basically a "if" statement that will repeat itself
2# It will continue iterating over itself untill the condition is False
3
4python_is_cool = True
5first_time = True
6
7while python_is_cool:
8 if first_time:
9 print("python is cool!")
10 else:
11 first_time = False
12
13print("Done")
14
15# The while loop can be terminated with a "break" statement.
16# In such cases, the "else" part is ignored.
17# Hence, a while loop's "else" part runs if no break occurs and the condition is False.
18# Example to illustrate the use of else statement with the while loop:
19
20counter = 0
21
22while counter < 3:
23 print("Inside loop")
24 counter = counter + 1
25else:
26 print("Inside else")
1while <condition>:
2 <run code here>
3
4# for example,
5i = 0
6while True:
7 i += 1
8 # i will begin to count up to infinity
9while i == -1:
10 print("impossible!")
1while <Condition>:
2 <code>
3
4 #example
5 i = 10
6 while i == 10:
7 print("Hello World!")