I've been playing around with some Java code (not my first language, but I'm trying to wrap my brain around it), but it is not giving me what I expected. I must not be looking at it correctly. I'd appreciate any suggestions on where I am mistaken..
HTML Code:
public class Mix5 {
public static void main(String[] args) {
int x = 0; //declare int named "x" & set value to 0
int y = 30; //declare int named "y" & set value to 30
for(int outer = 0; outer < 3; outer++) {
//Outer for loop: declare int named "outer", set value to 0.
//while value is less than 3, increment by 1.
for(int inner = 4; inner > 1; inner--) {
//Inner for loop: declare int named "inner", set value to 0.
//while is greater than 1, decrement by 1.
y = y - 2; //y = value of y (30) - 2 (28)
if(x == 6) {
break;
} //when x equals 6, break out of loop.
x = x + 3; //x = value of x(0) + 3 (3)
} //end inner for loop.
y = y - 2; //y = value of y (28) - 2 (26)
} //end outer for loop.
System.out.println(x + " " + y); //Print out value of x and y.
} //end main Method.
} //end Class.
Here's how I would THINK it would behave:
Setup:
x is set to 0 and y is set to 30.
In outer loop, int outer set to 0, and incremented by 1 until it = 3.
In inner loop, int inner set to 4, and decremented by 1 until it = 1.
Both loops should then complete a total of 3 times.
Action (Loop 1):
y equals itself (30) - 2 = 28.
a check is made, if x equals 6 (it doesn't yet), then break.
x equals itself (0) + 3 = 3.
y equals itself (now 28) - 2 = 26
Action (Loop 2):
y equals itself (26) - 2 = 24.
a check is made, if x equals 6 (it doesn't yet), then break.
x equals itself (3) + 3 = 6.
y equals itself (now 24) - 2 = 22
Action (Loop 3):
y equals itself (now 22) - 2 = 20
a check is made, if x equals 6 (it now does), then break.
Break out of loops.
Print a line with the value of x and the value of y. It should be:
6 20
However, what I get when I run this is:
6 14
Does my if statement only break me out of the inner for loop, not both? This is the only reason I can think that it would continue to process.