Home About Lessons Blog On The Mic Contact Projects
Lessons BASIC · C#

C# Basics — If / Else

7 min read · Emre Ulutabak
1
What is a decision structure?

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.");
}
💡
An if block means 'if this condition is true, do this'.
2
Why is it needed?

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
The logic of If / Else

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.");
}
💡
The program always chooses one of two paths: if the condition is true, it runs if; otherwise, it runs else.
4
Using else if

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ı");
}
💡
In else if chains, the first true condition runs; the rest are skipped.
5
Golden rules

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:

💡
Write conditions as clearly as possible. The reader should immediately understand what is being checked.
💡
If you are using too many nested if blocks, consider simplifying the logic.
💡
Do not leave ambiguity in condition names and variable names. Names like isLoggedIn or hasAccess are much easier to read.
MINI QUIZ
Which of the following is correct?
The else block runs when the condition is true
if can only be used with numbers
else if is used to check multiple possibilities in order
Curly braces cannot be used in an if block