Coding/Learn/cpp/if else conditions
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

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

Branching Decisions: if, else if, and else

PreviousNext Lesson
In Plain English

Programs make intelligent choices through conditional branching. The if statement tests a condition; if true, its code block executes. If false, it cascades down to optional else if checks or the catch-all else block.

The Mental Model

"An if/else statement is like a train track switch. If the signal is green (condition is true), the train rolls down Track A. Otherwise (else), the switch flips and the train rolls down Track B."

Deep Dive: How It Works

Comparison Operators: == (is equal to), != (not equal to), > (greater than), < (less than), >= (greater or equal), <= (less or equal).

The Assignment vs Equality Danger: Single = assigns a value; double == checks equality! Writing if (x = 5) always evaluates to true and overwrites x!

Curly Braces: While single statements don't strictly require braces, always use { } to avoid dangerous indentation bugs.

Syntax Blueprint

if (score >= 90) {
  grade = 'A';
} else if (score >= 80) {
  grade = 'B';
} else {
  grade = 'C';
}

Sequential conditional evaluation from top to bottom.

PointCore Rules to Remember

Point star
Double Equals for Comparison: Always use == to check equality (never single =).
Point star
First True Wins: Once an if or else if condition succeeds, the remaining blocks are skipped.
Point star
Default Else: The else block acts as the fallback safety net when no prior condition matched.

Common Beginner Traps & How to Fix Them

Mistake:Writing if (x = 10) instead of if (x == 10)

Why it happens: Typo of single equal sign.

How to fix: Remember: = is assignment, == is equality test.

Pro Tip: In modern compilers, writing -Wall will warn you immediately if you accidentally write if (x = 10)!

Live Interactive Example

Hit Run Code to see it live
Grading System with if/else
C++ 20
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 85 is not >= 90 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 number positivity

  • PointDeclare int num = -5;
  • PointWrite an if/else checking if num > 0 (print "Positive"), else if num < 0 (print "Negative"), else (print "Zero").
1
2
3
4
5
6
7
8
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