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
1myvariable = 10
2while myvariable > 0:
3 print(myvariable)
4 myvariable -= 1
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")
1j = 0
2while j < 3:
3 print("hello") # Or whatever you want
4 j += 1
5#This runs the loop until reaches 3 and above
1# a while loop repeats its instructions as long as the condition is met
2# this one will loop indefinitely
3python_is_cool = True
4
5while python_is_cool:
6 print("I love python!")
7
8# while loops are useful when you don't know the number
9# of times you want to iterate in advance