Control Flow Decision Making
Branching Logic

C++ Conditional Statements: Complete Decision Making Guide

Learn C++ conditional statements through many short, focused examples—if, if-else, else-if, nested if, switch, and ternary—so you can read one idea at a time and practice quickly.

if Statement

Simple condition check

if-else

Two-way branching

else-if Ladder

Multiple conditions

switch-case

Multi-way selection

C++ Tutorial · Control Statements

Branching lets programs react to data. Learn every major decision structure in C++—when to use if-chains versus switch, and how to keep conditions readable and bug-free.

What you will learn

  • Write if, else, and else-if ladders for multi-way logic
  • Use switch-case for discrete integer/enum choices
  • Apply the ternary operator for compact expressions
  • Nest conditions safely with clear block scope
  • Combine relational and logical operators in tests

Why this topic matters

Control flow is tested in every coding interview. switch vs if, fall-through, and dangling else are classic discussion topics.

Key terms & indexing

C++ if else C++ switch case C++ conditional ternary operator C++

Understanding Conditional Statements

Conditional statements allow your program to make decisions and execute different code blocks based on certain conditions. They are fundamental to creating dynamic, responsive programs in C++.

if Simple Condition

Executes code only if condition is true. Single path decision making.

if-else Two-way Branch

Executes one block if true, another if false. Binary decision making.

else-if Multi-way Branch

Chain of conditions for multiple decision paths. Sequential checking.

switch Multi-choice

Efficient multi-way branching based on a single expression value.

Key Concepts:
  • Boolean Expressions: Conditions evaluate to true (1) or false (0)
  • Relational Operators: ==, !=, <, >, <=, >=
  • Logical Operators: && (AND), || (OR), ! (NOT)
  • Block Scope: Code blocks { } define scope for variables
  • Condition Evaluation: From top to bottom, stops at first true condition

1. Simple if Statement

if (condition) {
    // Code to execute if condition is true
}

Simple Examples

1. Positive number
int n = 10;
if (n > 0)
    cout << "Positive";
2. Voting age
int age = 20;
if (age >= 18)
    cout << "Can vote";
3. Pass marks
int marks = 55;
if (marks >= 40)
    cout << "Pass";
4. Equal numbers
int a = 5, b = 5;
if (a == b)
    cout << "Equal";
5. Even check
int n = 8;
if (n % 2 == 0)
    cout << "Even";
6. AND condition
int m = 70;
if (m >= 40 && m <= 100)
    cout << "Valid marks";
C++ decision control infographic: if, if-else, else-if ladder, switch-case, and nested if flowcharts with syntax and example conditions
C++ decision control visual guide — if, if-else, else-if, switch, and nested if flowcharts with syntax and branching logic for control-flow interview prep.

2. if-else Statement

if (condition) {
    // Code if condition is true
} else {
    // Code if condition is false
}

Simple Examples

1. Positive or negative
int n = -3;
if (n >= 0)
    cout << "Non-negative";
else
    cout << "Negative";
2. Even or odd
int n = 7;
if (n % 2 == 0)
    cout << "Even";
else
    cout << "Odd";
3. Pass or fail
int marks = 35;
if (marks >= 40)
    cout << "Pass";
else
    cout << "Fail";
4. Max of two numbers
int a = 12, b = 9;
if (a > b)
    cout << a;
else
    cout << b;
5. Login check
string pwd = "abc";
if (pwd == "admin")
    cout << "Welcome";
else
    cout << "Denied";
6. Discount eligibility
int age = 65;
if (age >= 60)
    cout << "Senior discount";
else
    cout << "Regular price";
Best Practices:
  • Always use braces { } even for single statements
  • Keep conditions simple and readable
  • Place more likely conditions first for efficiency
  • Use parentheses for complex conditions for clarity

3. else-if Ladder (Multiple Conditions)

if (condition1) {
    // Code if condition1 is true
} else if (condition2) {
    // Code if condition2 is true
} else if (condition3) {
    // Code if condition3 is true
} else {
    // Code if all conditions are false
}

Simple Examples

