Control Flow in C++: Conditionals (if, else, else if)

What You’ll Learn

By the end of this post, you’ll be able to:

  • Make decisions in code using if, else, and else if
  • Use relational and logical operators
  • Handle multiple conditions
  • Write clean, readable conditional logic

Why Use Conditionals?

Not all programs follow a single path. Often, you want your program to make decisions based on user input or current values.

For example:

  • If the user is logged in, show their dashboard.
  • If the number is negative, print an error.
  • If the temperature is high, turn on a fan.

C++ gives you conditionals to handle such cases.


The if Statement

The if statement runs a block of code only if a condition is true.

Syntax:

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

Example:

int number = 10;

if (number > 0) {
cout << "The number is positive." << endl;
}

The else Statement

The else block runs when the if condition is false.

int number = -5;

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

The else if Ladder

Use else if to check multiple conditions in order.

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;
}

Conditions are checked from top to bottom. The first one that is true gets executed; others are skipped.


Relational Operators

These operators compare two values:

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

Logical Operators

Used to combine multiple conditions.

OperatorMeaningExample
&&Logical AND(x > 0 && x < 10) → true if both are 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;
}

Nested if Statements

You can put one if inside another. Be careful with indentation and logic.

int number = 8;

if (number > 0) {
if (number % 2 == 0) {
cout << "Positive even number." << endl;
} else {
cout << "Positive odd number." << endl;
}
}

Example: Simple Login Check

#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;
}

Common Mistakes to Avoid

  • Using = instead of == in conditions
    • if (x = 5) assigns 5 to x, which is almost always a bug
  • Forgetting braces {} for multiple statements
  • Writing conditions that always evaluate the same way (like if (true))

Quiz: Conditionals

Question 1/7

Mini Exercises

  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 between 13 and 19
    • “Adult” if age >= 20
  3. Input 3 numbers and find the largest using if/else if.

Try these before looking up solutions.


Summary

In this post, you learned how to:

  • Use if, else, and else if to make decisions
  • Work with comparison and logical operators
  • Combine multiple conditions
  • Write programs that respond to different inputs

Coming Up Next

In the next post, we’ll dive into Loops and Iteration — making your programs repeat tasks using while, for, and do-while.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top