Someone kindly explain how we get to the answer, | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Someone kindly explain how we get to the answer,

I know it gives 3, but i would like to understand this piece of Code🙏🙏 https://code.sololearn.com/cpHtT14w7TBs/?ref=app

24th Apr 2022, 7:16 PM
Mike Fedha
Mike Fedha - avatar
7 Answers
+ 2
A do while loop runs at least once and then keeps running as long as the condition is true. In this case the condition is false, so the loop runs only the one time. Here you start with x=y=3. In the loop x is reduced by 3, so x=0. So y-x=3. Outside if the loop you have y-x again, but since x=0, y stays the same.
24th Apr 2022, 7:23 PM
Simon Sauter
Simon Sauter - avatar
+ 2
Sorry there was a typo in my first answer: x=0 after the loop, not x=3. I corrected it above.
24th Apr 2022, 7:29 PM
Simon Sauter
Simon Sauter - avatar
+ 1
Does x -= 3 mean x = 3-x or x=x-3 ? Simon Sauter
24th Apr 2022, 7:26 PM
Mike Fedha
Mike Fedha - avatar
+ 1
x = x-3
24th Apr 2022, 7:27 PM
Simon Sauter
Simon Sauter - avatar
+ 1
so the new value of x becomes y-(x-3) which is y-x+3 » 3-3+3 Simon Sauter
24th Apr 2022, 7:31 PM
Mike Fedha
Mike Fedha - avatar
+ 1
No. The initial value of x is 3. Then it is reduced by 3 once. So at the end the value of x is 0. Before the loop: x = 3 y = 3 After the loop: x = 0 y = 3
24th Apr 2022, 7:33 PM
Simon Sauter
Simon Sauter - avatar
+ 1
Thank you very much 🙏 Well understood, step by Step👌 Simon Sauter
24th Apr 2022, 7:37 PM
Mike Fedha
Mike Fedha - avatar