Loops in C#
Loops in C#
Depending on the outcome of the condition to be tested in order to execute statements, looping in a programming language is a way to execute a statement or a series of statements many times. To execute statements inside loops, the resulting condition should be true.
Loops are specifically divided into two classifications:
Entry Controlled Loops:
Entry Controlled Loops are the loops in which the condition to be checked is present at the beginning of the body of the loop.
Eg.- while loop and for loop
While loop
At the beginning of the loop, the test condition is given and all statements are executed until the given boolean condition satisfies the control is out of the while loop when the condition becomes false.
Syntax:
while (boolean condition)
{
loop statements...
}
Example-
using System;
class whileLoopDemo
{
public static void Main()
{
int x = 1;
// Exit when x becomes greater than 2
while (x <=2)
{
Console.WriteLine("welcome to my world");
// Increment the value of x for
// next iteration
x++;
}
}
}
Output :
Welcome to my world
Welcome to my world
For loop
- For loop, but with different syntax, it has similar features as for while loop. When the number of loop statements to be executed is specified beforehand, loops are favoured.
- The initialization of the loop variable, condition to be evaluated, and increment / decrement of the loop variable is performed in one line for the loop, providing a shorter, easier to debug looping structure.
Syntax -
for (loop variable initialization ; testing condition; increment / decrement)
{
// statements to be executed
}
1. Loop variable initialization:
- This is where the expression / variable running the loop is initialised. This is the loop's starting point.
- An already declared variable can be used or a variable can be declared, local to loop only.
2. Testing Condition:
- The evaluation condition used to execute loop statements. It is used for checking the state of a loop 's exit.
- A true or false Boolean value must be returned. When the state becomes incorrect, the power is out of the loop and ends with the loop.
3. Increment / Decrement:
According to the criterion, the loop variable is incremented / decremented and the control then shifts again to the test state.
Note: The initialization segment will only be evaluated once when the for loop begins.
// C# program to illustrate for loop.
using System;
class forLoopDemo
{
public static void Main()
{
// for loop begins when x=1
// and runs till x <=2
for (int x = 1; x <=2; x++)
Console.WriteLine("Welcome to my house");
}
}
Output-
Welcome to my house
Welcome to my house
- Exit Controlled Loops:
- Exit Controlled Loops are the loops in which the testing condition is present at the end of the body of the loop.
- Do-while is an exit controlled loop.
Note: In Exit controlled Loops, as the test condition is present at the end of the loop body, the loop body is checked at least once.
1. do-while loop :
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);
using System;
class dowhileloopDemo
{
public static void Main()
{
int x =12;
do
{
// The line will be printed even
// if the condition is false
Console.WriteLine("welcome to my city");
x++;
}
while (x <11);
}
}
Output-
welcome to my city
-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 infiniteLoop
{
public static void Main()
{
// The statement will be printed
// infinite times
for(;;)
Console.WriteLine("Welcome to my house");
}
}
Output-
Welcome to my house
Welcome to my house
Welcome to my house
.
.
.
and so on
-Nested Loops:
When loops are present inside the other loops, it is known as nested loops.
Example:
using System;
class nestedLoops
{
public static void Main()
{
// loop within loop to print Welcome to my world
for(int i = 2; i < 3; i++)
for(int j = 1; j < i; j++)
Console.WriteLine("Welcome to my world");
}
}
Output:
Welcome to my world