Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Decision Making in C++
Next article icon

Decision Making in C++

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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++
#include <iostream> using namespace std;  int main() {     int age = 19;      	// Check if age is greater than 18 fo   	// vote eligiblity     if (age > 18) {         cout << "allowed to vote";     }     return 0; } 

Output
allowed to vote

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

Example:

C++
#include <iostream> using namespace std;  int main() {     int age = 19;     if (age > 18)         cout << "allowed to vote";     return 0; } 

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++
#include <iostream> using namespace std;  int main() {     int n = 5;      // Using if-else to determine if the number is positive     // or non positive     if (n > 0) {         cout << "number is positive.";     }     else {         cout << "number is non-positive.";     }     return 0; } 

Output
number is positive.

Flowchart:


3. if else if Ladder

The if 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++
#include <iostream> using namespace std;  int main() {     int age = 18;      // if this condition is true child is printed     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";     }     return 0; } 

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++
#include <iostream> using namespace std;  int main() {     int n = 44;        // to check if n is positive     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";     }     return 0; } 

Output
positive and even number

Flowchart:

5. Switch Statement

In C++, the switch 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++
#include <iostream> using namespace std;  int main() {     char c = 'B';     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";     }     return 0; } 

Output
GeeksforGeeks

Flowchart:

6. Ternary Operator ( ? : )

The conditional 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:

C++
expression ? statement_1 : statement_2; 

Example:

C++
#include <iostream> using namespace std;  int main() {     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;     return 0; } 

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++
#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         if (i == 3) {             break;         }         cout << i << endl;     }     // next statements     return 0; } 

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++
#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         if (i == 3) {             continue;         }         cout << i << endl;     }     return 0; } 

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++
#include <iostream> using namespace std;  int main() {     int age = 17;     if (age < 18) {                  // Execution of code go to the         // line 15         goto Noteligible;     }     else {         cout << "You can vote!";     } Noteligible:     cout << "You are not eligible to vote!\n";     return 0; } 

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++
#include <iostream> using namespace std;  // Function to add two numbers and returns the result 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; } 

Output
The sum is: 8

Next Article
Decision Making in C++

A

amritkumasakv
Improve
Article Tags :
  • C++
  • Geeks Premier League
  • CPP-Basics
  • Geeks Premier League 2023
Practice Tags :
  • CPP

Similar Reads

    Decision Making in LISP
    Decision making is used to specify the condition to evaluate an expression in LISP. There are 4 types of decision-making statements in LISP. They are ifcondwhencaseif Statement The if is a decision-making statement used to check whether the condition is right or wrong. The if the condition is right,
    6 min read
    Decision Making in C (if , if..else, Nested if, if-else-if )
    In C, programs can choose which part of the code to execute based on some condition. This ability is called decision making and the statements used for it are called conditional statements. These statements evaluate one or more conditions and make the decision whether to execute a block of code or n
    7 min read
    isgreater() in C/C++
    In C++, isgreater() is a predefined function used for mathematical calculations. math.h is the header file required for various mathematical functions.isgreater() function used to check whether the 1st argument given to the function is greater than the 2nd argument given to the function or not. Mean
    3 min read
    if constexpr in C++ 17
    In C++17, if constexpr feature was introduced to allow compile-time branching based on constant expressions. Unlike regular if statements, which are evaluated at runtime, the if constexpr allows the compiler to discard branches of code that do not apply. It means only the branch of code for which th
    3 min read
    Using Range in C++ Switch Case
    In C++, we generally know about the switch case which means we give an attribute to the switch case and write down the cases in it so that for each case value we can make desired statements to get executed. We can also define the cases with a range of values instead of a single value. Prerequisites:
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences