+ 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?
3 odpowiedzi
+ 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.
+ 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;}
+ 3
"If x==5"..it stops,so x will have the value 5 after exiting the loop