Decision Statements in C++
While coding for a problem we come along with various sets of conditions based on the requirement, and depending on these conditions
certain logic needs to be executed.
But the important part is that one condition should be executed at a time. So to achieve this C++ provides
us with Decision making or Selection statements.
These decision making statements play a vital role in managing the flow of execution
or control of a program.
Different types of decision making statements supported by C++ are:
- if
- if-else
- if-else-if ladder
- nested if-else
- switch-case
Let's look into the detail of each types.
if Statement:
"if " statement is used when we need to execute certain logic only when some condition is fulfilled. It is the simplest way of condition check.
The syntax of the "if" statement is:
if(expression){
.... < code logic >
}
Here "if " is the C++ keyword that is followed by the expression or condition which will be checked. If the condition is satisfied then the compiler
will
return "1" and if it fails then it will return "0". When the expression value is "1" then only the control goes inside the "if " and the code logic
will be
executed else the code logic will not be executed.
Flowchart diagram fro the for "if " statements is given as below:-
Now let's check one simple code example of if-statement usage.
#include < iostream>
using namespace std;
int main() {
int val = 0;
cout << "Enter and even integer!" << endl;
cin >> val;
if(val%2 == 0){
cout << "Your entry is correct..";
}
return 0;
}
Output will be:
Enter and even integer!
10
Your entry is correct..
Here it will first ask the user to enter a number, then in the "if " condition it will check that number if it is even or not. If the number is even
then it will return "1" and executes the code inside the "if " that will the output "Your entry is correct..". If the condition check fails then it
will
returns "0" and nothing will be printed on the screen.
if-else statement:
This statement is very useful while implementing scenarios like "either this or that" based on certain conditions. Suppose we are having a
situation where when some condition is
satisfied then it will execute a particular set of instructions and if it's not then some other set of
instructions will be executed.
The syntax for "if-else " is:-
if(expression){
.... < code logic 1>
} else {
.... < code logic 2>
}
Here when the expression value equals "1" or the condition check satisfies then the "code logic 1" will be executed and in case its fails
then "code logic 2"
will be executed.
Below is the flowchart of "while loop" execution.
Let's check a simple code example of "if-else".
#include < iostream>
using namespace std;
int main() {
int val = 0;
cout << "Enter an integer value!!"<< endl;
cin >> val;
if(val%2 == 0){
cout << "Number entered is 'Even'. ";
} else {
cout << "Number enterd is 'Odd'. ";
}
return 0;
}
Output will be:
Enter an integer value!!
63
Number enterd is 'Odd'.
Enter an integer value!!
84
Number entered is 'Even'.
Here the number will be first checked and based on the result it will print if it is even or odd.
if-else-if ladder:
In the above scenario, we were having only one condition, and based on the condition check two different statements will be executed.
Now consider the scenario where
we need to check multiple conditions one by one. It means when a condition satisfies then it's okay, but
when it fails then we need to check other conditions and so.
To implement this type of logic if-else-if comes into the picture. In this we are
checking the condition one after another using if and else which seems like the steps
of the ladder, hence it is known as if-else-if ladder
statements.
The syntax for "if-else-if " is:-
if (expression 1){
.... < code logic 1>
} else if (expression 2){
.... < code logic 2>
} else if (expression 3){
.... < code logic 3>
}....
....
....
else {
.....< code logic N>
}
Here the first if check the condition of 'expression 1' and if it satisfies then it will execute the 'code logic 1' and exit the loop. But if it fails it will
proceed to the next condition check for 'expression 2'. If it returns true then the 'code logic 2' will be executed and it will exit the loop. This process will
go on until the condition check will return at any of the steps. In case, if at every step condition check return fails then the last else will be executed and
the compiler executes the "code logic N".
Below is the flowchart of "if-else-if" statement.
Let check its concept with a simple code example.
#include < iostream>
using namespace std;
int main() {
const int rand_num = rand();
int input_num;
cout << "Enter an integer value!!";
cin >> input_num;
if(rand_num == input_num){
cout << "Congrats your number is same as mine---"<< endl;
} else if(rand_num < input_num){
cout << "Your number is greater than mine"<< endl;
} else {
cout << "Your number is smaller than mine"<< endl;
}
cout << "Your number:- "<< input_num << " My number:- "<< rand_num;
return 0;
}
Output will be:
Enter an integer value!!
55599955
Your number is smaller than mine
Your number:- 55599955 My number:- 1804289383
In this code there is constant integer variable 'rand_num' whoese value is randomly generated using the 'rand()' function of c++ library.
Next it will ask the
user to input a number of its choice and saves it into the variable 'input_num'. After that it will do the comparision
checkbetween the two variable values
and will return the corresponding result.
nested-if statement:
In the scenarios where need to check the condition inside the condition itself then there we will use nested-if statements. For example,
let's suppose if our main condition check
satisfies or not, whichever is the case, we are having the requirement to go inside and test for
more conditions further. In such cases, we will implement our logic with
the nested-if statement.
The syntax for "nested-if " is:
if (expression 1){
....
if (expression 1a){
.... < code logic 1a>
} else {
.... < code logic 1b>
}
} else{
...< code logic 2>
}
Here if the condition check for 'expression 1' satisfies then it will go inside it and check the next condition for 'expression 1a'. If it satisfies
then 'code logic 1a' will
be executed else 'code logic 1b' will be executed. And if the main condition check for 'expression 1' fails then control
will go to the else part and 'code logic 2' will be executed.
Below is the flowchart of "nested-if" statement.
Let check its concept with a simple code example.
#include < iostream>
using namespace std;
int main() {
int input_num;
cout << "Enter an integer value in range of 1 to 100!!";
cin >> input_num;
if((input_num >=1) && (input_num <= 100)){
if(input_num%2 == 0){
cout << "You had entered a even number.";
} else {
cout << "You had entered the odd number.";
}
} else {
cout << "Sorry!! Your number is not in required range.."<< endl;
}
return 0;
}
Output will be:
Enter an integer value in range of 1 to 100!!
65
You had entered the odd number.
Enter an integer value in range of 1 to 100!!
255
Sorry!! Your number is not in required range..
Enter an integer value in range of 1 to 100!!
82
You had entered a even number.
Here in the first "if" condition it will first check if the number entered is in the specified range or not. If the number is in the required
range of
1 to 100, then it will further proceed with the check if it is odd or even.
switch-case Statements:
Sometimes we are having a scenario when a condition check will result in multiple choices or outcomes and for each one of them, we need to
perform something separately.
To handle or implement such cases c++ provides us with the 'switch' statement.
The syntax for "switch " is as below:-
switch (experssion){
case value1:
< code logic 1>
break;
case value2:
< code logic 2>
break;
.......
.......
.......
default:
< code logic n>
break;
}
Here first the expression is checked with the switch statement. After that, there are different cases that handle the possible outcomes of the
expression values.
In each case required code logic will be executed and after that control will come out of the switch statement. If the value
of the expression does not fall in any
of the cases then it will go to the default case and the code logic inside it will be executed.
Below is the flowchart of "switch-case" statement.
Below is the small code snippet to explain the usage of 'switch' statement:
#include < iostream>
using namespace std;
int main() {
int input = 0;
cout << "---Welcome to the Global Bank---"<> input;
cout << endl;
switch(input){
case 1:
cout<< "Saving Accounts!! we offer good interest rate."<< endl;
cout<<"You will be re-directed to next page for registration."<< endl;
break;
case 2:
cout<<"Fixed Deposits!! For a secure future"<< endl;
cout<<"Lets proceed to secure your money."<< endl;
break;
case 3:
cout<<"SIP!! Mutual Funds will be your great friend"<< endl;
cout<<"Let us guide you with the Mutual funds."<< endl;
break;
case 4:
cout<<"Demat Account!! Making money from the market."<< endl;
cout<<"We will help you with investment in stocks."<< endl;
break;
case 5:
cout<<"Credit Card!! Use this wisely in case of urgency."<< endl;
cout<<"Proceed with teh next page for registration."<< endl;
break;
case 6:
cout<<"Welcome back Customer!!."<< endl;
cout<<"Please login with your credentials."<< endl;
break;
case 7:
cout<<"Thankss for visiting!!!."<< endl;
cout<<"Hope to see you again.."<< endl;
break;
default:
cout << "Your input "<< input <<" is out of range. Please try again!!";
break;
}
return 0;
}
Output will be:
The output is:
---Welcome to the Global Bank---
We are offering the below services
1. Open a saving account
2. Open a Fixed deposit account
3. Start the SIP
4. Open a Demat account
5. Apply for Credit card
6. Login for existing customer
7. Exit the menu..
------------------------------------
Please enter the number of your choice
1
Saving Accounts!! we offer good interest rate.
You will be re-directed to next page for registration.
This example provides a sample menu of the banking transaction. It will ask the user to select the option from the menu. After that based
on the
user input, it will internally match it with the cases in the switch statement and further proceed with the next steps.