Continue; used to skip many cycles

Here is the diagram of my code:

while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 continue;
            }
} 

What will happen next? It will only make the second loop iterate once, no? I would like him to reach the time, is this possible?

Thank!

+3
source share
6 answers

It continueaffects the next cycle - the second for. There are two ways to go directly to while:

  • gotoalthough it is sometimes β€œconsidered harmful”, this is perhaps the main reason it still exists.
  • return

To illustrate the latter:

while (..)
{
    DoSomething(..);
}

void DoSomething(..) {
    for (...; ...;...)
      for(...;...;...)
          if ( )
          {
             ...
             return;
          }
}

and first:

while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 goto continueWhile;
            }
   continueWhile:
       { } // needs to be something after a label
}
+5
source
while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 goto superpoint;
            }
superpoint:
//dosomething
} 
+2
source

, , .

while (..)
{
    bool goToWhile = false; 

    for (...; ... && !goToWhile; ...)
        for (...; ... && !goToWhile; ...)
            if ( )
            {
                ...
                goToWhile = true; 
            }
} 

;)

+2

, continue; , , , ,

+1

continueor breakalways for the innermost cycle that accepts continueor break. In this case, this is the lowest loop forin your code.

+1
source

This is not possible only with the continue statement. The action Continue and break affects only the innermost loop in which they are nested.

You can set the variable and check it in the outer loop. Or reorganize the IF statement and the break clause in the for statement.

while (..)
{
   for (...; ...;...)
   {
        for(...;...;...)
            if ( )
            {
                 ...
                 skipLoop = true
            }
         if (skipLoop)
             continue;
    }
} 

Hope this helps!

0
source

Source: https://habr.com/ru/post/1788433/


All Articles