There are 6 types of Decision statements available in c++:
1. if
2. if else
3. nested if
4. conditional operator or ternary operator
5. switch statement
6. break
7. goto statement
Decision making statements are used to change the flow of execution of the program from one part to another part of the program. They also used to test the condition(s) and change the flow accordingly.
1. if
If statement is used to execute set of statements if a particular condition is true. The set of statements to be executed should be included in a curly braces “{ }”. If there is only one statement, then there is no need to be included inside a curly brace.
Syntax:
if (condition)
{
//statements
}
Example:
Simple program to check if a number is a positive number or not.
#include<iostream>
using namespace std;
int main()
{
int num = 0;
cout<<"Enter a number"<<endl;
cin >>num;
if(num > 0)
cout<<"The number is a +ve number";
return 0;
}
Output:
Enter a number
10
The number is a +ve number
2. if else
In if..else statement, the if block is executed if the condition is true. And else block will be executed if the condition is false.
Syntax:
if (condition)
{
//statements
}
else
{
//statements
}
Example:
An example to check if a number is even or odd:
#include<iostream>
using namespace std;
int main()
{
int num = 0;
cout<<"Enter a number"<<endl;
cin >>num;
if(num %2 == 0)
cout<<"The number is an even number"<<endl;
else
cout<<"The number is an odd number"<<endl;
return 0;
}
Output:
Enter a number
10
The number is an even number
3. nested if
There can be an if statement inside an if statement. Then it is called as nested if.
Example to find greatest of 3 numbers:
#include<iostream>
using namespace std;
int main()
{
int num_1 = 10;
int num_2 = 8;
int num_3 =4;
if(num_1 > num_2)
{
if (num_1 > num_3)
cout<<"The num_1 is the greatest number"<<endl;
else
cout<<"The num_3 is the greatest number"<<endl;
}
else if (num_2 > num_3)
cout<<"The num_2 is the greatest number"<<endl;
else
cout<<"The num_3 is the greatest number"<<endl;
return 0;
}
Output:
The num_1 is the greatest number
4. conditional operator or ternary operator
Instead of if .. else, we can use a ternary operator.
Syntax is as shown below:
(condition ? <execute_if_true> : <execute_if_false> ) ;
Example to check if a number is even or odd using ternary operator:
#include<iostream>
using namespace std;
int main()
{
int num_1 = 5;
((5 % 2 == 0) ? cout<<"Number is even"<<endl : cout<<"Number is odd"<<endl);
return 0;
}
Output:
Number is odd
5. switch statement
Instead of using if .. else .. if statements, we can use “switch” statement. In switch statement, we have number of “case” statement. When the condition is resolved to any one of the “case”, then that particular case is executed. If the condition is not resolved to any of the case, then default case is executed.
Note:
1. It is important to use “break” after every case statement, else the next case statement will be executed.
2. Default block if defined, should not be an empty block.
3. The switch expression should be resolved to integer numbers only.
Below example shows all of the scenarios:
#include<iostream>
using namespace std;
int main()
{
int num = 0;
cout<<"Enter a number"<<endl;
cin>>num;
cout<<endl;
switch(num)
{
case 2: cout<<"Number is 2"<<endl;
break;
case 3: cout<<"Number is 3 \n";
cout<<"Here we did not put break. Hence program will execute next case statement also"<<endl;
case 4: cout<<"Number is 4\n";
break;
case 10: cout<<"Number is 10\n";
break;
default: cout<<"None of the number matches"<<endl<<"No need of break statement. As default is the last block"<<endl;
}
return 0;
}
Output 1:
Enter a number
10
Number is 10
Output 2:
Enter a number
3
Number is 3
Here we did not put break. Hence program will execute next case statement also
Number is 4
Output 3:
Enter a number
30
None of the number matches
No need of break statement. As default is the last block
6. break:
As we have seen in the above switch statement, we use “break” to come out of the loop. When the program encounters break statement, then the control will be passed to the next statement after the loop. In switch statement, break is used to come out of the case.
7. goto statement
“goto” is used to make a unconditional jump. When a program encounters a “goto” the program execution will jump to the “label” that has been mentioned. This causes confusion to the programmers. Hence it is often not used.
Example to check if a number is +ve or -ve using a goto statement:
#include<iostream>
using namespace std;
int main()
{
int num = 0;
cout<<"Enter a number"<<endl;
cin >>num;
if(num > 0)
goto even;
else
goto odd;
even:
cout<<"The number is +ve"<<endl;
return 0;
odd:
cout<<"The number is -ve"<<endl;
return 0;
}
Output:
Enter a number
-2
The number is -ve