Open In App

Decision Making in C++

Last Updated : 12 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Decision-making is the process to make a decision about which part of the code should be executed or not based on some condition. Decision-making in C++ involves the usage of conditional statements (also called decision control statements) to execute specific blocks of code primarily based on given situations and their results.

Types of Decision-Making Statements in C++

In C++, the following decision-making statements are available:

cpp decision making statements

1. if Statement

In C++, the if statement is the simplest decision-making statement. It allows the execution of a block of code if the given condition is true. The body of the if statement is executed only if the given condition is true.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

int main() {
//Driver Code Ends }

    int age = 19;
  
  	// Check if age is greater than 18 fo
  	// vote eligiblity
    if (age > 18) {
        cout << "allowed to vote";
    }

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
allowed to vote

We can skip to write curly brasses if there is only line statement inside the curly brasses.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

int main() {
//Driver Code Ends }

    int age = 19;
    if (age > 18)
        cout << "allowed to vote";
    return 0;

//Driver Code Starts{
}
//Driver Code Ends }

Output
allowed to vote

Flowchart:

2. if-else Statement

The if else is adecision-making statement allows us to make a decision based on the evaluation of a given condition. If the given condition evaluates to true then the code inside the 'if' block is executed and in case the condition is false, the code inside the 'else' block is executed.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

int main() {
    int n = 5;

    // Using if-else to determine if the number is positive
    // or non positive
//Driver Code Ends }

    if (n > 0) {
        cout << "number is positive.";
    }
    else {
        cout << "number is non-positive.";
    }

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
number is positive.

Flowchart:


3. if else if Ladder

Theif else if Ladder statements allow us to include additional situations after the preliminary if condition. The 'else if' condition is checked only if the above condition is not true, and the `else` is the statement that will be executed if none of the above conditions is true. If some condition is true, then not only the associated block is executed.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

int main() {
    int age = 18;

    // if this condition is true child is printed
//Driver Code Ends }

    if (age < 13) {
        cout << "child";
    }
 
    // if above above if statement is not true then we check
    // this else if condition if it evalutes to true print
    // growing age
    else if (age >= 1 and age <= 18) {
        cout << "Growing stage";
    }

    // if none of above condition is true print adult
    else {
        cout << "adult";
    }

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
Growing stage

Flowchart:

4. Nested if else

The nested if else statementcontains an 'if' statement inside another 'if' statement. This structure lets in more complex selection-making by way of comparing multiple conditions. In this type of statement, multiple conditions are checked, and then the body of the last if statement is executed.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

int main() {
    int n = 44;
  
    // to check if n is positive
//Driver Code Ends }

    if (n > 0) {

        // to check if the positive n is even or odd
        if (n % 2 == 0) {
            cout << "positive and even number";
        }
        else {
            cout << "positive and odd number";
        }
    }
    // to check if the n is 0
    else if (n == 0) {
        cout << "the number is zero";
    }
    // to check if the n is negative
    else {
        cout << "the number is negative";
    }

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
positive and even number

Flowchart:

5. Switch Statement

In C++, theswitch statement is used when multiple situations need to be evaluated primarily based on the value of a variable or an expression. switch statement acts as an alternative to multiple if statements or if-else ladder and has a cleaner structure and it is easy for handling multiple conditions.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

int main() {
    char c = 'B';
//Driver Code Ends }

    switch (c) {
        
    // if the input character is A then print GFG
    case 'A':
        cout << "GFG";
        break;

    // if the input character is B then print
    // GeeksforGeeks
    case 'B':
        cout << "GeeksforGeeks";
        break;
    default:
        
        // if the input character is invalid then print
        // invalid input
        cout << "invalid input";

//Driver Code Starts{
    }
    return 0;
}
//Driver Code Ends }

Output
GeeksforGeeks

Flowchart:

6. Ternary Operator ( ? : )

Theconditional operator is also known as a ternary operator. It is used to write conditional operations provided by C++. The '?' operator first checks the given condition, if the condition is true then the first expression is executed otherwise the second expression is executed. It is an alternative to an if-else condition in C++.

Syntax:

expression ? statement_1 : statement_2;

Flowchart:

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

int main() {
//Driver Code Ends }

    int num1 = 10, num2 = 40;
    int max;
  
    // if the condition is true then num1 will be printed
    // else num2 will printed
    max = (num1 > num2) ? num1 : num2;
    cout << max;

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
40

7. Jump Statements

Jump statements are used to alter the normal flow of the code. If you want to break the flow of the program without any conditions, then these jump statements are used. C++ provides four types of jump statements.

A) break

The break is a control flow statement that is used to terminate the loop and switch statements whenever a break statement is encountered and transfer the control to the statement just after the loop.

break statement is generally used when the actual number of iterations are not predefined, so we want to terminate the loop based on some conditions.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        // if i become 3 then break the loop and move to
        // next statement out of loop
//Driver Code Ends }

        if (i == 3) {
            break;
        }

//Driver Code Starts{
        cout << i << endl;
    }
    // next statements
    return 0;
}
//Driver Code Ends }

Output
0
1
2

B) continue

The continue statement is used to skip the loop body for the current iteration and continue from the next iteration. Unlike the break statement which terminates the loop completely, continue allows us just to skip one iteration and continue with the next iteration.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        // if i become 3 then skip the rest body of loop and
        // move next iteration
//Driver Code Ends }

        if (i == 3) {
            continue;
        }

//Driver Code Starts{
        cout << i << endl;
    }
    return 0;
}
//Driver Code Ends }

Output
0
1
2
4

C) goto

The goto statement is used to transfer the control to another part of the program. It transfers the control unconditionally to the labeled statement.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

int main() {
    int age = 17;
    if (age < 18) {
        
//Driver Code Ends }

        // Execution of code go to the
        // line 15
        goto Noteligible;
    }
    else {
        cout << "You can vote!";
    }
Noteligible:
    cout << "You are not eligible to vote!
";
    return 0;

//Driver Code Starts{
}
//Driver Code Ends }

Output
You are not eligible to vote!

Note Use of goto is generally avoided in modern programming practices because it may disturb the readability of the code and make the code error-prone, although it is still valid and used occasionally.

D) return

The return statement is used to exit the function immediately and optionally returns a value to the calling function. It returns to the function from where it was called and if it is the 'main' function then it marks the end of the execution. So basically, return is a mechanism used to communicate the results back to the calling function.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

// Function to add two numbers and returns the result
//Driver Code Ends }

int add(int a, int b) {
    int sum = a + b;
    return sum; // Return the sum to the calling code
}

int main() {
    int res = add(3, 5);
    cout << "The sum is: " << res << endl;

    return 0;

//Driver Code Starts{
}
//Driver Code Ends }

Output
The sum is: 8

Next Article

Similar Reads