C# [1] provides the following types of loops to handle looping requirements, listed in table 1.
while Loop
A while
loop statement in C# repeatedly executes a target statement if a given condition is true.
while(condition) { statement(s); }
Example:
using System; namespace Loops { class Program { static void Main(string[] args) { /* Local variable definition */ int a = 10; /* while loop execution */ while (a < 20) { Console.WriteLine("value of a: {0}", a); a++; } Console.ReadLine(); } } }
Output:
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
for Loop
A for
loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.
The syntax of a for loop in C# is:
for ( init; condition; increment ) { statement(s); }
Here is the flow of control in a for
loop:
init
step is executed first, and only once. This step allows you to declare and initialise any loop control variables. You are not required to put a statement here as long as a semicolon appears.for
loop.for
loop executes, the control flow returns to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank if a semicolon appears after the condition.for
loop terminates.Example:
using System; namespace Loops { class Program { static void Main(string[] args) { /* for loop execution */ for (int a = 10; a < 20; a = a + 1) { Console.WriteLine("value of a: {0}", a); } Console.ReadLine(); } } }
Output:
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
do…while Loop
The syntax of a do…while
loop in C# is:
do { statement(s); } while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) execute once before the condition is tested.
If the condition is true, the control flow returns to do
, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.
Example:
using System; namespace Loops { class Program { static void Main(string[] args) { /* Local variable definition */ int a = 10; /* do loop execution */ do { Console.WriteLine("value of a: {0}", a); a = a + 1; } while (a < 20); Console.ReadLine(); } } }
Output:
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
Nested for Loop
C# allows using one loop inside another loop (loop nesting). The following section shows a few examples to illustrate the concept.
The syntax for a nested for
loop statement in C# is as follows:
for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }
The syntax for a nested while
loop statement in C# is as follows:
while(condition) { while(condition) { statement(s); } statement(s); }
The syntax for a nested do…while
loop statement in C# is as follows:
do { statement(s); do { statement(s); } while( condition ); } while( condition );
A final note on loop nesting is that you can put any loop inside any other type. For example, a for loop can be inside a while loop or vice versa. Example:
using System; namespace Loops { class Program { static void Main(string[] args) { /* local variable definition */ int i, j; for (i = 2; i < 100; i++) { for (j = 2; j <= (i / j); j++) if ((i % j) ** 0) break; // if factor found, not prime if (j > (i / j)) Console.WriteLine("{0} is prime", i); } Console.ReadLine(); } } }
Output:
2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime 31 is prime 37 is prime 41 is prime 43 is prime 47 is prime 53 is prime 59 is prime 61 is prime 67 is prime 71 is prime 73 is prime 79 is prime 83 is prime 89 is prime 97 is prime
Infinite Loop
A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop is required, you can make an endless loop by leaving the conditional expression empty.
Example:
using System; namespace Loops { class Program { static void Main(string[] args) { for (; ; ) { Console.WriteLine("Hey! I am Trapped"); } } } }
When the conditional expression is absent, it is assumed to be true.