3

Here is a schema of my code :

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

What will do the continue? He will only make the second loop iterate one time, no? I would like it to reach the while, is it possible ?

Thanks!

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
Flo
  • 261
  • 1
  • 3
  • 6
  • 1
    you should avoid inner loops at all, they make code unreadable and highly error-prone. – onof Jan 28 '11 at 09:33
  • @onof I disagree, at least partially. There are times when "inner loops" are really the best way to solve the problem (sometimes another control abstraction or method call is better). However, I am of the belief that loop constructs should *always* use a `{}` as it helps indenting and avoids issues of confusing content-not-in-a-loop as being in the loop. –  Jan 28 '11 at 09:47

6 Answers6

5

The continue here impacts the nearest loop - your second for. There are two ways of jumping directly to the while:

  • goto, although sometimes "considered harmful", this is arguably the main reason why it still exists
  • return

To illustrate the latter:

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

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

and the former:

while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 goto continueWhile;
            }
   continueWhile:
       { } // needs to be something after a label
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2
while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 goto superpoint;
            }
superpoint:
//dosomething
} 
djeeg
  • 6,685
  • 3
  • 25
  • 28
  • goto shouldn't be used as far as i remember. – Pabuc Jan 28 '11 at 09:25
  • this is pretty much the reason goto exists, if it wasnt meant to be used why is it in the language? – djeeg Jan 28 '11 at 09:26
  • @Pabuc it shouldn't be used *lightly* - but here it does the job fine. Much simpler than checking a flag at every level. See also the MSDN [example](http://msdn.microsoft.com/en-us/library/13940fs2(v=VS.100).aspx) on breaking out of nested loops – Marc Gravell Jan 28 '11 at 09:27
  • I'm not talking about the way you use it. Goto should never be used. Don't know where I've read it but author had a pretty good reason. Searching for it atm and will post if I can find it – Pabuc Jan 28 '11 at 09:28
  • Ok I can't find it but, I would still make this a method and return when found instead of goto. Maybe that is just me but I never use goto - never. – Pabuc Jan 28 '11 at 09:34
  • All the (justified) reasons I have seen for not using goto come down to readability. If the content of the loop is simple enough then I think it should be simple enough to use goto without detracting too much from the readability. Having said that, unless performance is of particular concern I probably would have just extracted the inner loops to a separate method. – Brian Reichle Jan 28 '11 at 09:59
  • This is kung-fu classics: - At first you don't know that something shouldn't be ever done. - Then you know that you should never do something. - Then you understand that sometimes it's ok to do something. - And finally you know 66 ways to achieve the result and understand that all of them are mostly equal. When you are asked about how to do the job, you think of all 66 ways, looking for what is common there, sighing and reply that: "well, actually the most important is harmony". Replying to offended apprentices question: "How would I achieve it?" you say: "never do something". – Konstantin Oznobihin Jan 28 '11 at 10:25
  • @Pabuc I assume you mean [goto considered harmful](http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html) that I alluded to in my post? Either way: `goto` is *still* one of the clearest and least confusing way of handling this scenario. I prefer the `return` approach, but that is just me. – Marc Gravell Jan 28 '11 at 10:56
2

You should set a variable to determine when you need to leave the loops.

while (..)
{
    bool goToWhile = false; 

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

Come up with better names though ;)

Brian Reichle
  • 2,798
  • 2
  • 30
  • 34
Jason
  • 220
  • 3
  • 7
1

Not possible directly as continue; only continues the execution of the current loop, to go to outer loop the only thing you can do is set some flags and check it in outer loop

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
1

continue or break is always for the most inner loop which accepts a continue or break. In this case, it's the lowest for loop in your code.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
0

It's not possible only with continue statement. Continue and break statement affect only the the most inner loop that their are nested.

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

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

Hope this helps!

anthares
  • 11,070
  • 4
  • 41
  • 61