In life, you repeat some actions again and again. When climbing stairs, you make the same movement on every step. The only difference is that you do it many times.
Software works the same way. When an operation needs to be repeated, instead of writing the same code manually, you use a loop.
for (int i = 1; i <= 3; i++)
{
Console.WriteLine("Merhaba!");
}
If you want to print something 10 times, you could write 10 lines. But that is both repetitive and messy.
With a for loop, you can write repetitive tasks in a shorter, cleaner, and more manageable way.
A for loop usually has three parts: initialization, condition, and increment.
First, a counter is created. Then the condition is checked. After each round, the counter is updated.
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
In loops, the counter variable is often called i. It tracks how many times the loop has run.
There is nothing magical about i; it is just a common convention. You can name it differently, but short and simple names are usually preferred.
for (int sayi = 1; sayi <= 5; sayi++)
{
Console.WriteLine($"Tur: {sayi}");
}
Loops are powerful, but if used carelessly they can cause errors. Infinite loops and incorrect counter logic are especially common problems.