Ana Sayfa Hakkımda Dersler Blog MikrofondaOn The Mic İletişim Projeler Yakında
DerslerLessons TEMEL · C#

C# Temelleri — While Döngüsü C# Basics — While Loop

7 dk okuma 7 min read · Emre Ulutabak
1
While nedir? What is while?

Bazı işler kaç kez tekrarlanacağı baştan belli olmadan yapılır. Mesela bir oyunda canın sıfıra düşene kadar devam edersin.

İşte böyle durumlarda while kullanılır. Koşul doğru kaldığı sürece döngü çalışmaya devam eder.

Some tasks repeat without knowing the exact number beforehand. For example, in a game you keep going until your health reaches zero.

That is where while is used. The loop keeps running as long as the condition remains true.

csharp
int enerji = 3;

while (enerji > 0)
{
    Console.WriteLine("Devam ediyorsun...");
    enerji--;
}
💡
While, 'koşul doğru oldukça devam et' mantığıyla çalışır. While works with the idea of 'keep going as long as the condition is true'.
2
For ile farkı Difference from for

for genelde kaç kez çalışacağı belli olan tekrarlar için uygundur. while ise daha çok koşula bağlı tekrarlar için kullanılır.

Yani sayı odaklı tekrar varsa for, durum odaklı tekrar varsa while daha doğal gelir.

for is usually better when you know how many times something should repeat. while is better when repetition depends on a condition.

So if the repetition is count-based, use for; if it is state-based, while often feels more natural.

3
Koşul mantığı Condition logic

While döngüsünde her tur başlamadan önce koşul tekrar kontrol edilir. Koşul yanlış olursa döngü durur.

Bu yüzden koşulu etkileyen değişkenin döngü içinde güncellenmesi çok önemlidir.

In a while loop, the condition is checked before each round begins. If the condition becomes false, the loop stops.

That is why it is very important to update the variable that affects the condition inside the loop.

csharp
int sayac = 1;

while (sayac <= 5)
{
    Console.WriteLine(sayac);
    sayac++;
}
💡
Koşulu değiştiren şey yoksa while sonsuza kadar sürebilir. If nothing changes the condition, a while loop can continue forever.
4
Sonsuz döngü riski Risk of infinite loop

Yeni başlayanların en sık yaptığı hatalardan biri, sayaç ya da koşulu güncellemeyi unutmaktır.

Böyle olunca döngü hiç bitmez. Buna sonsuz döngü denir ve programı kilitleyebilir.

One of the most common beginner mistakes is forgetting to update the counter or the condition.

When that happens, the loop never ends. This is called an infinite loop and it can freeze the program.

csharp
int i = 1;

while (i <= 5)
{
    Console.WriteLine(i);
}
💡
Yukarıdaki örnekte i hiç değişmediği için döngü bitmez. In the example above, i never changes, so the loop never ends.
5
Altın kurallar Golden rules

While döngüsü çok kullanışlıdır ama en güvenli şekilde yazılması gerekir. Küçük bir kontrol hatası bile tüm akışı bozabilir.

A while loop is very useful, but it should be written carefully. Even a small control mistake can break the entire flow.

💡
Koşulu ve koşulu değiştiren satırı birlikte düşün. Biri varsa diğeri de olmalı. Think about the condition and the line that changes it together. If one exists, the other should too.
💡
Kaç kez döneceği belliyse while yerine for kullanmak daha temiz olabilir. If the number of repetitions is known, using for instead of while may be cleaner.
💡
Sonsuz döngü ihtimalini her zaman aklında tut. Always keep the possibility of an infinite loop in mind.
MİNİ QUIZ MINI QUIZ
Aşağıdakilerden hangisi doğrudur? Which of the following is correct?
while döngüsü koşul yanlışken çalışır A while loop runs while the condition is false
while döngüsü koşul doğru kaldığı sürece çalışır A while loop runs as long as the condition stays true
while içinde değişken güncellenemez Variables cannot be updated inside while
while sadece metinlerle kullanılır While is only used with strings