C++ Basics · Chapter 3

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.

By Arj Updated July 2025 12 min read No prior experience needed
What you'll learn
  • 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:

cpp
if (condition) {
    // code runs if condition is true
}

Example:

cpp
int number = 10;

if (number > 0) {
    cout << "The number is positive." << endl;
}
OutputThe number is positive.
Braces matter
Even if you have only one statement inside the 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.

cpp
int number = -5;

if (number > 0) {
    cout << "Positive number." << endl;
} else {
    cout << "Not a positive number." << endl;
}
OutputNot a positive number.

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.

cpp
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;
}
OutputGrade: B
Order matters
Conditions are checked from top to bottom. The first true condition executes, and the rest are ignored. Put the most specific conditions first and the most general last.

5Relational operators

Relational operators compare two values and return true or false. These are the building blocks of conditional logic.

OperatorMeaningExample (x = 5, y = 10)
==Equal tox == y → false
!=Not equal tox != y → true
>Greater thanx > y → false
<Less thanx < y → true
>=Greater than or equalx >= y → false
<=Less than or equalx <= y → true
== vs =
== 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.

OperatorMeaningExample
&&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:

cpp
int age = 25;
bool hasLicense = true;

if (age >= 18 && hasLicense) {
    cout << "You can drive." << endl;
} else {
    cout << "You cannot drive." << endl;
}
OutputYou can drive.

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.

cpp
int number = 8;

if (number > 0) {
    if (number % 2 == 0) {
        cout << "Positive even number." << endl;
    } else {
        cout << "Positive odd number." << endl;
    }
}
OutputPositive even number.
Keep nesting shallow
Too many levels of nesting make code hard to read. If you find yourself going more than two or three levels deep, consider using logical operators or refactoring.

8Example: Simple login check

Here is a practical example that combines several concepts from this chapter: reading input, comparing strings, and using conditionals.

cpp
#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;
}
Sample runEnter username: admin Enter password: 1234 Login successful!

9Common mistakes to avoid

1. Using = instead of ==
if (x = 5) assigns 5 to x instead of comparing. This is a bug that the compiler will not catch. Always use == for comparison.
2. Forgetting braces
Without braces, only the first statement after if is conditional. The rest run regardless of the condition.
3. Always-true or always-false conditions
Avoid writing conditions like 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

Try these yourself
  1. Write a program that checks if a number is even or odd.
  2. Ask the user for their age, and print:
    • "Child" if age < 13
    • "Teenager" if age is between 13 and 19
    • "Adult" if age >= 20
  3. 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

Conditionals Quiz
Question 1 of 10
Coming up next
Loops and Iterations
Learn how to make your programs repeat tasks using while, for, and do-while loops.
Continue to Chapter 4

Stop wrestling with confusion.

Join thousands of students mastering Computer Science without the academic jargon.

From syntax to systems. We break down the hardest ideas in computer science so you can actually build things.

© 2026 Painless Programming. Built for students.
Scroll to Top