write a for loop in csharp

In C#, you can write a for loop using the following syntax:

main.cs
for (initialization; condition; increment/decrement)  
{  
    // code to be executed  
}  
92 chars
5 lines

The initialization statement is executed only once at the beginning of the loop. It is typically used to initialize the loop variable.

The condition statement is evaluated at the beginning of each iteration of the loop. If the condition is true, the loop executes; otherwise, it ends.

The increment/decrement statement is executed at the end of each iteration of the loop. It is typically used to increment or decrement the loop variable.

Here's an example of a for loop that prints the numbers from 1 to 10:

main.cs
for (int i = 1; i <= 10; i++)  
{  
    Console.WriteLine(i);  
}  
68 chars
5 lines

Output:

main.cs
1
2
3
4
5
6
7
8
9
10
21 chars
11 lines

related categories

gistlibby LogSnag