Loops in C# .NET
July 19, 2018
There are different kinds of loops are used in C# .NET as follows:
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
}