C sharp - for Loop

 

The for loop is used when you know in advance how many times you want to repeat a block of code.

Syntax:

for (initialization; condition; increment)
{
// Code to execute
}

Example:

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

Explanation:

  • int i = 0 → Start at 0

  • i < 5 → Continue looping while i is less than 5

  • i++ → Increase i by 1 each time