+ 1

While looping rule (Edited)

Hey guys, can you explain Why my x changes in last line of the code? I learned the variable stores its value through the program but in this code, x changed as you see in the last line while x is 7 in first line.. Thanks <3 https://code.sololearn.com/cfMgQ88VDB8D/?ref=app

16th Nov 2022, 8:06 AM
Freequense
Freequense - avatar
4 Antworten
+ 3
In this way you can see how the loop works: in the last loop x become 12. After that the loop again check the value of x. And x is no more smaller than 12. So the loop ends. x = 7 print("x is",x) print("while statement run:") while x<12: print("while before add x is",x) x +=1 print("while after add x is",x) print("while statement end.") print("But now x is",x)
16th Nov 2022, 4:28 PM
JaScript
JaScript - avatar
+ 2
You can save the code on here on playground. The add in addition a print statement after x+=1 and look what happend.
16th Nov 2022, 8:12 AM
JaScript
JaScript - avatar
+ 1
Please share the code so we easily understand what's your doubt
16th Nov 2022, 8:54 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
0
The orders in your while loop states: print the x, then add 1. The last integer that met the condition was 11, so 11 was printed, then, "behind the scenes" x was updated to 12.
16th Nov 2022, 9:41 PM
Karolina Augustyniak
Karolina Augustyniak - avatar