+ 1
can anyone please explain how the code has been executed line by line
x = 2 while x <=10: x += 2 print(x) guys note that the output is 12 i dont know why
9 Antworten
+ 3
starts at two, adds two and prints each time it loops to 10
+ 3
Griffer Kay9 as you've defines the value of X as 2 so first the while loop checks the condition and then 2 in every value of X and everytime prints the value of X till X becomes <=10
+ 2
First the value of x is initialized or seted as 2.
Then there is a loop condition that x becomes 2 added to its value each time loop runs Until x is less than or equal to 10
And the value of x at each time gets printed.
+ 2
Slick You have written program in which programming language.
+ 1
guys note that the output is 12 i dont know why
+ 1
Akash I really didnt write my answer in code. I just meant to explain whats going on in his.
Griffer Kay9 can you copy and paste THAT code. Cause, its different than the code that is posted here. The while loop stops at 10. Theres no way it couldve gotten to 12
0
Slick thats exactly what i want to know,the output is 12 ,how..
0
I got the thing,print was not indented,so it was running like when x is finally = 10 ,incriments by 2 and prints x
0
because the while condition is set to less than or equal to 10. So when x == 10, the loop continues, adds 2 to x (x now equals 12) and then proceeds to print x which is now 12. Since x is now 12, it is not less than or equal to 10 so the loop stops