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:
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 }
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 }
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 }
Outputnumber 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 }
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 }
Outputpositive 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 }
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 }
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 }
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 }
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 }
OutputYou 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 }
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
Types of Decision-making
Decision-making is the process of selecting the best course of action from a set of alternative options to achieve a desired goal or objective. It involves four interrelated phases: explorative (searching for potential alternatives), speculative (identifying the factors that influence the decision p
6 min read
Exception Handling in C++
In C++, exceptions are unexpected problems or errors that occur while a program is running. For example, in a program that divides two numbers, dividing a number by 0 is an exception as it may lead to undefined errors. The process of dealing with exceptions is known as exception handling. It allows
11 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
8 min read
Decision Making in Objective-C (if, if...else, Nested if)
Like, every programming language, Objective-C also has a decision control statement, namely an if-else statement and a switch statement. These statements help programmers to execute a single or a set of instructions based on a defined condition. If the defined condition evaluates as true, then the s
7 min read
equal_range in C++
std::equal_range is used to find the sub-range within a given range [first, last) that has all the elements equivalent to a given value. It returns the initial and the final bound of such a sub-range. This function requires the range to be either sorted or partitioned according to some condition suc
5 min read
Type Conversion in C++
Type conversion means converting one type of data to another compatible type such that it doesn't lose its meaning. It is essential for managing different data types in C++. Let's take a look at an example: [GFGTABS] C++ #include <iostream> using namespace std; int main() { // Two variables of
4 min read
isunordered() function in C++
The isunordered() function is defined in <cmath.h> and checks whether the value of first argument can be meaningfully compared with the second argument. If the first argument cannot be meaningfully compared with the second argument (i.e one or both are NAN), it return 1 otherwise 0. Syntax: bo
2 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