Home » ASP.NET Basics » 07 - Writing code and handling events
7

Loops

Defining function and uses of loops

Loops are used for repeating a statement or group of statements. The use of loops makes it very easy to repeat a process as many times as we want without writing the same code repeatedly. A major use of loops is in working with Arrays. We will learn about arrays later in this chapter.

C# provides different types of loop statements. Let us examine them:

`for' loop

The `for' loop is the most commonly used loop. To use this loop, we need to know the following things:

  • A starting value to initialize (start) the loop.
  • A condition for running the loop.
  • An operation to update the loop.

The syntax of the `for' loop is:

 for(initialize loop; check condition; update loop) { //statements inside the loop. }

Here is an example of the `for' loop-

 int count, number; number =1; for(count=0;count<10;count++) { number=number+1; }

In the example, the initial value of count is 0. This initial value is tested using the condition - count<10. Since the value of count is 0 and it is less than 10, the program will enter inside the loop and execute the statements here. Once all the statements inside the loop are executed, the program will increase the variable `count' by 1 (count++). Now the value of count is 1. This new value will again be tested using the condition. The condition is still true (1 is less than 10). Hence, the loop will execute once again.

When the value of count becomes 9, the loop will execute and then increase the count to 10. Now the condition will become false since 10 is not less than 10. Here, the loop will stop.

If we make a mistake in the initialization, the loop may not run. For example, if we initialize count with 10 instead of 0, the condition will become false very first time and the loop will not execute even once.

Similarly, if we forget to increment count, it will always remain 0. Now the loop will run forever because 0 is always less than 10 so the condition remains true. Such a loop is known as an infinite loop.

`while' loop

The `while loop is almost similar to the `for' loop with some difference in the syntax:

 initialize loop; while (check condition) { //statements inside the loop update loop }

In the `while' loop, the initialization is done outside the loop and the updating is done inside the loop. Apart from these differences, the working of the `while' loop is similar to that of the `for' loop.

 `do-while' loop

The `do-while' loop is a bit different from the other two loop statements we have seen. Let us check its syntax to understand the difference.

 initialize loop; do { statements of the loop; }while(check condition);

As we can see, there is no condition checking before the loop starts. So, the loop will run at least once irrespective of the initial value and the testing condition. Consider the following example:

 int count; count=20; do { // some statements }while(count<10);

In this example, the initial value of count is 20. Since there is no check condition before the program enters the loop, the statements inside the loop will execute. After this, the condition will be checked. Since 20 is not less than 10, the condition will become false and the loop will stop. Compare this with the `for' and `while' loops. They will not execute a single time if the initial value makes the condition false.

Apart from these three loop statements, there is one more special loop statement in C#, known as the `foreach' loop. We are going to lean about it in the next section.