Control Flow in C++: Conditionals (if, else, else if)
Learn how to make your C++ programs think, decide, and respond to different conditions using if, else, else if, and logical operators.
- How to make decisions in code using if, else, and else if
- How to use relational operators (==, !=, >, <, >=, <=)
- How to combine conditions with logical operators (&&, ||, !)
- How to handle multiple conditions with else if
- How to write nested if statements
- Common mistakes to avoid with conditionals
1Why use conditionals?
Not all programs follow a single, straight path. Often, you want your program to make decisions based on user input or changing values. That is where conditionals come in.
Here are some real-world examples of conditionals in action:
- If the user is logged in, show their dashboard. Otherwise, show the login screen.
- If the number is negative, print an error message.
- If the temperature is above a threshold, turn on a fan.
C++ gives you several tools to handle such decisions: if, else, and else if. These let your program choose which path to follow based on conditions that evaluate to either true or false.
2The if statement
The if statement runs a block of code only if a condition is true. If the condition is false, the block is skipped entirely.
Syntax:
if (condition) { // code runs if condition is true }
Example:
int number = 10; if (number > 0) { cout << "The number is positive." << endl; }
if, it is good practice to use braces {}. It makes your code clearer and prevents bugs when you add more statements later.
3The else statement
The else block runs when the if condition is false. This gives you two paths: one for when the condition is true, and another for when it is false.
int number = -5; if (number > 0) { cout << "Positive number." << endl; } else { cout << "Not a positive number." << endl; }
4The else if ladder
Use else if to check multiple conditions in order. The first condition that evaluates to true runs its block, and the rest are skipped.
int score = 85; if (score >= 90) { cout << "Grade: A" << endl; } else if (score >= 80) { cout << "Grade: B" << endl; } else if (score >= 70) { cout << "Grade: C" << endl; } else { cout << "Grade: F" << endl; }
5Relational operators
Relational operators compare two values and return true or false. These are the building blocks of conditional logic.
| Operator | Meaning | Example (x = 5, y = 10) |
|---|---|---|
== | Equal to | x == y → false |
!= | Not equal to | x != y → true |
> | Greater than | x > y → false |
< | Less than | x < y → true |
>= | Greater than or equal | x >= y → false |
<= | Less than or equal | x <= y → true |
== compares for equality. = assigns a value. Mixing them up is one of the most common bugs in C++.
6Logical operators
Logical operators let you combine multiple conditions into a single expression.
| Operator | Meaning | Example |
|---|---|---|
&& | Logical AND | (x > 0 && x < 10) → true if both are true |
|| | Logical OR | (x == 0 || x == 5) → true if either is true |
! | Logical NOT | !(x == y) → true if x != y |
Example:
int age = 25; bool hasLicense = true; if (age >= 18 && hasLicense) { cout << "You can drive." << endl; } else { cout << "You cannot drive." << endl; }
7Nested if statements
You can put an if statement inside another if statement. This is called nesting. It is useful for checking multiple related conditions.
int number = 8; if (number > 0) { if (number % 2 == 0) { cout << "Positive even number." << endl; } else { cout << "Positive odd number." << endl; } }
8Example: Simple login check
Here is a practical example that combines several concepts from this chapter: reading input, comparing strings, and using conditionals.
#include <iostream> #include <string> using namespace std; int main() { string username; string password; cout << "Enter username: "; cin >> username; cout << "Enter password: "; cin >> password; if (username == "admin" && password == "1234") { cout << "Login successful!" << endl; } else { cout << "Invalid credentials." << endl; } return 0; }
9Common mistakes to avoid
if (x = 5) assigns 5 to x instead of comparing. This is a bug that the compiler will not catch. Always use == for comparison.
if is conditional. The rest run regardless of the condition.
if (true) or if (5 > 3). They do not depend on any variable and usually indicate a mistake or a temporary test that should be removed.
10Mini exercises
- Write a program that checks if a number is even or odd.
- Ask the user for their age, and print:
- "Child" if age < 13
- "Teenager" if age is between 13 and 19
- "Adult" if age >= 20
- Input 3 numbers and find the largest using if/else if.
Hint: Use the modulo operator % for even/odd checks, and chain conditions with && for age ranges.
11Quiz yourself
while, for, and do-while loops.