Hayatta bazı işleri tekrar tekrar yaparsın. Merdiven çıkarken her basamağa aynı hareketle çıkarsın. Tek fark, bunu bir kez değil birçok kez yapmandır.
Yazılımda da aynı mantık vardır. Bir işlemi tekrar tekrar yapmak gerektiğinde her satırı elle yazmak yerine döngü kullanılır.
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!");
}
10 kez ekrana yazı yazdırmak istiyorsan bunu 10 satırla da yapabilirsin. Ama bu hem sıkıcıdır hem de kodu büyütür.
For döngüsü sayesinde tekrar eden işleri daha kısa, daha okunur ve daha yönetilebilir şekilde yazarsın.
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.
Bir for döngüsü genelde üç parçadan oluşur: başlangıç, koşul ve artış.
Yani önce sayaç oluşturulur, sonra döngünün devam edip etmeyeceği kontrol edilir, ardından her turda sayaç güncellenir.
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);
}
Döngülerdeki sayaç değişkeni çoğu zaman i olur. Bu değişken tur sayısını takip eder.
Aslında i'nin özel bir gücü yoktur; sadece geleneksel bir isimdir. İstersen başka isim de verebilirsin ama kısa ve sade olması tercih edilir.
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}");
}
Döngüler güçlüdür ama dikkatsiz kullanılırsa hata üretir. Özellikle sonsuz döngü ve yanlış sayaç mantığı çok yaygın problemler oluşturur.
Loops are powerful, but if used carelessly they can cause errors. Infinite loops and incorrect counter logic are especially common problems.