Performing Loops – VB and C#

This will be a short post. I just wanted to display the differences between C# and VB when performing loops.  See below:

VB:

For i As Integer = 10 To 1 Step -1

Console.WriteLine(i)

If i < 5 Then

Exit For

End If

Next

 

Dim start As Integer = 5

For i As Integer = start To start + 10

Console.WriteLine(i)

Next

 

Dim str As String

Do

str = Console.ReadLine()

Console.WriteLine(str)

Loop Until str = “”

 

Do

str = Console.ReadLine().Trim()

If String.IsNullOrEmpty(str) Then

Exit Do

End If

Console.WriteLine(“You typed:{0}”, str)

Loop

Dim names As String() = New String() {“Bob”, “David”}

For Each name As String In names

Console.WriteLine(name)

Next

 

C#: 

for (int i=0; i<10; i++) {}

 

while (i<10) { i++; }

 

do { i++; } while (i<10);

 

foreach (ListItem item in list.Items) {}