このヘルプでは、COBOL、C#、VB.NET で使用できるさまざまなループ構造の種類について説明します。
Pre-test loops:
*> No WHILE keyword
perform until c >= 10
add 1 to c
end-perform
perform varying c from 2 by 2 until c > 10
display c
end-perform
Post-test loops:
perform with test after until c >= 10
add 1 to c
end-perform
Array or collection looping
01 names string occurs any.
01 s string.
set content of names to ("Fred" "Sue" "Barney")
perform varying s through names
display s
end-perform
Breaking out of loops:
01 i binary-long value 0.
perform until exit
if i = 5
exit perform
end-if
add 1 to i
end-perform
Continue to next iteration:
01 i binary-long value 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:
// 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
}
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
これらの例の一部は、ハーディング大学コンピュータ サイエンス学部の Frank McCown 博士が作成したもので、クリエイティブ コモンズ ライセンスに基づいて使用が許可されています。