Loops - in COBOL and C# and VB.NET

C# COBOL VB.NET
Pre-test Loops:
// no "until" keyword
while (c < 10)
{
   c++;
}





for (c = 2; c <= 10; c += 2)
{
   Console.WriteLine(c);
}

Post-test Loop:
do
{
  c++;
} while (c < 10);


Array or collection looping
string[] names = {"Fred", "Sue", "Barney"};
foreach (string s in names)
{
   Console.WriteLine(s);
}


Breaking out of loops
int i = 0;
while (true)
{
  if (i == 5)
  {
      break;
  }
  i++;
}

Continue to next iteration
for (i = 0; i < 5; i++)
{
  if (i < 4)
  {
    continue;
  }
  Console.WriteLine(i);   // Only prints 4
}
declare c as binary-long = 0

perform 10 times
    display "Again and "
end-perform

*>Pre-test loops:
perform until c >= 10
   add 1 to c
end-perform

perform varying c from 2 by 2 until c > 10
   display c
end-perform

perform varying c2 as binary-long from 2 by 2 until c2 > 10
   display c2
end-perform

*>Post-test loops:
set c = 0
perform with test after until c >= 10
   add 1 to c
end-perform

*> Varying

*>Array or collection looping
declare names as string occurs any
set content of names to ("Fred" "Sue" "Barney")
perform varying s as string through names
   display s
end-perform

*>Breaking out of loops:
declare i as binary-long = 0
perform until exit
   if i = 5
      exit perform
   end-if
   display i
   add 1 to i
end-perform

*>Continue to next iteration:
set i = 0
perform varying i from 0 by 1 until i >= 5
   if i < 4
      exit perform cycle
   end-if
   display i *>Only prints 4
end-perform
Pre-test Loops:
While c < 10
  c += 1
End While
Do Until c = 10
  c += 1
Loop

Do While c < 10
  c += 1
Loop
For c = 2 To 10 Step 2
  Console.WriteLine(c)
Next


Post-test Loops:
Do
  c += 1
Loop While c < 10   Do
  c += 1
Loop Until c = 10

Array or collection looping
Dim names As String() = {"Fred", "Sue", "Barney"}
For Each s As String In names
  Console.WriteLine(s)
Next



Breaking out of loops
Dim i As Integer = 0
While (True)
   If (i = 5) Then
      Exit While
   End If
   i += 1
End While



Continue to next iteration
For i = 0 To 4
   If i < 4 Then
      Continue For
   End If
   Console.WriteLine(i)   ' Only prints 4
Next