//
In programming, loops are essential constructs that allow you to execute a block of code repeatedly. This repetition can be based on a specific condition or for a predetermined number of times. One of the most commonly used loops in C# is the for loop.
The for loop is particularly useful when you know in advance how many times you want to repeat a block of code. It consists of three main parts: initialization, condition, and increment/decrement expressions. These components are enclosed within parentheses after the for keyword.
A for loop in C# follows this basic syntax:
for (initialization; condition; increment/decrement)
{
// Code to be executed repeatedly
}
- **Initialization**: This part is executed only once, before the loop starts. It's typically used to initialize a counter variable.
- **Condition**: Before each iteration of the loop, this condition is evaluated. If it evaluates to `true`, the loop body executes. If `false`, the loop terminates.
- **Increment/Decrement**: After each iteration of the loop body, this expression is executed. It's usually used to update the counter variable.
## Examples
Let's explore some practical examples to understand how `for` loops work in C#.
### Example 1: Basic Counting
Here's a simple example that prints numbers from 1 to 5:
```csharp
CodeBlock language="csharp">
{`
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
}
}
`}
</CodeBlock>
When you run this program, the output will be:
<OutputBlock>
{`1
2
3
4
5`}
</OutputBlock>
### Example 2: Counting Down
In this example, we'll count down from 5 to 1:
```csharp
CodeBlock language="csharp">
{`
using System;
class Program
{
static void Main()
{
for (int i = 5; i >= 1; i--)
{
Console.WriteLine(i);
}
}
}
`}
</CodeBlock>
The output will be:
<OutputBlock>
{`5
4
3
2
1`}
</OutputBlock>
### Example 3: Skipping Iterations
You can use the `continue` statement to skip an iteration of the loop. Here's an example that skips even numbers and prints only odd numbers from 1 to 10:
```csharp
CodeBlock language="csharp">
{`
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue;
}
Console.WriteLine(i);
}
}
}
`}
</CodeBlock>
The output will be:
<OutputBlock>
{`1
3
5
7
9`}
</OutputBlock>
### Example 4: Nested Loops
Nested loops are loops inside other loops. Here's an example that prints a multiplication table for numbers 1 to 3:
```csharp
CodeBlock language="csharp">
{`
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
Console.WriteLine($"{i} x {j} = {i * j}");
}
}
}
}
`}
</CodeBlock>
The output will be:
<OutputBlock>
{`1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9`}
</OutputBlock>
## What's Next?
Now that you've learned about `for` loops, the next step is to explore another type of loop: **while loops**. While loops are useful when you don't know in advance how many times a block of code needs to be executed and rely on a condition to determine when to stop.
Stay tuned for more tutorials on control flow structures in C#!