1. LOOPS USED IN C#.NET
Loops are used to execute one or more statements multiple times until a
specified condition is fulfilled. There are many loops in C# such as for
loop , while loop , do while loop etc.
Details about these are given as follows:
2. 1] FOR LOOP IN C#
The for loop executes one or more statements multiple times as long as
the loop condition is satisfied. If the loop condition is true , the body of
the for loop is executed. Otherwise , the control flow jumps to the next
statement after the for loop .
3. DIAGRAM
As seen from the above diagram, first the initialisation is done.
This declares and initialises the loop variables , if there are any.
Then the condition is evaluated . If it is true , loop body is
executed. If it is false , the control passes to the next statement
after the for loop body. After the loop body is executed, the
loop variables are updated. Then the condition is checked again
and the cycle continues .
4. SYNTAX AND OUTPUT OF FOR LOOP
The syntax of the for loop is given below:
for ( initialization, condition, increment )
{
// These statements will be executed if the condition evaluates to true
}
A program that demonstrates the for loop is as follows:
Source Code: Program that demonstrates for loop in C#
using System;
namespace LoopDemo
{
class Example {
static void Main(string[] args) {
Console.WriteLine(“First 10 Natural Numbers are: “);
for (int i = 1; i <= 10; i++)
Console.WriteLine(i);
}
}
}
The output of the above program is as
follows:
First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10
5. The while loop continuously executes one or
more statements as long as the loop condition
is satisfied. If the loop condition is true, the
body of the for loop is executed. Otherwise, the
control flow jumps to the next statement after
the while loop.
A diagram that demonstrates the flow in the
while loop is given as follows:
2] WHILE LOOP IN C#
6. As seen from the above diagram, first the
condition is checked. If the condition is true, the
loop body is executed. If the condition is false,
control passes to the next statement after the
loop body. The while loop may never run if the
condition is false the first time it is tested. The
control skips the loop and goes directly to the next
statement.
• The syntax of the while loop is given as follows:
• while (condition) { // These statements will be executed if the
condition evaluates to true }
7. WHILE LOOP EXAMPLE AND OUTPUT
SYNTAX
using System; namespace LoopDemo
{ class Example
{ static void Main(string[] args) {
int i = 1;
Console.WriteLine
("First 10 Natural Numbers are: ");
while (i <= 10)
{ Console.WriteLine( i );
i++;
}
}
}
}
OUTPUT
The output of the above program is as follows:
First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10
8. 3] DO WHILE LOOP IN C#
The do while loop executes one or
more statements multiple times as
long as the loop condition is
satisfied. It is similar to the while
loop but the while loop has the test
condition at the start of the loop
and the do while loop has the test
condition at the end of the loop. So
it executes at least once always.
A diagram that demonstrates the
flow in the do while loop is given as
follows:
9. As seen from the above diagram, the first time loop body is directly executed as the test
condition is at the bottom. The condition is checked. If it is true, the loop body is executed
again. If the condition is false, control passes to the next statement after the loop body.
SYNTAX
{ class Example
{ static void Main(string[] args) {
int i = 1;
Console.WriteLine("First 10 Natural Numbers are:
");
do
{
Console.WriteLine( i );
i++;
}
while (i <= 10); } } }
OUTPUT
First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10
10. 4] NESTED LOOPS IN C#
Nested loops are loop where one loop is situated
inside another. Nested loops can be created from
for loops, while loops and do while loops.
11. SYNTAX
namespace LoopDemo
{
class Example {
static void Main(string[] args) {
Console.WriteLine(“Nested for loop”);
for( int i = 0; i < 5; i++)
{
for(int j=0; j<=i; j++)
Console.Write(“*”);
Console.WriteLine();
}
}
}
}
OUTPUT
Nested for loop
*
**
***
****
*****
12. 5] FOR EACH LOOP IN C#
• The foreach loop executes one or more statements for all the
elements in an instance of the type
Systems.Collection.Generic.IEnumerable<T> or
Systems.Collection.IEnumerable interface.
As seen from the above diagram, first the initialisation is done. This declares and initialises the loop variables , if there are any. Then the condition is evaluated . If it is true , loop body is executed. If it is false , the control passes to the next statement after the for loop body. After the loop body is executed, the loop variables are updated. Then the condition is checked again and the cycle continues .