public static void main(String[] args) {
int x = 0;
int y = 30;
for (int outer = 0; outer < 3; outer ++) {
for (int inner = 4; inner > 1; inner --) {
x = x + 3;
y = y - 2;
if (x == 6) {
break;
}
x = x + 3;
}
y = y - 2;
}
System.out.println(x + " " + y);
}
I was studying with a HeadFirstJava Book (the most recent one published) and I was working on this question. In this code, there is a break statement in the loop. I am not quite sure on which stage that the code will break by itself. Can you show me the process? even if the break statement does not execute, can you tell me why? Thank you!
What I am curious about at this point is will the program will start the if statement right away as soon as x reaches 6?
-> The program will then run for integer inner value 3 , then x becomes 9 and y becomes 26 . x != 6 , if condition wont be executed then x becomes 12
inner value 2 :
x = 15, y = 24 , x = 18
inner value 1 : ( the for loop break )
x = 18 and y= 22
outer =1 :
inner = 4, x = 24, y = 20 ,
inner = 3 , x = 30 , y =18
inner = 2 , x = 36, y = 16
inner = 1 ( won't executed )
exits inner for loop
x = 36, y = 14
outer= 2 ;
inner = 4 :
x = 42, y = 12
inner = 3 :
x = 48 , y = 10 ;
inner =2
x = 54, y = 8
inner =1 : exits inner for loop
x = 54 and y = 6
thus the final values are x = 54, and y = 6 only
: