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
Naming Convention in C++
Naming a file or a variable is the first and the very basic step that a programmer takes to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way to read the code. In C++, naming conventions are the set of rules for choosing the valid name for
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
log() Function in C++
The std::log() in C++ is a built-in function that is used to calculate the natural logarithm (base e) of a given number. The number can be of any data type i.e. int, double, float, long long. It is defined inside the <cmath> header file. In this article we will learn about how to use std::log(
2 min read
isfinite() function in C++
The isfinite() function is a builtin function in C++ and is used to determine whether a given value if finite or not. A finite value is a value that is neither infinite nor NAN. If the number is finite then the function returns 1 else returns zero.Syntax: bool isfinite(float x); or, bool isfinite(do
2 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
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
ratio_less() function in C++
The ratio_less() is an inbuilt function in C++ which checks if the ratio R1 is less than the ratio R2. It returns True if the ratio is less than ratio 2, else it returns false. Syntax: template ratio_less Template Parameters: The function accepts two template parameters ratio1 and ratio2 which are t
2 min read
std::min_element in C++
The std::min_element() in C++ is an STL algorithm that is used to find the minimum element in a given range. This range can be array, vector, list or any other container. It is defined inside the <algorithm> header file. In this article, we will learn about the std::min_element() in C++. Examp
4 min read
iswdigit() function in C/C++
The iswdigit() is a built-in function in C++ STL which checks if the given wide character is an decimal digit character or not. It is defined within the cwctype header file of C++. The characters from 0 to 9 i.e.0, 1, 2, 3, 4, 5, 6, 7, 8, 9 are classified as decimal digits. Syntax: int iswdigit(ch)
2 min read