I have two questions about that code .1)is i=0 A STARTING POINT SINCE WHICH WE INCREASE OUR NUMBERS?Sorry ,for caps,forgot to turn off.2.Why does it print 0,2,3,4,5 if in condition only 1=1 should be printed?Explain ,please%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%20(8)|690x388
Thinking like the computer.
i = 0; Ok, I’m creating a variable, calling it i, and giving it a value of 0.
while 1 == 1: Ok, 1 is equal to 1. (note that it is a lot clear to just write “while true:”)
print(i) ok, look up the variable named i, and print the value. i has the value 0 (as assigned before the loop) so you are going to get the value 0.
still don`t get the explanation
Try this code instead.
i = 0 while true: i += 1 print(i) break
The condition of the while loop is checked every time to know if the loop should be stopped or executed again .
So in general,if the condition is evaluated to True the while loop executes the code in its body, and if the conditon is evaluated to False the loop will stop execution.
In your example the condition of the while loop (1==1) is evaluated to True, and will be always evaluated to True! (because 1==1 will be always True, there is no variable in this condition) so the loop will continue executing forever (infinite loop)…
Fortunately , you have another condition inside the body of your loop (the if i>=5 condition) which will make the loop stop using break.
I got it!The confusing part is 1==1 it really makes the code hard to understand from the first sight.I got it !It is just not a condition,and rather optional there/Thank you