Coding/Learn/csharp/if else conditions
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnConditional Branching: if, else if, and else
7 / 20 0%
Lesson 07 of 20
5 mins read

Conditional Branching: if, else if, and else

PreviousNext Lesson
In Plain English

Branching logic allows your code to make intelligent runtime decisions. The if statement tests a boolean condition; if true, its code block executes. Otherwise, control flows to optional else if checks or the fallback else block.

The Mental Model

"An if/else statement is like a security gate at a VIP club. The bouncer checks your badge (condition). If you are on the list, you enter through the gold door. If not (else), you wait in the general line outside."

Deep Dive: How It Works

Comparison Operators: == (equal), != (not equal), > (greater), < (less), >= (greater/equal), <= (less/equal).

Ternary Operator (?:): Concise one-line if/else: string status = score >= 50 ? "Pass" : "Fail";.

Block Scope: Variables declared inside an if block { } exist only inside that block and are destroyed when the block ends.

Syntax Blueprint

if (health <= 0) {
  Console.WriteLine("Game Over");
} else if (health < 30) {
  Console.WriteLine("Warning: Low Health!");
} else {
  Console.WriteLine("Healthy");
}

Sequential conditional evaluation from top to bottom.

PointCore Rules to Remember

Point star
Double Equals for Equality: Always use == to check equality (never single = assignment).
Point star
Ternary for One-Liners: Use condition ? trueVal : falseVal for compact assignments.
Point star
Curly Braces: Always use { } around blocks for safety and readability.

Common Beginner Traps & How to Fix Them

Mistake:Putting a semicolon right after the if header: if (x > 5);

Why it happens: Habit of placing semicolons at the end of every line.

How to fix: if (condition) does NOT take a semicolon.

Pro Tip: In C#, you can use pattern matching: if (obj is int number) to type-check and assign in one step!

Live Interactive Example

Hit Run Code to see it live
Branching Decision Tree
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output Console
Click "Run Code" to view the rendered output.
How it works: Since 45 is not < 30 but is < 80, the second branch executes.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Check speed limit

  • PointDeclare int speed = 75;
  • PointIf speed > 70 print "Speeding!", else print "Safe Speed".
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates