C# Loops

In C#, it is possible to manage the execution of code with different control structures. In this article, we will look at loops. Loops can be used to repeat parts of a code a specified number of times or until a certain condition is met.

Contents

C# While Loops

The syntax for a while loop is:

1 while (condition) 
2 {
3 	Statement
4 } 

The syntax can almost be directly translated into English: do something while a condition is met.

Let’s look at a simple example:

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {
 5         int i = 1;
 6         while (i <= 10)
 7         {
 8             Console.WriteLine("This text is repeated 10 times");
 9             i = i + 1;
10         }
11         Console.ReadLine();
12     }
13 }
14 
15 /*
16 This example produces the following results:
17 This text is repeated 10 times
18 This text is repeated 10 times
19 This text is repeated 10 times
20 This text is repeated 10 times
21 This text is repeated 10 times
22 This text is repeated 10 times
23 This text is repeated 10 times
24 This text is repeated 10 times
25 This text is repeated 10 times
26 This text is repeated 10 times
27 */

In the example, a variable named i is used. As you can see, variables in C# always start with a datatype int in our case.

Apart from that, the example is almost self-explanatory. First the variable i is set to be equal to 1. Then the loop asks the .NET to repeat the text while i is less or equal to 50. In each loop, the variable i will be increased by 1.

C# Do-While Loops

A ‘do-while’ loop is just like a ‘while’ loop except that the condition is evaluated after the block of code specified in the ‘do’ clause has been run. So even where the condition is initially false, the block runs once. For instance, the following code outputs ‘4’:

1 int a = 4;
2 do
3 {
4     System.Console.WriteLine(a);
5     a++;
6 } while (a < 3);

C# For Loops

Another way to make a loop is with for statement in the following form:

1 for (Initialization; Condition; Step) 
2 {
3 	 Statement
4 }

The statement is repeated as long as ‘Initialization’ + ‘Step’ meets the ‘Condition’. If that doesn’t make sense, look at this example:

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {
 5         for (int i = 0; i <= 50; i = i + 5) 
 6         {
 7             Console.WriteLine("variable i is now = "+ i );
 8         }
 9         Console.ReadLine();
10     }
11 }
12 
13 /*
14 This example produces the following results:
15 variable i is now = 0
16 variable i is now = 5
17 variable i is now = 10
18 variable i is now = 15
19 variable i is now = 20
20 variable i is now = 25
21 variable i is now = 30
22 variable i is now = 35
23 variable i is now = 40
24 variable i is now = 45
25 variable i is now = 50
26 */

In the example above, i is growing with the value 5 in each loop. The loop will continue as long as i is below or equals 50. Also note how the value i is used as part of the sentence.

C# Nested Loops

In principle, there are no limitations on how loops can be used. For instance, you can easily put loops inside loops and thereby create many repeats.

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {
 5         for (int i = 1; i <= 10; i++)
 6         {
 7             for (int j = 1; j <= 10; j++)
 8             {
 9                 Console.Write( j + "X" + i + "=" +  j * i + "\t");
10             }
11             Console.WriteLine();
12         }
13         Console.ReadLine();
14     }
15 }
16 
17 /*
18 This example produces the following results:
19 1X1=1   2X1=2   3X1=3   4X1=4   5X1=5   6X1=6   7X1=7   8X1=8   9X1=9   10X1=10
20 1X2=2   2X2=4   3X2=6   4X2=8   5X2=10  6X2=12  7X2=14  8X2=16  9X2=18  10X2=20
21 1X3=3   2X3=6   3X3=9   4X3=12  5X3=15  6X3=18  7X3=21  8X3=24  9X3=27  10X3=30
22 1X4=4   2X4=8   3X4=12  4X4=16  5X4=20  6X4=24  7X4=28  8X4=32  9X4=36  10X4=40
23 1X5=5   2X5=10  3X5=15  4X5=20  5X5=25  6X5=30  7X5=35  8X5=40  9X5=45  10X5=50
24 1X6=6   2X6=12  3X6=18  4X6=24  5X6=30  6X6=36  7X6=42  8X6=48  9X6=54  10X6=60
25 1X7=7   2X7=14  3X7=21  4X7=28  5X7=35  6X7=42  7X7=49  8X7=56  9X7=63  10X7=70
26 1X8=8   2X8=16  3X8=24  4X8=32  5X8=40  6X8=48  7X8=56  8X8=64  9X8=72  10X8=80
27 1X9=9   2X9=18  3X9=27  4X9=36  5X9=45  6X9=54  7X9=63  8X9=72  9X9=81  10X9=90
28 1X10=10 2X10=20 3X10=30 4X10=40 5X10=50 6X10=60 7X10=70 8X10=80 9X10=90 10X10=100
29 */

In this example, we have created a multiplication table from 1 to 10 you can easily change the value for variable i and j and extend the table.

C# Foreach Loops

The ‘foreach’ loop is used to iterate through the values contained by any object which implements the IEnumerable interface. When a ‘foreach’ loop runs, the given variable1 is set in turn to each value exposed by the object named by variable2. As we have seen previously, such loops can be used to access array values. So, we could loop through the values of an array in the following way:

1 int[] a = new int[]{1,2,3};
2 foreach (int b in a)
3     System.Console.WriteLine(b);

The main drawback of ‘foreach’ loops is that each value extracted (held in the given example by the variable ‘b’) is read-only.

C# Loops
Share this

Subscribe to Code with Shadman