DEV Community

Sabin Sim
Sabin Sim

Posted on

06. C# (if / else Conditionals)

0️⃣ The Real Goal of This Lesson

Understanding how code flow splits based on conditions

After this lesson, you should be able to:

  • clearly explain why a specific block of code executed
  • understand the exact difference between if, else if, and else
  • ensure that only one branch runs when intended

1️⃣ What if / else Really Means (Plain English)

If (this condition is true)
    run this code
Otherwise
    run that code
Enter fullscreen mode Exit fullscreen mode

📌 Every condition must evaluate to a boolean value (true or false).


2️⃣ The Most Basic if / else (Run This)

using System;

class Program
{
    static void Main()
    {
        var userInput = Console.ReadLine();

        if (userInput == "ABC")
        {
            Console.WriteLine("Input is ABC");
        }
        else
        {
            Console.WriteLine("Input is NOT ABC");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Execution flow

  • userInput == "ABC" → produces a bool
  • trueif block runs
  • falseelse block runs

3️⃣ A Condition Is Just an Expression

Using string length as a condition

if (userInput.Length > 10)
{
    Console.WriteLine("Long input");
}
else
{
    Console.WriteLine("Short input");
}
Enter fullscreen mode Exit fullscreen mode

One key concept here

userInput.Length
Enter fullscreen mode Exit fullscreen mode
  • Length is a property
  • It’s information the string already has
  • Accessed using the dot (.) operator

📌 For now, it’s enough to remember:
👉 Strings have a length.


4️⃣ Why Breakpoints Are Better Than Console Output

Why not rely only on Console.WriteLine?

  • Console output ❌ noisy
  • Breakpoints ⭕ precise

Where to place breakpoints

Place one inside each branch:

if (userInput.Length > 10)
{
    Console.WriteLine("Long input"); // breakpoint
}
else
{
    Console.WriteLine("Short input"); // breakpoint
}
Enter fullscreen mode Exit fullscreen mode

What you’ll observe

  • Execution stops at exactly one location
  • That stop tells you which branch actually ran

👉 This is how you see control flow, not guess it.


5️⃣ More Than Two Paths → else if

Example requirement

  • length ≤ 3 → "Short answer"
  • length 4–9 → "Medium answer"
  • length ≥ 10 → "Long answer"

Correct structure

if (userInput.Length <= 3)
{
    Console.WriteLine("Short answer");
}
else if (userInput.Length < 10)
{
    Console.WriteLine("Medium answer");
}
else
{
    Console.WriteLine("Long answer");
}
Enter fullscreen mode Exit fullscreen mode

Evaluation order (important)

  1. First if is checked
  2. If false → else if is checked
  3. If still false → else runs

👉 Only one branch executes


6️⃣ A Very Common Mistake (Critical)

❌ Removing else

if (userInput.Length <= 3)
{
    Console.WriteLine("Short answer");
}

if (userInput.Length < 10)
{
    Console.WriteLine("Medium answer");
}
Enter fullscreen mode Exit fullscreen mode

What happens?

Input length = 2:

  • First if → true → runs
  • Second if → true → also runs

👉 Both blocks execute


7️⃣ Why This Happens

Because of one simple rule:

Without else, if statements are independent

  • A previous if being true
  • does not stop the next if from running

The correct “single-branch” pattern

if (condition1)
{
}
else if (condition2)
{
}
else
{
}
Enter fullscreen mode Exit fullscreen mode

👉 This structure guarantees
exactly one execution path


8️⃣ if / else Introduces Scope

if (condition)
{
    int x = 10;
}
Enter fullscreen mode Exit fullscreen mode
  • x exists only inside this block
  • It cannot be used outside

👉 This is called scope
(and it’s the topic of the next lesson)


9️⃣ What You Must Take Away From This Lesson

❌ Not there yet if

  • You can’t explain why a branch ran
  • You think removing else doesn’t change behavior

✅ You’re on track if

You understand that else is not optional syntax,
but a tool that controls execution flow


🔑 One-Line Summary

if checks conditions,
else groups the remaining cases,
and without else, every if runs independently.


Why the Next Lesson Naturally Follows

Next topic 👇
Scope (variable lifetime and visibility)

  • Why variables declared inside if
  • are invisible outside the block

👉 You cannot understand scope
without first understanding if / else.

Top comments (0)