-4

I have a for loop in c# winform and I want to start for loop again if i=7

The code looks like this :

 for (int i = 0; i < 10; i++)
        {
           if (i==7)
             {
               MessageBox.Show("you must start for loop again ");
              //here I want to go back to for loop again
             }
        }  

any ideas ?

also,I know this code makes no sense, I just wrote it for example — I have similar situation in my c# code.

Vakho Akobia
  • 304
  • 1
  • 3
  • 13

2 Answers2

5

In this case, just mutate i back to 0, like so:

for (int i = 0; i < 10; i++)
{
    if (i==7)
    {
        MessageBox.Show("you must start for loop again ");
        i = 0;
    }
}

If you really want to use goto, then here is an example for that:

BackToTheStart:
  for (int i = 0; i < 10; i++)
  {
      if (i==7)
      {
          MessageBox.Show("you must start for loop again ");
          goto BackToTheStart;
      }
  }

It's worth keeping in mind if you didn't already know, goto is generally considered bad practice and an unwelcome legacy baggage C# brought from C style languages of yesteryear. In most cases you do not need it, like in this example, it's easier not to use it. And, most importantly, no one will ever thank you for adding a goto.

Stuart
  • 5,358
  • 19
  • 28
0

It's worth noting that this would be an infinite loop. Think about it: if you go back to 0 every time i reaches 7, how would it ever bet the case that i == 10? How would you break out of the loop? This may be a case of the XY problem - i.e. you probably don't actually want to do what you say you want to do here.

If you really want this to occur in an infinite loop, just do this:

while (true) {
   for (int i = 0; i <= 7; i++) {
       if (i == 7) {
           // ..
       }
    }
 }

Actually, I'm slightly baffled as to why you bother with the "for" loop at all in this case if you're only doing something in the case where i = 7; why bother with the for loop at all and just put the action in an infinite loop?

Also, if you're doing this on the UI thread, it'll appear to "hang" the UI because the UI thread won't ever be able to do anything other than service your for loop. Users won't even be able to close it normally.

Finally, as other people have pointed out, never use goto, it's a bad practice - Microsoft shouldn't have even included it in the language.

Community
  • 1
  • 1
  • I'm afraid you didn't read the post completely . In the end i noted that i know this code will loop forever and makes no sense . I just needed to know how goto works – Vakho Akobia Jan 15 '17 at 21:20
  • @VakhoAkobia What actually you stated in your question is, "I know this code makes no sense, I just wrote it for example — I have similar situation in my c# code." Where do you state anything about knowing that this is an infinite loop? Also, if you want an infinite loop, why not just explicitly make an infinite loop like I do in my answer? Also, the correct answer to "how does goto work?" is *still* "don't use goto." – EJoshuaS - Stand with Ukraine Jan 15 '17 at 22:07