C# | loops

A loop in programming languages ​​is a way to execute a statement or a series of statements multiple times depending on the outcome of a condition that is evaluated to execute the statement. The result condition must be true for a statement to execute within the loop. Loops fall into two main categories. Input Control Loop: A loop in which the condition to be tested is at the beginning of the loop body is called an input control loop. The while loop and for loop are input-controlled loops.
1. while loop: The while loop test condition is specified at the beginning of the loop, and all statements are executed until the specified Boolean condition is met. When the condition becomes false, control exits the while loop.
while (boolean condition)
{
   loop statements...
}

whle loop

//below is C# program to illustrate while loop
using System;
class whileLoopDemo
{
    public static void Main()
    {
        int x = 1;
 
        // Exit when x becomes greater than 4
        while (x <= 3)
        {
            Console.WriteLine("Codeconfig.in");
 
            // Increment the value of x for
            // next iteration
            x++;
        }
    }
}
Output:
Codeconfig.in
 Codeconfig.in
 Codeconfig.in

2. for loop: A for loop has similar functionality to a while loop, but the syntax is different. A for loop is recommended when you know in advance how often you want to execute the loop statement. Initializing the loop variable, the condition being tested, and incrementing/decrementing the loop variable are all done on a single line within the for loop, providing a shorter and easier to debug loop structure.

for (loop variable initialization; testing condition. 
                              increment / decrement)
{    
    // statements to be executed
}

1. Initialize loop variables: The expressions/variables that control the loop are initialized here. This is the starting point for the for loop. You can use variables that have already been declared, or you can declare variables only locally to the loop.

2. Test condition: Test condition for executing a loop statement. Used to test loop exit conditions. Must return a boolean value ‘true’ or ‘false’. When the condition becomes false, control exits the loop and the for loop ends.

3. Increment / Decrement: The loop variable is incremented/decremented according to the request and the controller returns to the test state. Note: The initialization part is evaluated only once, at the beginning of the for loop.

// C# program to illustrate for loop.
using System;
class forLoopDemo
{
    public static void Main()
    {
        // for loop begins when x=1
        // and runs till x <=4
        for (int x = 1; x <= 5; x++)
            Console.WriteLine("Codeconfig.in");
    }
}
Output:
Codeconfig.in
 Codeconfig.in
 Codeconfig.in

Codeconfig.in
 Codeconfig.in

Exit Controlled Loops: A loop in which a test condition exists at the end of the loop body is called as Exit controllable loop. do-while is an output of Exit control loop.

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.

1. do-while loop do-while loop A do-while loop is similar to a while loop; the only difference is that it checks a condition after executing a statement. The condition is checked after the statement is executed, so the loop body is executed exactly once.

do
{
    statements.
} while (condition);

// C# program to illustrate do-while loop
using System;
class dowhileloopDemo
{
    public static void Main()
    {
        int x = 21;
        do
        {
            // The line will be printed even
            // if the condition is false
            Console.WriteLine("Codeconfig.in");
            x++;
        }
        while (x < 20);
    }
}
Output:
Codeconfig.in

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.

// C# program to demonstrate infinite loop
using System;
class infiniteLoop
{
    public static void Main()
    {
        // The statement will be printed
        // infinite times
        for(;;)
        Console.WriteLine("Ahh this is printed infinite times.");
    }
}
Output:
Ahh this is printed infinite times.
Ahh this is printed infinite times.
Ahh this is printed infinite times.
Ahh this is printed infinite times.
Ahh this is printed infinite times.
Ahh this is printed infinite times.
Ahh this is printed infinite times.
..........

Nested Loops: When loops are present inside the other loops, it is known as nested loops.

// C# program to demonstrate nested loops
using System;
class nestedLoops
{
    public static void Main()
    {
        // loop within loop printing CodeConfig
       for(int i = 2; i < 3; i++)
             for(int j = 1; j < i; j++)
                 Console.WriteLine("CodeConfig.in");
    }
}
Output:
CodeConfig.in

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:

C#Continue-statement.

// C# program to demonstrate continue statement
using System;
class CodeConfig
{
    public static void Main()
    {   
        // codeconfig is printed only 2 times
        // because of continue statement
        for(int i = 1; i < 3; i++)
        {
            if(i == 2)
              continue;
            
            Console.WriteLine("CodeConfig.in");
        }
    }
Output:
CodeConfig.in
In this article we tried to explain C# | loops with Examples. I hope you enjoyed reading this article. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such articles.