Loops in C#
Looping in a programming language is a way to execute a statement or a set of statements multiple times, depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.
Types of Loops in C#
Loops are mainly divided into two categories:
- Entry Controlled Loops
- Exit Controlled Loops
1. Entry Controlled Loops
The loops in which the condition to be checked before entering the loop body are known as Entry Controlled Loops.
Example: while, for
1.1 while Loop : The test condition is given at the beginning of the loop and all statements are executed till the given Boolean condition is satisfied. When the condition becomes false, the control will be out of the while loop.
Syntax:
while (boolean condition)
{
loop statements...
}
Flowchart:

Example:
using System;
class Geeks
{
public static void Main()
{
int x = 1;
// Exit when x becomes greater than 4
while (x <= 4)
{
Console.WriteLine("GeeksforGeeks");
// Increment the value of x for next iteration
x++;
}
}
}
Output
GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
1.2 for loop : The for loop is a control flow statement that allows you to execute a block of code repeatedly for a fixed number of iterations. Itâs commonly used when the number of repetitions is known beforehand.
Syntax:
for (loop variable initialization ; testing condition; increment / decrement)
{
// statements to be executed
}
Flowchart:

Note:
Initialization part is evaluated only once when the for loop starts.
Example:
using System;
class Geeks
{
public static void Main()
{
// for loop begins when x=1 and runs till x <= 4
for (int x = 1; x <= 4; x++)
Console.WriteLine("GeeksforGeeks");
}
}
Output
GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
2. Exit Controlled Loops
The loops in which the condition is checked after the loop body is known as Exit Controlled Loops.
Note: In Exit Controlled Loops, loop body will be evaluated for at-least one time as the testing condition is present at the end of loop body.
Example: do-while Loop
2.1 do-while loop : The do while loop is similar to while loop with the only difference that it checks the condition after executing the statements, i.e it will execute the loop body one time for sure because it checks the condition after executing the statements.
Syntax :
do
{
statements..
} while (condition);
Flowchart:

Example:
using System;
class Geeks
{
public static void Main()
{
int x = 21;
do
{
// The line will be printed even if the condition is false
Console.WriteLine("GeeksforGeeks");
x++;
}
while (x < 20);
}
}
Output
GeeksforGeeks
Loop Variations and Control Statements
1. Infinite Loops
The loops in which the test condition does not evaluate false ever tend to execute statements forever until an external force is used to end it and thus they are known as infinite loops.
Example:
using System;
class Geeks
{
public static void Main()
{
// The statement will be printed infinite times
for(;;)
Console.WriteLine("This is printed infinite times");
}
}
Output:
This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
..........
2. Nested Loops
When loops are present inside the other loops, it is known as nested loops.
Example:
using System;
class Geeks
{
public static void Main()
{
// loop within loop printing GeeksforGeeks
for (int i = 2; i < 3; i++)
for (int j = 1; j < i; j++)
Console.WriteLine("GeeksforGeeks");
}
}
Output
GeeksforGeeks
3. Continue Statement
Continue statement is used to skip over the execution part of loop on a certain condition and move the flow to next updation part.
Flowchart:

Example:
using System;
class Geeks
{
public static void Main()
{
// GeeksforGeeks is printed only 1 times
for (int i = 1; i < 3; i++)
{
if (i == 2)
continue;
Console.WriteLine("GeeksforGeeks");
}
}
}
Output
GeeksforGeeks