In reality, there are repetitive tasks we have to do and this is too often, and it is quite hectic to do the same job over and over again.
In the case of programming, there are situations in which a programming statement has to be written, a logical set several times executed or a message with slight number variations displayed. It will clearly be time-consuming and less efficient to do this type of job manually.
Loop is a concept used several times for execution of a single statement or collection of statements depending on the condition in almost all programming language.
The loops in C # are based essentially on two different kinds. The following are:
When the loop condition is checked right at the beginning of the loop body and before the loop block is executed, such loop types are called entry controlled loops. There are two kinds of C # entry-controlled looping statements. The following are:
While Loop in C#
With this loop statement, the test condition is given at the very beginning of the loop block before the execution is carried through. The iteration is continued until the condition becomes false.
Syntax:
while (boolean conditional expression)
{
// block of loop statements
}
Example:
using System;
class IterateWithWhile
{
public static void Main()
{
int i = 1;
while (i <= 5)
{
Console.WriteLine(" Hello UK Academe "+i);
i++;
}
}
}
Output:
Hello UK Academe 1
Hello UK Academe 2
Hello UK Academe 3
Hello UK Academe 4
Hello UK Academe 5
For Loop in C#
The for loop is another C#-controlled entry loop statement that does also perform the iteration in a programme, but with a different syntax. In one single line, the counter variable is initialized, after that looping condition is checked, the increase / decrement is made, and each of these expressions is separated by a semi-colon.
Syntax:
for (initialization of counter variable; test the condition of looping; increment / decrement)
{
// block of loop statements
}
Example:
using System;
class IterationWithFor
{
public static void Main()
{
for (int i = 1; i <= 5; i++)
Console.WriteLine("Hello UK Academe "+i);
}
}
Output:
Hello UK Academe 1
Hello UK Academe 2
Hello UK Academe 3
Hello UK Academe 4
Hello UK Academe 5
Once the loop condition is checked at the end of the loop and just after the loop block is executed (at least once), these loop conditions are referred to as exit control loops. C# only gives a single exit-controlled loop declaration, which is do-while loop.
do while Loop in C#
This loop is similar to the while loop, except that the loop condition at the end of the loop block is checked, which means that the loop block at least once is executed and evaluated regardless of the condition.
Syntax:
do
{
// block of loop statements
} while (condition);
Example:
using System;
class IterateWithDoWhile
{
public static void Main()
{
int i = 11;
do
{
Console.WriteLine(" Hello UK Academe "+i);
i++;
}
while (i < 10);
}
}
Output:
Hello UK Academe 11
There are also other types of loops in which the condition will not be false and the iteration will continue until the external agent or an external potential is used to forcefully stop this endless iteration.
These loop kinds are known as endless loops. Here's an example of a loop that is endless in C#.
Example:
using System;
class GoWithInfiniteLoop
{
public static void Main()
{
for( ; ; )
Console.WriteLine(" Hello Uttarakhand");
}
}
Output:
Hello Uttarakhand
Hello Uttarakhand
Hello Uttarakhand
.
.
.