+ 2

(Java) While loop, with break

https://code.sololearn.com/cY8lfb9crYg8/?ref=app why the last statement is “exit loop at x = 5”, not x = 4? does it mean that when break, System.out.print(x) is not print, but x++; is executed?

20th Nov 2018, 9:08 AM
Poo
3 ответов
+ 13
Loop will get break only when the value of x will be 5. Since loop gets break at x = 5 so the value in the variable x gets printed.
20th Nov 2018, 9:22 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 9
Becuase you break out of the while loop when x equals 5 and then the program executes the next print statement outside the body of the while loop it works the way i expected. you should change your if statement to look like this if(x==5){break;}
20th Nov 2018, 9:19 AM
D_Stark
D_Stark - avatar
+ 3
"If x==5"..it stops,so x will have the value 5 after exiting the loop
20th Nov 2018, 9:14 AM
Mensch
Mensch - avatar