28 While
28.1 First example
while condition_is_true:
do_something
= 0
a while a < 10:
= a + 1
a print(a)
When you use while loops, you need to first initialize your loop variable, and you need to also modify that variable so that loop will finish at some time.
28.2 No initialization example
while x < 10:
print(x)
Output will be like below.
while x < 10:
print(x)
NameError Traceback (most recent call last)
<ipython-input-1-c956f313d152> in <module>
----> 1 while x < 10:
2 print(x)
NameError: name 'x' is not defined
28.3 infinite loop
Not modifying condition variable. Following code will be infinite loops of 1 until you close the program or use CTRL-c.
= 1
x while x < 10:
print(x)
correct solution should be.
= 1
x while x < 10:
print(x)
= x +1 x