C++ Decision Making

In a program, we often need to make decisions based on a condition. For example, a program may need to check whether a student has passed, whether a person is old enough to vote, or whether two numbers are equal.

C++ provides different statements for making these decisions. These statements check a condition and then decide which part of the program should run. This is called Decision Making in C++.

In this tutorial, we will discuss if, if-else, nested if, the else-if ladder, switch, and the ternary operator.


if Statement in C++

The if statement is used when you want to run some code only when a condition is true.

Syntax

if (condition)
{
    // code to execute
}

Example

int age = 20;

if (age >= 18)
{
    cout << "You can vote.";
}

Here, C++ checks whether age >= 18 is true. Since the age is 20, the message is displayed.

If the condition is false, the code inside the if block is simply skipped.


if-else in C++

What if you want your program to do one thing when a condition is true and something else when it is false? This is where if else in C++ is useful.

Example

int marks = 45;

if (marks >= 50)
{
    cout << "Pass";
}
else
{
    cout << "Fail";
}

Output

Fail

Since 45 is less than 50, the condition is false and the else block runs.


Nested if in C++

Sometimes one condition needs to be checked only after another condition is true. In such cases, you can place one if statement inside another if statement. This is called a Nested if in C++.

Example

int age = 20;
bool hasID = true;

if (age >= 18)
{
    if (hasID)
    {
        cout << "Entry allowed";
    }
}

First, the program checks the person’s age. If the person is 18 or older, it then checks whether they have an ID.

Nested if statements are useful when one decision depends on another decision.


else-if Ladder

Sometimes there are several conditions to check. Writing many separate if statements can make the program difficult to follow. The else-if ladder provides a cleaner way to handle this situation.

Example

int marks = 75;

if (marks >= 90)
{
    cout << "Grade A";
}
else if (marks >= 75)
{
    cout << "Grade B";
}
else if (marks >= 50)
{
    cout << "Grade C";
}
else
{
    cout << "Fail";
}

The conditions are checked from top to bottom. Once C++ finds a true condition, it runs that block and skips the remaining conditions.


switch Statement in C++

The switch statement is useful when you want to compare one value with several fixed choices. It is often easier to read than a long else-if ladder when you are checking exact values.

This is why the switch statement in C++ is commonly used for menus and choice-based programs.

Example

int choice = 2;

switch (choice)
{
    case 1:
        cout << "Add";
        break;

    case 2:
        cout << "Update";
        break;

    case 3:
        cout << "Delete";
        break;

    default:
        cout << "Invalid choice";
}

Output

Update

The break statement stops the switch after the matching case is executed. The default block runs when none of the cases match.


Ternary Operator in C++

The ternary operator is a short way to write a simple if-else condition. It uses three parts, which is why it is also called the conditional operator.

Syntax

condition ? value1 : value2;

Example

int age = 20;

string result = (age >= 18) ? "Adult" : "Minor";

cout << result;

Output

Adult

Here, if age >= 18 is true, "Adult" is selected. Otherwise, "Minor" is selected.

The Ternary Operator in C++ is useful when the decision is small and you want to keep the code short. For more complicated conditions, an if-else statement is usually easier to understand.


Why Decision Making is Important

Without decision-making statements, a program would simply execute every statement one after another. These statements allow your program to react differently depending on the situation.

For example, a login program can check whether the password is correct, a shopping program can check whether a product is available, and a result program can decide a student’s grade based on marks.


Frequently Asked Questions (FAQs)

Q1. What is decision making in C++?

Decision Making in C++ allows a program to execute different statements depending on whether a condition is true or false. Common statements include if, if-else, else-if, and switch.

Q2. How does if-else work in C++?

if else in C++ executes one block when a condition is true and another block when it is false. It is commonly used when a program needs to choose between two alternatives.

Q3. When should I use a switch statement in C++?

A switch statement in C++ is useful when you need to compare one expression against multiple fixed values. It is commonly used for menu-based programs and multiple-choice selections.

Q4. What is a nested if statement in C++?

A Nested if in C++ means placing one if statement inside another if statement. It is useful when a second condition needs to be checked only after the first condition is satisfied.

Q5. What is the ternary operator in C++?

The Ternary Operator in C++ (?:) is a shorthand for simple if-else conditions. It evaluates a condition and returns one of two values.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top