-2

I need to stop "for" cycle before it ends by itself. I have one "for" cycle in other. Here is my code(a simple example):

for (int i = 0; i < 100; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                if (i == 5)
                {
                    //stop "for" cycle
                }
            }
        }

I want to stop the "for" cycle which is inside without stopping outside one. Which command should I use to manage that?

Kalvis
  • 595
  • 2
  • 7
  • 13
  • 4
    `break`.. Is it so hard to try? – L.B Mar 17 '14 at 21:22
  • 1
    use break as you suggested – thumbmunkeys Mar 17 '14 at 21:22
  • possible duplicate of [I want to know the behaviour of break statement in C. Does it work only for ';for-while-do-switch' or also for 'if-statements'?](http://stackoverflow.com/questions/2565659/i-want-to-know-the-behaviour-of-break-statement-in-c-does-it-work-only-for-fo) – La-comadreja Mar 17 '14 at 21:24
  • Next time before you ask a question, try googling the exact title you're about to ask, and see if you get any results that might help. – Colin DeClue Mar 17 '14 at 21:24
  • 1
    I don't think this question is worth -4: it has *nested* loops and so the answer is not obvious. Plus it's well-posed with a short example. – Bathsheba Mar 17 '14 at 21:26
  • From the early comments, I am guessing OP originally suggested using `break` himself, and then edited it out inside the edit-window. – Blorgbeard Mar 17 '14 at 21:28

4 Answers4

2

For just one layer of loop, you can use break.

for (int i = 0; i < 100; i++)
{
    for (int j = 0; j < 10; j++)
    {
        if (i == 5)
        {
            break; // <== Exits the 'j' loop, returns control
                   //     to the 'i' loop which continues
        }
    }
}

For nested loops, you may want to use a variable (for instance, a bool) that you set to a break condition. Looking at this page, I don't think C# has directed breaks as some other languages do.

bool keepGoing = true;
for (int i = 0; keepGoing && i < 100; i++)
{
    for (int j = 0; keepGoing && j < 10; j++)
    {
        if (i == 5)
        {
            keepGoing = false; // Stops both
        }
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0
    for (int i = 0; i < 100; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            if (i == 5)
            {
                break;
            }
        }
    }
LB2
  • 4,802
  • 19
  • 35
0

You can use break statements (which exit the innermost loop), or even a break to a label. Personally I don't like the latter as it means you need to maintain that label which makes code hard to read.

If you need to exit right out of an elaborate nested for loop structure then encode the whole thing in a function and use return. You can then even return a value.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

You want to break two loops with a single command? Use a goto.

for (int i = 0; i < 100; i++)
{
    for (int j = 0; j < 100; j++)
    {
        if (i == 10 && j == 50)
            goto Exit;
    }

    continue;

Exit:
    break;
}

Other has already posted how to break just the inner loop.

whoisj
  • 398
  • 1
  • 2
  • 10