1. Grade from marks
int marks = 72;
if (marks >= 90) cout << "A";
else if (marks >= 75) cout << "B";
else if (marks >= 60) cout << "C";
else if (marks >= 40) cout << "D";
else cout << "F";
2. Temperature
int t = 28;
if (t < 0) cout << "Freezing";
else if (t < 15) cout << "Cold";
else if (t < 30) cout << "Pleasant";
else cout << "Hot";
3. Traffic light action
char light = 'R';
if (light == 'G') cout << "Go";
else if (light == 'Y') cout << "Slow";
else cout << "Stop";
4. Age group
int age = 14;
if (age < 13) cout << "Child";
else if (age < 20) cout << "Teen";
else if (age < 60) cout << "Adult";
else cout << "Senior";
5. BMI category
float bmi = 22.5;
if (bmi < 18.5) cout << "Under";
else if (bmi < 25) cout << "Normal";
else if (bmi < 30) cout << "Over";
else cout << "Obese";
6. Weekday type
int day = 6;
if (day == 1) cout << "Monday";
else if (day <= 5) cout << "Weekday";
else cout << "Weekend";
Important Notes:
  • Conditions are checked from top to bottom
  • Only the first true condition's block executes
  • Use else at the end for default case
  • Keep conditions mutually exclusive when possible
  • Avoid too many else-if levels (consider switch or other approaches)

4. Nested if Statements

if (condition1) {
    // Outer if block
    if (condition2) {
        // Inner if block
    }
}

Simple Examples

1. Login (user + password)
string user = "admin";
string pwd = "1234";
if (user == "admin") {
    if (pwd == "1234")
        cout << "Login OK";
    else
        cout << "Wrong password";
}
2. Loan eligibility
int age = 25;
int income = 40000;
if (age >= 21) {
    if (income >= 30000)
        cout << "Eligible";
    else
        cout << "Low income";
}
3. Positive + even/odd
int n = 12;
if (n > 0) {
    if (n % 2 == 0) cout << "Pos even";
    else cout << "Pos odd";
}
4. Exam + attendance
int marks = 55, att = 80;
if (marks >= 40) {
    if (att >= 75)
        cout << "Promoted";
    else
        cout << "Low attendance";
}
5. Number is zero?
int n = 0;
if (n != 0) {
    if (n > 0) cout << "Positive";
    else cout << "Negative";
} else {
    cout << "Zero";
}
Avoid Deep Nesting:
  • Deeply nested code (more than 3 levels) is hard to read and maintain
  • Consider refactoring with functions or early returns
  • Use logical operators (&&, ||) to combine conditions
  • Keep nesting shallow for better code readability

5. Switch Statement

switch (expression) {
    case value1:
        // Code for value1
        break;
    case value2:
        // Code for value2
        break;
    default:
        // Code if no case matches
        break;
}

Simple Examples

1. Calculator (+ - * /)
char op = '+';
int a = 10, b = 3;
switch (op) {
    case '+': cout << a + b; break;
    case '-': cout << a - b; break;
    case '*': cout << a * b; break;
    case '/': cout << a / b; break;
    default: cout << "Invalid";
}
2. Menu choice
int choice = 2;
switch (choice) {
    case 1: cout << "Pizza"; break;
    case 2: cout << "Burger"; break;
    case 3: cout << "Pasta"; break;
    default: cout << "Invalid";
}
3. Day of week
int day = 5;
switch (day) {
    case 1: cout << "Mon"; break;
    case 2: cout << "Tue"; break;
    case 7: cout << "Sun"; break;
    default: cout << "Other day";
}
4. Grade (char)
char g = 'B';
switch (g) {
    case 'A': cout << "Excellent"; break;
    case 'B': cout << "Good"; break;
    case 'C': cout << "Average"; break;
    default: cout << "Needs work";
}
5. Season by month
int m = 7;
switch (m) {
    case 12: case 1: case 2:
        cout << "Winter"; break;
    case 6: case 7: case 8:
        cout << "Summer"; break;
    default: cout << "Other";
}
6. HTTP-style status
int code = 404;
switch (code) {
    case 200: cout << "OK"; break;
    case 404: cout << "Not Found"; break;
    case 500: cout << "Server Error"; break;
    default: cout << "Unknown";
}
Switch Limitations & Rules:
  • Switch expression must be integral or enumeration type (int, char, enum)
  • Case values must be constant expressions (not variables)
  • break is optional but usually needed to prevent fall-through
  • default case is optional but recommended
  • Each case creates its own block scope (variables need to be in blocks)

