Ahmadali Shafiee
Ahmadali Shafiee

Reputation: 4657

How can I continue The upper Loop

I have this code:

foreach(int i in Directions)
        {
            if (IsDowner(i))
            {
                while (IsDowner(i))
                {
                    continue;
                    //if (i >= Directions.Count)
                    //{
                    //    break;
                    //}
                }

                //if (i >= Directions.Count)
                //{
                //    break;
                //}

                if (IsForward(i))
                {

                        continue;
                        //if (i >= Directions.Count)
                        //{
                        //    break;
                        //}

                    //check = true;
                }

                //if (i >= Directions.Count)
                //{
                //    break;
                //}

                if (IsUpper(i))
                {
                        //if (i >= Directions.Count)
                        //{
                        //    break;
                        //}
                    num++;
                    //check = false;
                }

                //if (check)
                //{
                //    num++;
                //}
            }
        }

but I want to have continue for foreach in while loop. how can I do this?

Upvotes: 4

Views: 4247

Answers (4)

Saikat Chakraborty
Saikat Chakraborty

Reputation: 311

you could try to use predicates for the inner for loop like:

    foreach (item it in firstList)
     {
        if (2ndList.Exists(x => it.Name.StartsWith(x))) //use predicate instead of for loop.
            {
                continue;
            } 
     }

Hope this helps you.

Upvotes: 0

laroley
laroley

Reputation: 41

In my opinion, using goto is justifiable within complex nested loops (whether or not you should avoid using complex nested loops is a different question).

You could do this:

foreach(int i in Directions)
{
    while (IsDowner(i))
    {
        goto continueMainLoop;
    }
    //There be code here
continueMainLoop:
}

Just be careful if other people have to deal with the code, make sure they're not goto-phobic.

Upvotes: 4

Alexey Raga
Alexey Raga

Reputation: 7525

You cannot continue an outer loop from an inner one. You have two options:

  1. The bad one: set a boolean flag before breaking the inner loop, then check this flag and continue if it is set.

  2. The good one: simply refactor your big spagetti code into a set of functions so you do not have inner loops.

Upvotes: 9

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could break out of the while loop and move on to the next iteration of the outer foreach loop which will start a new while loop:

foreach(int i in Directions)
{
    while (IsDowner(i))
    {
        break;
    }
}

If you had some other code after the while loop that you don't want to be executed in this case you could use a boolean variable which will be set before breaking out of the while loop so that this code doesn't execute and automatically jump on the next iteration of the forach loop:

foreach(int i in Directions)
{
    bool broken = false;
    while (IsDowner(i))
    {
        // if some condition =>
        broken = true;
        break;
    }

    if (broken) 
    {
        // we have broken out of the inner while loop
        // and we don't want to execute the code afterwards
        // so we are continuing on the next iteration of the
        // outer foreach loop
        continue;
    }

    // execute some other code
}

Upvotes: 6

Related Questions