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, andelse - 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
📌 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");
}
}
}
Execution flow
-
userInput == "ABC"→ produces a bool -
true→ifblock runs -
false→elseblock 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");
}
One key concept here
userInput.Length
-
Lengthis 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
}
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");
}
Evaluation order (important)
- First
ifis checked - If false →
else ifis checked - If still false →
elseruns
👉 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");
}
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,ifstatements are independent
- A previous
ifbeing true - does not stop the next
iffrom running
The correct “single-branch” pattern
if (condition1)
{
}
else if (condition2)
{
}
else
{
}
👉 This structure guarantees
exactly one execution path
8️⃣ if / else Introduces Scope
if (condition)
{
int x = 10;
}
-
xexists 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
elsedoesn’t change behavior
✅ You’re on track if
You understand that
elseis not optional syntax,
but a tool that controls execution flow
🔑 One-Line Summary
ifchecks conditions,
elsegroups the remaining cases,
and withoutelse, everyifruns 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)