C# Looping

 General audience classification icon  General audience classification icon
C# [1] provides the following types of loops to handle looping requirements, listed in table 1.

Table 1: C# 2010 Loops Definitions
Sr.No. Loop Type & Description
1 while loop – it repeats a statement or a group of statements while a given condition is true. It tests the condition before executing the loop body
2 for loop – it executes a sequence of statements multiple times and abbreviates the code that manages the loop variable
3 do…while loop – it is similar to a while statement, except that it tests the condition at the end of the loop body
4 Nested loop – you can use one or more loop inside any another while, for or do…while loop

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:

  1. The 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.
  2. Next, the condition is evaluated. If it is true, the body of the loop is executed. If false, the loop's body does not run, and the control flow jumps to the next statement just after the for loop.
  3. After the body of the 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.
  4. The condition is now re-evaluated. If it is true, the loop executes, and the process repeats itself (body of the loop, then increment step, and then again testing for a condition). After the condition becomes false, the 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.

en/iot-open/getting_familiar_with_your_hardware_rtu_itmo_sut/raspberrypi_rpi/looping_winiot.txt · Last modified: 2023/11/23 10:29 by pczekalski
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0