6. Ternary (Conditional) Operator

// Syntax:
condition ? expression1 : expression2

// Equivalent to:
if (condition) {
    expression1;
} else {
    expression2;
}

Simple Examples

1. Max of two numbers
int a = 10, b = 20;
int max = (a > b) ? a : b;
2. Even or odd (text)
int n = 7;
string s = (n % 2 == 0) ? "even" : "odd";
3. Absolute value
int x = -15;
int absVal = (x < 0) ? -x : x;
4. Pass or fail
int marks = 55;
string st = (marks >= 40) ? "Pass" : "Fail";
5. Member discount
bool member = true;
double d = member ? 0.2 : 0.1;
6. Greeting by hour
int h = 14;
string msg = (h < 12) ? "Morning" :
             (h < 18) ? "Afternoon" : "Evening";
Ternary Operator Warnings:
  • Keep ternary expressions simple and readable
  • Avoid nested ternary operators - they become hard to read
  • Don't use for complex logic - use if-else instead
  • Both expressions must be of compatible types
  • Use parentheses for complex conditions

Comparison & Best Practices

When to Use Which Statement

Statement Best For Limitations Example Use Case
if Single condition check Only handles true case Input validation, checking preconditions
if-else Binary decisions (true/false) Only two outcomes Pass/fail, even/odd, login success/failure
else-if ladder Multiple exclusive conditions Can become long and complex Grade systems, temperature ranges, age groups
nested if Hierarchical decisions Can become deeply nested (hard to read) Multi-level menus, complex validation rules
switch Equality checks on single expression Only works with integral/enum types Menu systems, day/month names, calculator operations
ternary operator Simple one-liner assignments Only two outcomes, can be hard to read Simple value assignments, inline conditions
Best Practices Summary:
  • Always use braces { } even for single statements
  • Keep conditions simple and readable
  • Avoid deep nesting - refactor into functions if needed
  • Use switch for equality checks on single expressions
  • Prefer if-else for range checks and complex conditions
  • Document complex conditions with comments
  • Test edge cases in your conditions

Quick Practice: Mixing Conditionals

Short programs that combine if, else-if, switch, and ternary in real mini tasks.

Student result (if + else-if + ternary)
int marks = 68;
char grade;
if (marks >= 90) grade = 'A';
else if (marks >= 75) grade = 'B';
else if (marks >= 60) grade = 'C';
else grade = 'F';
string st = (marks >= 40) ? "Pass" : "Fail";
ATM menu (switch)
int ch = 2;
switch (ch) {
    case 1: cout << "Balance"; break;
    case 2: cout << "Withdraw"; break;
    case 3: cout << "Deposit"; break;
    default: cout << "Exit";
}
Ticket price (nested if)
int age = 10;
if (age < 5) cout << "Free";
else if (age < 18) cout << "Child fare";
else cout << "Adult fare";
Leap year check
int y = 2024;
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
    cout << "Leap year";
else
    cout << "Not leap";

Quick Quiz: Test Your Knowledge

What will be the output of this code?

int x = 5, y = 10;
if (x > y) {
  cout << "X is greater";
} else if (x == y) {
  cout << "Equal";
} else {
  cout << "Y is greater";
}
A) X is greater
B) Equal
C) Y is greater
D) No output

Summary & Key Takeaways

Do's
  • Always use braces for code blocks
  • Keep conditions simple and readable
  • Use comments for complex conditions
  • Test all possible paths (edge cases)
  • Choose the right statement for the job
Don'ts
  • Don't create deeply nested structures
  • Avoid complex conditions in one line
  • Don't forget break in switch cases
  • Avoid unnecessary else blocks
  • Don't use switch for range checks

Final Thoughts

Conditional statements are the backbone of decision-making in C++ programming. Mastering if, else-if, nested if, and switch statements is essential for writing dynamic and responsive programs. Remember that clarity and readability are more important than clever one-liners. Practice with different scenarios to build your intuition for choosing the right conditional structure for each situation.

Frequently asked questions

When should I use switch instead of if?

When comparing one expression to many constant integral values; switch can be clearer and sometimes faster.

What is dangling else?

Ambiguous pairing of else with nested ifs—use braces {} to make intent explicit.

Can switch work on strings?

Not directly in classic C++; use if-else or map/hash for string branching (C++17 may offer constexpr alternatives).