I am trying to understand how "continue" works. I understood the concept of the keyword but when I run different programs, it works differently :-/ Let me show you few examples:
If I run this program :
int j = 0; int i = 0; LABEL1: for (; i < 3; i++) { if (true) continue; }
The value of i is gonna be 3. so far so good. Let's add an outer loop :
int j = 0; int i = 0; LABEL2: for (; j < 3; j++) { LABEL1: for (; i < 3; i++) { if (true) continue LABEL2; } }
The value of i is gonna be ... 0 !! I don't understand why i is not incremented if continue is used with a label that goes to the outer loop. Can somebody explain it why ? Do you have some tricky things like that with break ? or with a do {} while ?
I really appreciate any help you can provide.
The continue statement gives you the option to skip over the part of a loop where an external condition is triggered, but to go on to complete the rest of the loop. That is, the current iteration of the loop will be disrupted, but the program will return to the top of the loop.
The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop. In a for loop, the continue keyword causes control to immediately jump to the update statement.
The continue statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.
C++ Continue The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
The basic for
statement structure is as follows:
BasicForStatement: for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement
Now, from JLS §14.14.1.3. Abrupt Completion of for
Statement:
If execution of the Statement completes abruptly because of a
continue
with no label, then the following two steps are performed in sequence:
First, if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If the ForUpdate part is not present, no action is taken.
Second, another
for
iteration step is performed.If execution of the Statement completes abruptly because of a
continue
with labelL
, then there is a choice:
If the
for
statement has labelL
, then the following two steps are performed in sequence:
First, if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If the ForUpdate is not present, no action is taken.
Second, another
for
iteration step is performed.If the
for
statement does not have labelL
, thefor
statement completes abruptly because of a continue with labelL
.
(emphasis mine)
ForUpdate, in your case, is i++
. Based on what's above:
Your first snippet falls under the first case, so i
is incremented.
Your second snippet falls under the second case, so i
is not incremented because the for
statement completes abruptly.
Note that if you had continue LABEL1
in your second snippet, i
would have been incremented as in your first snippet (in accordance with the JLS).
As a tip for the future, for definitive answers regarding language rules/semantics, you should always consult the language specification. For Java, that's the JLS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With