Search code examples
cnested-loopscontinue

Continue inside nested loop in C


How does continue work for following example?

for(i = 0; i < 10; i++) {
    if( j && j->data != some_data) {
        continue;
    }

    if(j) {
       /* do something */
    }
}

Can I combine these two inside loop and achieve the same result like:

for(i = 0; i < 10; i++) {
    if(j) {
       if(j->data != some_data) {
          continue;
       }

       /* do something */
     }
}

Solution

  • Your example does not contain a nested loop. There is only one for and/or while in the code. Therefore the location of the continue statement depends only on your program logic.

    If you do have a nested loop, then a continue or break affects only the innermost loop.