C++ Conditionals Tutorial: if, else, and switch Explained
Every program makes decisions. Should we display an error or a success message? Is the user old enough to proceed? Did the calculation overflow? Conditionals are how your program answers these questions — they let your code take different paths based on whether something is true or false.
C++ has two main conditional structures: if/else and switch. This tutorial explains both from scratch.
The if Statement
The most basic conditional: “if this is true, do this.”
if (condition) {
// runs only if condition is true
}
A concrete example:
#include <iostream>
int main() {
int temperature = 35;
if (temperature > 30) {
std::cout << "It's hot outside." << std::endl;
}
return 0;
}
Output:
It's hot outside.
If temperature were 25, nothing would print — the block is skipped entirely.
The condition inside if ( ) must evaluate to a boolean: true or false. In C++, any non-zero value is treated as true, and 0 is false.
if / else
Add an else block to handle the case when the condition is false:
if (condition) {
// runs if true
} else {
// runs if false
}
int score = 72;
if (score >= 60) {
std::cout << "You passed." << std::endl;
} else {
std::cout << "You failed." << std::endl;
}
// Output: You passed.
Exactly one of the two blocks will always run. Never both, never neither.
else if — Multiple Conditions
Chain else if to test multiple conditions in sequence:
if (condition1) {
// runs if condition1 is true
} else if (condition2) {
// runs if condition1 is false AND condition2 is true
} else if (condition3) {
// runs if both above are false AND condition3 is true
} else {
// runs if ALL conditions are false
}
A grade calculator:
int score = 85;
if (score >= 90) {
std::cout << "A" << std::endl;
} else if (score >= 80) {
std::cout << "B" << std::endl;
} else if (score >= 70) {
std::cout << "C" << std::endl;
} else if (score >= 60) {
std::cout << "D" << std::endl;
} else {
std::cout << "F" << std::endl;
}
// Output: B
C++ evaluates conditions top to bottom and stops at the first true condition. Once a match is found, the rest are skipped — even if multiple conditions would be true.
Comparison Operators
These produce true or false and are used inside conditions:
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | x == 5 |
!= | Not equal to | x != 0 |
< | Less than | x < 10 |
> | Greater than | x > 0 |
<= | Less than or equal | x <= 100 |
>= | Greater than or equal | x >= 18 |
Common mistake: using = (assignment) instead of == (comparison):
int x = 5;
if (x = 10) { // BUG: assigns 10 to x, then evaluates as true (non-zero)
std::cout << "This always runs!" << std::endl;
}
if (x == 10) { // Correct: compares x to 10
std::cout << "x is 10" << std::endl;
}
Logical Operators
Combine multiple conditions with logical operators:
| Operator | Meaning | Example |
|---|---|---|
&& | AND — both must be true | age >= 18 && hasID |
|| | OR — at least one must be true | isAdmin || isOwner |
! | NOT — inverts the result | !isEmpty |
int age = 20;
bool hasTicket = true;
if (age >= 18 && hasTicket) {
std::cout << "Entry allowed." << std::endl;
} else {
std::cout << "Entry denied." << std::endl;
}
// Output: Entry allowed.
bool isWeekend = true;
bool isHoliday = false;
if (isWeekend || isHoliday) {
std::cout << "Office is closed." << std::endl;
}
// Output: Office is closed.
Short-Circuit Evaluation
C++ stops evaluating as soon as the result is determined:
- For
&&: if the first condition is false, the second is not checked (already false) - For
||: if the first condition is true, the second is not checked (already true)
This matters when the second condition has a side effect or could crash:
int* ptr = nullptr;
if (ptr != nullptr && *ptr > 0) { // safe — if ptr is null, *ptr is never evaluated
std::cout << "Positive" << std::endl;
}
Nested if Statements
You can put an if inside another if:
int age = 25;
bool hasLicense = true;
if (age >= 16) {
if (hasLicense) {
std::cout << "You can drive." << std::endl;
} else {
std::cout << "You need a license." << std::endl;
}
} else {
std::cout << "Too young to drive." << std::endl;
}
In practice, deep nesting makes code hard to read. The same logic is often cleaner with &&:
if (age >= 16 && hasLicense) {
std::cout << "You can drive." << std::endl;
}
The switch Statement
switch is a cleaner alternative to a long else if chain when you’re comparing one variable against many specific values:
switch (expression) {
case value1:
// runs if expression == value1
break;
case value2:
// runs if expression == value2
break;
default:
// runs if no case matches
break;
}
A day-of-the-week example:
int day = 3;
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
case 4:
std::cout << "Thursday" << std::endl;
break;
case 5:
std::cout << "Friday" << std::endl;
break;
default:
std::cout << "Weekend" << std::endl;
break;
}
// Output: Wednesday
Why break Matters
Without break, execution falls through to the next case:
int x = 2;
switch (x) {
case 1:
std::cout << "One" << std::endl;
case 2:
std::cout << "Two" << std::endl; // prints this
case 3:
std::cout << "Three" << std::endl; // also prints this (no break!)
default:
std::cout << "Default" << std::endl; // and this too
}
// Output: Two Three Default
Always add break unless you intentionally want fallthrough. Intentional fallthrough is sometimes useful for grouping cases:
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
std::cout << "Weekday" << std::endl;
break;
case 6:
case 7:
std::cout << "Weekend" << std::endl;
break;
}
switch works with int, char, and enum types — not with float, double, or std::string.
The Ternary Operator
A compact one-line if/else for simple assignments:
condition ? value_if_true : value_if_false
int age = 20;
std::string status = (age >= 18) ? "adult" : "minor";
std::cout << status << std::endl; // adult
Equivalent to:
std::string status;
if (age >= 18) {
status = "adult";
} else {
status = "minor";
}
Use the ternary when it makes code shorter and clearer. Don’t nest ternaries — it becomes unreadable.
if with Initialiser (C++17)
C++17 lets you declare a variable directly inside the if condition:
if (int result = calculate(); result > 0) {
std::cout << "Positive result: " << result << std::endl;
}
// result is only in scope within the if/else block
This keeps the variable scoped to where it’s needed, reducing clutter in the surrounding function.
A Practical Example: Simple Calculator
#include <iostream>
int main() {
double a, b;
char op;
std::cout << "Enter: number operator number (e.g. 5 + 3): ";
std::cin >> a >> op >> b;
switch (op) {
case '+':
std::cout << a << " + " << b << " = " << a + b << std::endl;
break;
case '-':
std::cout << a << " - " << b << " = " << a - b << std::endl;
break;
case '*':
std::cout << a << " * " << b << " = " << a * b << std::endl;
break;
case '/':
if (b == 0) {
std::cout << "Error: division by zero" << std::endl;
} else {
std::cout << a << " / " << b << " = " << a / b << std::endl;
}
break;
default:
std::cout << "Unknown operator: " << op << std::endl;
break;
}
return 0;
}
This combines switch for the operator and a nested if for the division-by-zero guard.
Summary
Conditionals let your program make decisions. if/else handles true/false branching; else if chains handle multiple conditions; switch handles matching one value against many specific options. The ternary operator is a compact shorthand for simple assignments. Always use == for comparison, and never forget break in switch cases.
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.
👉 Get the C++ Better Explained Ebook — $19
Related Articles
- C++ Variables and Data Types: A Complete Beginner’s Guide — you compare variables in conditionals; understand them first.
- C++ Loops Tutorial: for, while, and do-while Explained — conditionals and loops are often used together; learn both.
- C++ Functions Tutorial: How to Write and Use Functions — wrap conditional logic in functions to keep code clean and reusable.