-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.

Abbas
  • 14,186
  • 6
  • 41
  • 72
  • 1
    Duplicate: http://stackoverflow.com/questions/324831/breaking-out-of-a-nested-loop – Ulugbek Umirov Mar 31 '14 at 10:26
  • possible duplicate of [How to Break from main/outer loop in a double/nested loop?](http://stackoverflow.com/questions/13073300/how-to-break-from-main-outer-loop-in-a-double-nested-loop) – rpax Mar 31 '14 at 10:27
  • 6
    I don't understand this, you are already in the outer loop. – Tim Schmelter Mar 31 '14 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... – Adam Houldsworth Mar 31 '14 at 10:30
  • Breaking for loop is a sign of bad programming approach. – Faizan Mubasher Mar 31 '14 at 10:44
  • thank you @TimSchmelter. May you check the full version of my question and help me to figure this problem out. http://stackoverflow.com/questions/22758625/how-to-break-the-following-execution-and-error-in-showing-the-result – user3478432 Mar 31 '14 at 10:46

3 Answers3

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.
Birlla
  • 1,700
  • 2
  • 15
  • 17
  • *goto* isnt welcome mostly. I woudnt recommend it, mostly (yes, there are exceptions) its an indication of bad coding style. – BudBrot Mar 31 '14 at 10:43
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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
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;
}
Mike
  • 126
  • 9