Friday, 17 August 2018

C#: for loop

‘for’ statement provides a compact way to iterate over a range of values.

Syntax
    for(initialization; condition; update){
        //statements to be executed
    }

‘for’ loop execute the statements until the condition evaluates to false.

Initialization section executed only once, at the starting of loop.
Condition section evaluated every time, before executing the loop statements
Update section executes immediately after every execution of loop finishes.

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        for(int var1 = 0; var1 < 10; var1++)
        {
            Console.WriteLine(var1);
        }
    }
}

Output
0
1
2
3
4
5
6
7
8
9


Previous                                                 Next                                                 Home

No comments:

Post a Comment