While in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

While in python

Can anyone explain to me what while does?

8th Nov 2016, 7:47 PM
Atom Bomb
Atom Bomb - avatar
1 Answer
+ 5
it means run the code for as long the condition supplied is of True value for example x = 0 while x < 3: x += 1 print(x) the end value of x will be 3 let's see what happens step by step x = 0: 0<3? True, enter the loop x is increased by 1, now x is 1 x = 1: 1<3? True, repeat again x increased by 1, now x is 2 x = 2: 2<3? True, repeat again x is increased once more, now x is 3 x = 3: 3<3? False, end the loop and continue to the print(x) code
8th Nov 2016, 7:57 PM
Burey
Burey - avatar