
Decision making in C++Programming
Decision-making is about the flow of execution based on certain constraints or reruns a group of statements until specified constraints are met.
Decision-making structures require to specify conditions to be tested and evaluated by the program, along with statements to be executed can be either true or false.
The general form of a typical decision-making structure:

C++ programming language handles decision-making by supporting the following statements,

Sr.No | Statement & Description |
---|---|
1 | if statement- It consists of a boolean expression followed by one or more statements. |
2 | if…else statement- An ‘if’ statement can be followed by an optional ‘else’ statement, which executes when the boolean expression is false. |
3 | nested if statements- You can use one ‘if’ or ‘else if’ statement inside another ‘if’ or ‘else if’ statement(s). |
4 | switch statement- A ‘switch’ statement allows a variable to be tested for equality against a list of values. |
Decision making with 'if
‘ statement
The If statement is the most simple decision-making statement. It is used to determine whether a block of code will be executed or not, that is, if the block of code meets the required condition then it is true and is executed otherwise not.
The general form of if structure:

The general form of a if statement is,
if(condition)
{
inside-statement;
}
outside-statement;
If the condition is true, then ‘inside-statement’ will be executed, otherwise ‘inside-statement’ is skipped and only ‘outside-statement’ will be executed.
#include<iostream.h>
int main( )
{
int a,b;
a=16;
b=14;
if (a > b )
{
cout << "a is greater than b";
}
}
OUTPUT
------
a is greater than b
if...else
statement
The general form of a simple if…else statement is,
if(expression)
{
block1;
}
else
{
block2;
}
If the ‘expression’ returns true, then the ‘block1’ will get executed, else ‘block1’ will be skipped and ‘block2’ will be executed.
void main( )
{
int a,b;
a=16;
b=19;
if (a > b )
{
cout << "a is greater than b";
}
else
{
cout << "b is greater than a";
}
}
OUTPUT
-----
b is greater than a
Nested if....else
statement
The general form of nested if..else structure:

The general form of a nested if…else statement is,
if(expression)
{
if(condition1)
{
block1;
}
else
{
block2;
}
}
else
{
block3;
}
If ‘expression‘ returns false, then the ‘block3‘ will be executed, otherwise, execution will enter the if
condition and check for ‘condition1‘. Then if the ‘condition1‘ returns true, then the ‘block1‘ will be executed otherwise ‘block2‘ will be executed.
void main( )
{
int x,y,z;
cout << "Enter 3 numbers";
cin >> x >> y >> z;
if(x > y)
{
if( x > z )
{
cout << "x is Greatest";
}
else
{
cout << "z is Greatest";
}
}
else
{
if( y > z )
{
cout << "y is Greatest";
}
else
{
cout << "z is Greatest";
}
}
}
The above code will print different statements based on the values of x, y
and z
variables.
else-if
Ladder
The general form of else-if ladder:
if(expression 1)
{
statement-block1;
}
else if(expression 2)
{
statement-block2;
}
else if(expression 3 )
{
statement-block3;
}
else
default-statement;
The condition is tested from the top further going down the ladder. As soon as the condition is found true, the associated statement with it is executed.
Decision making with ‘Switch case’ statement
In the C++ programming language, a switch-case statement is a simplified form of the Nested if-else statement, it helps to avoid long chains of if..else statements. A switch-case statement evaluates a condition against multiple cases to identify the block of code to be executed.

Syntax of Switch-case:
switch(expression){
case value1:
// statements
break;
case value2:
// statements
break;
default:
// statements
break;
}
Sample Program for Switch case:
include<iostream.h>
using namespace std;
int main()
{
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
}
OUTPUT
------
Thursday
To remember, when code execution reaches a break
keyword, it breaks out of the switch block.