m.qayyum
m.qayyum

Reputation: 418

How to reset or restart nested loops

loop one
{
    looptwo
    {
        if(condition=true)
        {
           reset values//restart both loops
        }
     }
}
and possibilities for reset values is 3

basically i want to compair two matrices

a= 1 2 3 4

   1 2 3 4 
b= 3 4 5 6
   4 6 7 8

and when row 1 of a[] is matched with row 1 of b[].....i will add these rows and a[]
become = 2 4 6 8

for(i=0;i<rows;i++)
for(j=0;j<columns;j++)
{
a[i]=a[i]+b[i,j] 
}

and again find my maches from restart with new a[] Matrix

and i have to insure that all rows of b[] matrix are checked with a[] which are 3 in this case

Upvotes: 0

Views: 5991

Answers (5)

&#216;yvind Br&#229;then
&#216;yvind Br&#229;then

Reputation: 60694

The best choice here is to move the loops into their own method, and return from inside the inner loop. Example:

public void MyMehod(){
  loop one{
    looptwo{
      if(condition=true){
        return;
      }
    }
  }
}

If this is not possible for some reason, you can use a bool value that you set in the inner loop to bail out of all of them, but this is a bit more messy:

bool endloop = false;
while(!endloop){
  while(!endloop){
    if(condition){
      endloop = true;
    }
  }
}

For a while loop it looks ok, but even more messy for a for loop or a foreach loop.

Upvotes: 1

Mohamed
Mohamed

Reputation: 1

if you use numeric loop variables like i and j you can just reset the values

e.g.

for (i=0; i<10; i++) {
   for (j=0; j<10; j++) {
      if (var[i][j] == 'x') {
         i=0; j=0; break;
      }
   }
}

you can also use the method approach as suggested earlier

void someFunction(params) {
   for (i=0; i<10; i++) {
      for (j=0; j<10; j++) {
         if (var[i][j] == 'x') {
            someFunction(params)
            return;
         }
      }
   }       
}

Upvotes: 0

wageoghe
wageoghe

Reputation: 27608

If you can guarantee that you will have a condition that will tell you that you don't need to restart, you could wrap the whole thing in one more loop.

bool keepLooping = true;
while (keepLooping)
{
  keepLooping = false;
  for (int x = 0; x < maxx; x++)
  {
    for (int y = 0; y < maxy; y++)
    {
      if (DoSomething(x, y))
      {
        keepLooping = true;
        break;
      }
    }
    if (keepLooping)
    {
      break;
    }
  }
}

If you are checking a list for duplicates and modifying them do make all entries unique, you might do something like this (assuming string values):

List<string> a = GetNamesFromeSomewhere();

bool duplicateFound = true;
while (duplicateFound )
{
  duplicateFound = false;
  for (int x = 0; x < a.Length; x++)
  {
    for (int y = x + 1; y < a.Length; y++)
    {
      if (a[x].Equals(a[y]))
      {
        //Change a[y], but now we have to recheck for duplicates...
        a[y] += "_";
        duplicateFound = true;
        break;
      }
    }
    if (duplicateFound)
    {
      break;
    }
  }
}

Upvotes: 0

Aaron McIver
Aaron McIver

Reputation: 24713

            Start:
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if(j == 5)
                        goto Start;
                }
            }

Although structuring your code in a way to not use a goto is a much better approach...

Upvotes: 0

cdhowie
cdhowie

Reputation: 168988

You have to use goto to break out of multiple loop levels in C#. For example:

RESTART:
    while (a) {
        while (b) {
            if (that_other_thing)
                goto RESTART;
        }
    }

Well, you don't have to use goto but the alternative might be using a bunch of flag variables to indicate that a restart is required. And that code will probably be pretty hard to follow.

Upvotes: 2

Related Questions