Programming References and tutorials for Developers.

C# Looping Statement

By The Saint on Saturday, March 07, 2009

comments (0)

Filed Under:

C# just like Java and C++ it also provides looping statements.
Common looping statements:

for loop
while
do while

Three components of a looping statement:
1. Initial value
2. Condition
3. Change of state

while loop
A while loop executes a statement, or a block of statements wrapped in curly braces, repeatedly until the condition specified by the Boolean expression reach the false condition.

The boolean expression is evaluated before any code in the following block has executed. When the boolean expression evaluates to true, the statements will execute. Once the statements have executed, control returns to the beginning of the while loop to check the boolean expression again.

Syntax in creating a while (expression) loop.

while(expression)
Statements...
}

Example:

int counter=0; //initial value

while(counter<5) style="font-weight: bold;">do while Loop
A do while loop is just like a while loop, except that it checks its condition at the end of the loop. This means that the do loop is guaranteed to execute at least one time. On the other hand, a while loop evaluates its Boolean expression at the beginning and there is generally no guarantee that the statements inside the loop will be executed, unless you program the code to explicitly do so.

Syntax in creating a do while (expression) loop.

do{
Statement...
}while(expression)

Example:

int counter=0;

do{
Console.WriteLine(counter);
counter++;
}while(counter<5);

Program Output:
0
1
2
3
4

The for Loop

A for loop works like a while loop, except that the syntax of the for loop includes initialization and condition modification. for loops are appropriate when you know exactly how many times you want to perform the statements within the loop. The contents within the for loop parentheses hold three sections separated by semicolons (; ; ) { }.

The initializer list is a comma separated list of expressions. These expressions are evaluated only once during the lifetime of the for loop. This is a one-time operation, before loop execution. This section is commonly used to initialize an integer to be used as a counter.

Once the initializer list has been evaluated, the for loop gives control to its second section, the boolean expression. There is only one boolean expression, but it can be as complicated as you like as long as the result evaluates to true or false. The boolean expression is commonly used to verify the status of a counter variable.

When the boolean expression evaluates to true, the statements within the curly braces of the for loop are executed. After executing for loop statements, control moves to the top of loop and executes the iterator list, which is normally used to increment or decrement a counter. The iterator list can contain a comma separated list of statements, but is generally only one statement.

for(int counter = 0; counter<5; counter++)
{
Console.WriteLine(counter);
}

Program Output:
0
1
2
3
4