Table of Contents

    Understanding the for Loop in C#: Syntax and Usage Explained

    Understanding the for Loop in C#: Syntax and Usage Explained

    for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

    Syntax

    The syntax of a for loop in C# is −

    for ( init; condition; increment ) {
       statement(s);
    }

    Here is the flow of control in a for loop −

    • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

    • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

    • After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

    • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again testing for a condition). After the condition becomes false, the for loop terminates.

    Flow Diagram

    for loop in C#

    Example

     
    using System;
    
    namespace Loops {
       class Program {
          static void Main(string[] args) {
             
             /* for loop execution */
             for (int a = 10; a < 20; a = a + 1) {
                Console.WriteLine("value of a: {0}", a);
             }
             Console.ReadLine();
          }
       }
    } 
    

    When the above code is compiled and executed, it produces the following result −

    value of a: 10
    value of a: 11
    value of a: 12
    value of a: 13
    value of a: 14
    value of a: 15
    value of a: 16
    value of a: 17
    value of a: 18
    value of a: 19

    Infinite for Loop

    Be careful with infinite loop. It will be an infinite loop if for loop does not contain initialization, condition or steps part. Also, make sure that conditional expression will return false at some point of time to stop the looping.

    
    for (  ;  ; )
    {
        Console.Write(1);
    }
    

    Output

    
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.....
    

    The control variable for the for loop can be of any numeric data type, such as double, decimal, etc.

    
    for (double d = 1.01D; d < 1.10; d+= 0.01D)
    {
        Console.WriteLine("Value of i: {0}", d);
    }
    

    Output

    
    Value of i: 1.01 
    Value of i: 1.02 
    Value of i: 1.03 
    Value of i: 1.04 
    Value of i: 1.05 
    Value of i: 1.06 
    Value of i: 1.07 
    Value of i: 1.08 
    Value of i: 1.09
    

    break in for loop

    You can also exit from a for loop by using the break keyword.

    Prohram

    
    for (int i = 0; i < 10; i++)
    {
        if( i == 5 )
            break;
    
        Console.WriteLine("Value of i: {0}", i);
    }
    

    Output

    
    Value of i: 1.01 
    Value of i: 1.02 
    Value of i: 1.03 
    Value of i: 1.04 
    Value of i: 1.05 
    Value of i: 1.06 
    Value of i: 1.07 
    Value of i: 1.08 
    Value of i: 1.09
    

    Nested for Loop

    C# allows a for loop inside another for loop.

    Prohram

    
    for (int i = 0; i < 10; i++)
    {
        for(int j =i; j< 10; j++)
            Console.WriteLine("Value of i: {0}, J: {1} ", i,j);
    }
    

    Output

    
    Value of i: 0 , j: 0 
    Value of i: 0 , j: 1 
    Value of i: 0 , j: 2 
    Value of i: 0 , j: 3 
    Value of i: 0 , j: 4 
    Value of i: 0 , j: 5 
    Value of i: 0 , j: 6 
    Value of i: 0 , j: 7 
    Value of i: 0 , j: 8 
    Value of i: 0 , j: 9 
    Value of i: 1 , j: 1 
    Value of i: 1 , j: 2 
    Value of i: 1 , j: 3 
    Value of i: 1 , j: 4 
    Value of i: 1 , j: 5 
    Value of i: 1 , j: 6 
    Value of i: 1 , j: 7 
    Value of i: 1 , j: 8 
    Value of i: 1 , j: 9 
    Value of i: 2 , j: 2 
    Value of i: 2 , j: 3 
    Value of i: 2 , j: 4 
    Value of i: 2 , j: 5 
    Value of i: 2 , j: 6 
    Value of i: 2 , j: 7
    
    Points to Remember :
    1. The for loop executes the block of code repeatedly.
    2. The for loop has three steps: initialization, condition and increment/decrement.
    3. The for loop can use the control variable of any numeric data type.
    4. Use break keyword to stop the execution and exit from for loop.
    5. Nested for loop is allowed in C#.