Unnecessary mark on continuation / break
I am confusing the execution of the following code:
label:
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) continue;
if (i == 99) {
continue label;
}
System.out.println("i = " + i);
}
I was hoping this cycle is endless. But no. When the value is 99 , the program has been terminated. I am trying to play with operators continue
and break
, but still did not expect the result.
I'll just try to understand why this loop is not infinite and what would I do to get it done?
Output:
i = 1 i = 3 ...... i = 93 i = 95 i = 97 Process finished with exit code 0
source to share
continue
continues the marked cycle, which does not include a reset i
to its initial value; so nothing you do prevents it from increasing i
until it reaches the value 100
and the loop ends. And yes, you are correct that the label is not needed.
To make it infinite just reset i
inside the loop, no need for a shortcut or continue
:
for (int i = 0; i < 100; i++) {
if (i == 99) {
i = 0;
}
System.out.println("i = " + i);
}
or perhaps this, which again produces 99, then 0 and keeps going:
for (int i = 0; i < 100; i++) {
System.out.println("i = " + i);
if (i == 99) {
i = -1;
}
}
Or if for some reason you really wanted to do it with continue
, you need a second outer loop: http://ideone.com/ofgpK3
label:
while (true) {
for (int i = 0; i < 100; i++) {
System.out.println("i = " + i);
if (i == 99) {
continue label;
}
}
}
source to share
The label is not like a goto statement that you seem to be confusing it with. When you continue
pass a program to the specified loop instruction, you only execute continue
that by executing the next loop. As you continue in i = 99
, you loop for
with the next iteration value i = 100
, which makes the loop condition i < 100
equal false
such that the loop ends.
What you are trying to achieve will require an outer loop to:
label: while (true) {
for (int i = 0; ; i++) {
if (i % 2 == 0) continue;
if (i == 99) {
continue label;
}
System.out.println("i = " + i);
}
}
}
Note that I removed the condition in the loop for
, which is now redundant, because the explicit jump back into the outer loop while
, which implicitly ends the inner loop. This jump restarts the inner loop and therefore causes the counter to reset to i = 0
. In general, such an explicit program flow, however, is difficult to read and easy to confuse, so it should be avoided whenever possible, for the same reason that one would like to avoid a goto statement about which labels are very light versions.
source to share
i
is initialized only once. continue
not reset i
to 0, so when you reach the label continue
, the condition in the loop for
will evaluate to false
and you will miss it.
See the docs :
When using this version of the for statement, keep in mind that:
- Initialization initializes the loop; executed once when the loop starts .
- When the completion expression evaluates to false, the loop ends.
- The increment expression is called after each iteration through the loop; this is a perfectly acceptable expression to increase or decrease a value.
source to share