0
While in python
Can anyone explain to me what while does?
1 ответ
+ 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