-4

How to break two for loop at the highlighted line. (after showing the MessageBox.Show("THE ITEM ID DOES NOT EXIST.!"); )

bool conditionitem = true;

for (int cun = 0; cun < ItemIdNumber.Length; cun++)
{
    int Item_Id = Convert.ToInt32(ItemIdNumber[cun]);

    for (int yyu = 0; yyu <= 1258038; yyu++)
    {
        int weer = c[yyu];

        if (weer == Item_Id)
        {
            conditionitem = false;
            itemseq = yyu;
        }
    }

    if (conditionitem != false)
    {
        MessageBox.Show("THE ITEM ID DOES NOT EXIST.!");
        break; //--> here i want two break for two times
    }
}

By this break it only break the first loop.

6
  • 1
    Duplicate: stackoverflow.com/questions/324831/… Commented Mar 31, 2014 at 10:26
  • possible duplicate of How to Break from main/outer loop in a double/nested loop? Commented Mar 31, 2014 at 10:27
  • 6
    I don't understand this, you are already in the outer loop. Commented Mar 31, 2014 at 10:28
  • 2
    @user3478432 As Tim has pointed out, that break is in the if statement, and will break out of the context of the first containing iterator block, in other words your outer for, not the inner one. So your code should work as you want it to, which means this is not a valid question. Unless you are not showing us the complete code sample... Commented Mar 31, 2014 at 10:30
  • Breaking for loop is a sign of bad programming approach. Commented Mar 31, 2014 at 10:44

3 Answers 3

1

Two options I can think of:

(1) Set a flag inside the second loop before you break out of it. Follow the inner iteration with a condition that breaks out of the first iteration if the flag is set.

 bool flag = false;
 foreach (item in Items)
 {
   foreach (item2 in Items2)
   {
       flag = true; // whenever you want to break
       break;
   }

if (flag) break;
}

(2) Use a goto statement.

  foreach (item in Items)
  {
    foreach (item2 in Items2)
    {
        goto GetMeOutOfHere: // when you want to break out of both
    }

   }

    GetMeOutOfHere:
      // do what you want to do.
Sign up to request clarification or add additional context in comments.

1 Comment

goto isnt welcome mostly. I woudnt recommend it, mostly (yes, there are exceptions) its an indication of bad coding style.
1

You can refactor the loop to be a method that finds the item:

SomeType SomeMethod(int itemId)
{
    for (int cun = 0; cun < ItemIdNumber.Length; cun++)
    {
        int Item_Id = Convert.ToInt32(ItemIdNumber[cun]);

        for (int yyu = 0; yyu <= 1258038; yyu++)
        {        
            if (c[yyu] == itemId) return yyu;
        }
    }
    return null;
}

Then just use that:

var item = SomeMethod(Item_Id);
if(item == null)
{
    MessageBox.Show("THE ITEM ID DOES NOT EXIST.!");
}
else
{
    // ...
}

This also avoids mixing UI logic and internal logic.

Comments

0

Put your nested loop into a function and return true/false whenever you want to break the loop?

bool Function()
{
    for(int i = 0; i < 10; ++i)
    {
        for(int j = 0; j < 10; ++j)
        {
            if (error)
            {
                MessageBox.Show("THE ITEM ID DOES NOT EXIST.!");
                return false;
            }
        }
    }
    return true;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.