I don't understand why | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I don't understand why

The output of this piece of code is 9. int a = 5; int b = 4; while (b-- != 0) ++a ; System.out.print(a); Can someone please explain why?

5th Sep 2019, 8:40 PM
Boubacar Toure
Boubacar Toure - avatar
3 Answers
+ 1
Yes, I can: You chose to use: While( somthing ) do one thing Instead of While( somthing ){ do everything between brackets } So the loop will do b-- and ++a, giving this set of b/a 4/6 3/7 2/8 1/9 0 --> because you post decrease b it will first execute and then subtract 1 from b Print( 9 ) Because this isn't in your loop (using the first example of while loop will only execute one thing) you will only get the output after the loop, and not while looping
5th Sep 2019, 8:55 PM
Roel
Roel - avatar
+ 1
Well I'm pretty sure the answer is right you basically asked for 3+6 b-- = 3 a++ = 6
5th Sep 2019, 8:57 PM
Yusuf
Yusuf - avatar
0
thanks for the answer Roel
5th Sep 2019, 9:42 PM
Boubacar Toure
Boubacar Toure - avatar