C# For Loop

CodeWithHonor
2 min readDec 20, 2022
title
For Loop

The for loop in C# allows you to repeatedly execute a block of code a specific number of times. The syntax for a for loop is as follows:

for (initialization; condition; iteration)
{
// code to execute as long as condition is true
}

The initialization statement is executed before the loop starts, and is typically used to initialize a loop counter. The condition is checked before each iteration of the loop, and if it is true, the code inside the loop is executed. The iteration statement is executed after each iteration of the loop, and is typically used to increment or decrement the loop counter.

For example, consider the following code snippet:

for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}

In this example, the for loop will execute 10 times, with the value of i being printed to the console each time. On the first iteration, i is initialized to 0, the condition i < 10 is true, and the code inside the loop is executed. On the second iteration, i is incremented to 1, and the condition is still true, so the code inside the loop is executed again. This process continues until i is incremented to 10, at which point the condition i < 10 is false and the loop is exited.

Breaking Out of a Loop

--

--