A situation may arise during coding where you need to repeat the block of code for some number of times then the loops present in C# will come into play.
Loops allow us to execute a block of code or statement or group of statements as many times according to the user’s need. The condition is checked every time the loop is repeated. It is done by evaluating a given condition for true and false.
If true, the loop statement are repeated, if false then the loop is skipped.
Two Types of Loops in C Programming.
1. Entry controlled loop:
In entry control loops, the conditional is checked first and then the block of code is executed. It is also called a pre-checking loop.
There are two types of entry control loop:
- for loop.
- while loop.
2. Exit controlled loop:
In exit control loop the block of code is executed first and then the condition is checked. It is also called a post-checking loop.
There is only one type loop in the exit control loop.
- do-while loop.
C# While Loop
A while loop is an entry-control loop that evaluates the condition before executing the block of the loop. If the condition is true then the body of the loop is executed else the loop will be skipped. The loop continues until the condition stated found to be false.
The syntax for while loop
in C#:
1 2 3 4 | while(condition) { //block of code to be executed } |
You should use a while loop, where the exact number of iterations is not known but the loop termination condition, is known.
For example and flowchart of while loop
, click here
C# do…while loop
As we know from the above loop that if the condition is found false then the loop will not be executed but what if we want the loop to execute once (like the menu drive program) even if the condition is found to be false. So in this kind of situation do-while loop comes into play.
It is the same as the while loop where the loop is terminated on the basis of the test condition. The main difference is that the do-while loop checks the condition at the end of the loop which allows the do-while to execute the loop at least once.
The syntax for do-while loop
in C#:
1 2 3 | do{ //statements.. }while (condition); |
You should use do while loop if the code needs to be executed at least once.
For example and flowchart of do-while loop
, click here
C# for Loop
The for loop is an entry-control loop as the condition is initially checked. It has the most efficient structure than the other two loops. The loop allows the programmer to write the execution steps together in a single line and also the specific number of times the execution is to be iterated.
It has three computing steps as shown in the syntax below.
- initialization: The first step is the initialization of the variable and is executed only once. And need to end with a semicolon(
;
). - condition: Second is condition check, it checks for a boolean expression. If true then enter the block and if false exit the loop. And need to end with a semicolon(
;
). - Increment or Decrement: The third one is the increment or decrement of the variable for the next iteration. Here, we do not need to use the semicolon at the end.
The syntax for for loop
in C#:
1 2 3 4 | for(initialization; condition; Increment/Decrement) { // Statements } |
You should use for a loop when the number of iterations is known beforehand, i.e. when the number of times the loop body is needed to be executed is known.
For example and flowchart of for loop
, click here
C# Nested Loops
A combination of loops where one loop is inside the other loop is called a nested loop. C# allow nesting any of the above loops.
Syntax:
The syntax for a nested for loop
statement in C#:
1 2 3 4 5 6 7 8 9 | for (initialization1; condition1; increment/decrement) { for (initialization2; condition2; increment/decrement) { //inner statement } //outer statement } |
The syntax for a nested while loop
statement in C#:
1 2 3 4 5 6 7 8 9 | while(condition1) { while(condition2) { //inner statement } //outer statement } |
The syntax for a nested do-while loop
statement in C#:
1 2 3 4 5 6 7 8 | do { //outer statement do { //inner statement } while(condition1); } while(condition2); |
You can use loops inside another for any number of times as required. You can also use any type of loop inside any other type of loops. For example, you can use while loop inside for loop or vice versa.
Example of C# nested loop
We will see an example of use of nested for loop in C# to print a star pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; namespace Loops { class NestedLoopProgram { public static void Main(string[] args) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { Console.Write("* "); } Console.WriteLine(); } } } } |
Output:
1 2 3 4 5 | * * * * * * * * * * * * * * * |
The Infinite Loop
As the name suggests, the infinite loop is a forever executing loop. The infinite loop repeats indefinitely and the condition never becomes false. It is also known as an indefinite loop or an endless loop.
We can use one of the Loops in C# to turn it into infinite loop.
infinite ‘for’ loop
1 2 3 4 | for(; ;) { //body of the loop } |
Example: of infinite for loop in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System; namespace Loops { class InfiniteLoop { static void Main(string[] args) { for (; ; ) { Console.WriteLine("Executed Forever."); } } } } |
The above program will run forever. The compiler assumes the condition to be true if the condition is absent in the loop.