can anyone please explain how the code has been executed line by line | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

22nd Jul 2020, 11:56 AM
Bald Bear
Bald Bear - avatar
9 Answers
+ 3
starts at two, adds two and prints each time it loops to 10
22nd Jul 2020, 12:04 PM
Slick
Slick - avatar
+ 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
22nd Jul 2020, 12:56 PM
Navneet Kaur💫
Navneet Kaur💫 - avatar
+ 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.
22nd Jul 2020, 12:09 PM
Akash
Akash - avatar
+ 2
Slick You have written program in which programming language.
23rd Jul 2020, 5:55 AM
Akash
Akash - avatar
+ 1
guys note that the output is 12 i dont know why
23rd Jul 2020, 3:31 AM
Bald Bear
Bald Bear - avatar
+ 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
23rd Jul 2020, 8:59 AM
Slick
Slick - avatar
0
Slick thats exactly what i want to know,the output is 12 ,how..
29th Dec 2020, 12:58 PM
Bald Bear
Bald Bear - avatar
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
29th Dec 2020, 1:01 PM
Bald Bear
Bald Bear - avatar
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
29th Dec 2020, 1:02 PM
Slick
Slick - avatar