+ 1

Need help with Guess the output question.

what is the last value of y? int y=4; for(int x=0;x<5;x++) y++; System.out.print(y);//outputs 9 (i know how) But for(int x=0;x<5;x++); y++; System.out.print(y);//outputs 5 What is the use of ; in the for(); statement can anybody explain?

22nd Jan 2018, 3:05 PM
Sunil Thakali
Sunil Thakali - avatar
2 odpowiedzi
+ 8
The second loop is an empty loop(a loop that doesn't change anything outside its loop definition) because it is terminated by the semicolon at its end. So the initial value for y was 4, then y is incremented y++; so the next time y is printed the value of y becomes 5. This is a challenge question I presume? something quite similar was asked before, had it not for a friend's note, I would never know about this fact : ) Hth, cmiiw
22nd Jan 2018, 3:19 PM
Ipang
+ 3
Thank you..yes it was a challange question..similar questions were asked in the challange questions
22nd Jan 2018, 3:34 PM
Sunil Thakali
Sunil Thakali - avatar