Code then description of problem:
class Weather { public static void main(String[] args) { // declare variables int i = 1; // represents year double temp = 0.0; final double ANNUAL_INCREASE = 1.0; double uncertainty = 0.0; String result = " "; System.out.println(" Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"); while(true) { if (i > 5) break; else if (i == 3) continue; System.out.println(i); i++; } } }
So part of my assignment is to loop though my variable for year (named i) from 1 through 5. Then skip through once "i" has the value of 3, and continue on afterwards. The code above compiles and displays this output:
Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1 2
It stops and leaves 3 as an empty blinking line inside of my terminal. Switching the break and continue statements around like this:
while(true) { if (i == 3) continue; else if (i > 5) break; System.out.println(i); i++; }
gives me the same result as the first code. It was my understanding that the break statement is essentially a loops stopping point, and continue carries on to the next iteration without terminating the loop.
This code below gives me the nearly desired result:
while(true) { if (i > 5) break; System.out.println(i); i++; }
It prints out like this:
1 2 3 4 5
So that makes me know that the stopping point is at least correct. The confusion comes when trying to skip over once (i == 3).
The only time i was instructed to use the continue statement was inside of a for loop like this:
for (int i = 0 ; i <= 5 ; i++) { if (i == 3) continue; System.out.println("I = " + i); }
Which gives me the perfect output (1, 2, 4, 5). I'm hoping this is just a syntax thing rather than a misunderstanding on my end for combining a continue/break statement.
Any help towards the solution is appreciated