Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does continue work?

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.

like image 933
user3884432 Avatar asked Jul 28 '14 13:07

user3884432


People also ask

How do you use continue?

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.

How does continue work in a for 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.

How does continue work in Javascript?

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.

How does continue work in CPP?

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.


1 Answers

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:

  1. 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.

  2. Second, another for iteration step is performed.

If execution of the Statement completes abruptly because of a continue with label L, then there is a choice:

  • If the for statement has label L, then the following two steps are performed in sequence:

    1. 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.

    2. Second, another for iteration step is performed.

  • If the for statement does not have label L, the for statement completes abruptly because of a continue with label L.

(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.

like image 134
arshajii Avatar answered Sep 28 '22 10:09

arshajii