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

C# Temelleri — If / Else C# Basics — If / Else

7 dk okuma 7 min read · Emre Ulutabak
1
Karar yapısı nedir? What is a decision structure?

Hayatta sürekli karar verirsin. Hava yağmurluysa şemsiye alırsın. Açsan yemek yersin. Geç kaldıysan daha hızlı hazırlanırsın.

Yazılımda da aynı mantık vardır. Program bazı durumlara bakar ve ona göre farklı davranır. İşte bu yapıya karar yapısı denir.

In life, you constantly make decisions. If it is raining, you take an umbrella. If you are hungry, you eat. If you are late, you get ready faster.

Software works the same way. A program checks a condition and behaves differently depending on the result. This is called a decision structure.

csharp
bool yagmurYagiyor = true;

if (yagmurYagiyor)
{
    Console.WriteLine("Şemsiyeni al.");
}
💡
If bloğu, 'eğer bu koşul doğruysa bunu yap' demektir. An if block means 'if this condition is true, do this'.
2
Neden gerekli? Why is it needed?

Karar yapıları olmadan programlar herkese, her durumda aynı tepkiyi verirdi. Bu da yazılımı kör ve cansız hale getirirdi.

Bir giriş ekranı düşün: şifre doğruysa içeri almalı, yanlışsa hata vermeli. İşte bu farkı yaratan şey if / else mantığıdır.

Without decision structures, programs would react the same way to everyone and every situation. That would make software blind and lifeless.

Think of a login screen: if the password is correct, it should let the user in; if it is wrong, it should show an error. The thing that creates this difference is the if / else logic.

3
If / Else mantığı The logic of If / Else

if kısmı koşul doğruysa çalışır. else kısmı ise koşul yanlışsa devreye girer.

Bunu kapı örneğiyle düşün: kartın geçerliyse kapı açılır, değilse kapı kapalı kalır.

The if part runs when the condition is true. The else part runs when the condition is false.

Think of it like a door system: if your card is valid, the door opens; otherwise, it stays closed.

csharp
int yas = 17;

if (yas >= 18)
{
    Console.WriteLine("Giriş yapabilirsiniz.");
}
else
{
    Console.WriteLine("Giriş yapamazsınız.");
}
💡
Program her zaman iki yoldan birini seçer: koşul doğruysa if, yanlışsa else. The program always chooses one of two paths: if the condition is true, it runs if; otherwise, it runs else.
4
Else if kullanımı Using else if

Bazen iki seçenek yetmez. Not sistemini düşün: 90 üstü A, 70 üstü B, 50 üstü C gibi farklı durumlar olabilir.

Böyle durumlarda else if kullanılır. Böylece program birden fazla ihtimali sırayla kontrol eder.

Sometimes two options are not enough. Think about a grading system: above 90 is A, above 70 is B, above 50 is C.

In such cases, you use else if. This lets the program check multiple possibilities in order.

csharp
int not = 78;

if (not >= 90)
{
    Console.WriteLine("A");
}
else if (not >= 70)
{
    Console.WriteLine("B");
}
else if (not >= 50)
{
    Console.WriteLine("C");
}
else
{
    Console.WriteLine("Kaldı");
}
💡
Else if bloklarında ilk doğru olan koşul çalışır; alttakilere artık bakılmaz. In else if chains, the first true condition runs; the rest are skipped.
5
Altın kurallar Golden rules

Karar yapıları basit görünür ama temiz yazılmazsa kodu çabuk karmaşıklaştırır. Bu yüzden bazı temel kuralları erken öğrenmek çok faydalıdır:

Decision structures look simple, but if they are not written cleanly, they can quickly make code messy. That is why learning a few core rules early is very useful:

💡
Koşulları mümkün olduğunca açık yaz. Okuyan kişi neyi kontrol ettiğini hemen anlamalı. Write conditions as clearly as possible. The reader should immediately understand what is being checked.
💡
Çok fazla iç içe if kullanıyorsan mantığı sadeleştirmeyi düşün. If you are using too many nested if blocks, consider simplifying the logic.
💡
Koşul adlarında ve değişken isimlerinde belirsizlik bırakma. isLoggedIn, hasAccess gibi isimler daha okunur olur. Do not leave ambiguity in condition names and variable names. Names like isLoggedIn or hasAccess are much easier to read.
MİNİ QUIZ MINI QUIZ
Aşağıdakilerden hangisi doğrudur? Which of the following is correct?
else bloğu koşul doğruysa çalışır The else block runs when the condition is true
if sadece sayılarla kullanılabilir if can only be used with numbers
else if birden fazla ihtimali sırayla kontrol etmek için kullanılır else if is used to check multiple possibilities in order
Bir if bloğunda süslü parantez kullanılamaz Curly braces cannot be used in an if block