Looping Statements in C#

Looping Statements in C#

C# has four looping statements: while, do-while, for and for each. Each of them provides ways for you to specify that a group of statements should be executed until some condition is satisfied.

The while Loop

The while loop is easy to understand. All of the statements inside the braces are executed repeated as long as the condition is true.

i = 0;

while ( i < 100)

{

x = x + i++;

}

Since the loop is executed as long as the condition is true, it is possible that such a loop may never be executed at all, and of course, if you are not careful, that such a while loop will never be completed.

The do-while Statement

The C# do-while statement is quite analogous, except that in this case the loop must always be executed at least once, since the test is at the bottom of the loop:

i = 0;

do {

x += i++;

}

while (i < 100);

The for Loop

The for loop is the most structured. It has three parts: an initializer, a condition, and an operation that takes place each time through the loop.

Each of these sections are separated by semicolons:

for (i = 0; i< 100; i++) {

x += i;

}

Let's take this statement apart:

for (i = 0; //initialize i to 0

i < 100 ; //continue as long as i < 100

i++) //increment i after every pass

In the loop above, i starts the first pass through the loop set to zero. A test is made to make sure that i is less than 100 and then the loop is executed. After the execution of the loop, the program returns to the top, increments i and again tests to see if it is less than 100. If it is, the loop is again

executed.

Note that this for loop carries out exactly the same operations as the while loop illustrated above. It may never be executed and it is possible to write a for loop that never exits.

Declaring Variables as Needed in For Loops

One very common place to declare variables on the spot is when you need an iterator variable for a for loop. You can simply declare that variable right in the for statement, as follows:

for (int i = 0; i < 100; i++)

Such a loop variable exists or has scope only within the loop. It vanishes once the loop is complete. This is important because any attempt to reference such a variable once the loop is complete will lead to a compiler error message. The following code is incorrect:

for (int i =0; i< 5; i++) {

x[i] = i;

}

//the following statement is in error

//because i is now out of scope

System.out.println(“i=” + i);

Commas in for Loop Statements

You can initialize more than one variable in the initializer section of the C# for statement, and you can carry out more than one operation in the operation section of the statement. You separate these statements with commas:

for (x=0, y= 0, i =0; i < 100; i++, y +=2)

{

x = i + y;

}

It has no effect on the loop's efficiency, and it is far clearer to write:

x = 0;

y = 0;

for ( i = 0; i < 100; i++)

{

x = i + y;

y += 2;

}

It is possible to write entire programs inside an overstuffed for statement using these comma operators, but this is only a way of obfuscating the intent of your program.

 

 

Shashi Ray