10

For sake of argument, how could I do this in VB?

foreach foo in bar
{
   if (foo == null)
       break;

   if (foo = "sample")
       continue;

   // More code
   //...
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jude Allred
  • 10,977
  • 7
  • 28
  • 27
  • Exit For, Exit Select etc., and you can always fake a break; statement with a GoTo statement and a Line Label. Many people frown on GoTo statements btw. and I found that whenever I do use them they always disappear in the rewrite. – David Rutten Sep 10 '09 at 00:57
  • For the other way around, in C#: *[C# loop - break vs. continue](https://stackoverflow.com/questions/6414/c-sharp-loop-break-vs-continue/6417#6417)* – Peter Mortensen Jun 24 '17 at 17:34

2 Answers2

19

-- Edit:

You've changed your question since I've answered, but I'll leave my answer here; I suspect a VB.NET programmer will show you how to implement such a loop. I don't want to hurt my poor C# compilers feelings by trying...

-- Old response:

I believe there is

Continue While
Continue For

and

Exit While
Exit For
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Noon Silk
  • 54,084
  • 6
  • 88
  • 105
4

I thought a VB.NET example may help in the future:

Sub breakTest()
    For i = 0 To 10
        If (i = 5) Then
            Exit For
        End If
        Console.WriteLine(i)
    Next
    For i = 0 To 10
        If (i = 5) Then
            Continue For
        End If
        Console.WriteLine(i)
    Next
End Sub

The output for break:

0
1
2
3
4

And for continue:

0
1
2
3
4
6
7
8
9
10
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sansknwoledge
  • 4,209
  • 9
  • 38
  • 61