In this C++ tutorial, you will learn about decision making statements if statement, if..else statement, switch statement, conditional operator along with syntax and examples.
Decision-making is an important concept in any programming language and to accomplish this, C++ uses the following decision making statements:
- if statement
- if..else statement
- switch statement
- conditional operator
Explanation of decision-making statements in C++ programming language:
if statement:
if (condition)
{
statement 1;
statement 2;
...
}
If the condition returns true value then all the statements inside the braces of if block are executed. Otherwise, if the condition returns false, the statements from inside the block are ignored, and the rest of the code is executed. If the block contains only one statement, it can be written without the braces.
For Example:
The programmer desires to retrieve the input from the user, check the number value and wishes to print the output only if it is greater than 50. Thus, this can be performed as follows:
#include <iostream>
using namespace std;
void main()
{
int a;
cout << "Input the number: ";
cin >> a;
if (a>50)
cout << a;
}
The output of the above program is
There is only one statement to execute if the if statement returns true value, therefore, the braces are not placed.
Suppose, in the above program, if the condition returns true value. The programmer wants to print the value of a first and then add 10 to a and print. Braces are then used for the if block as follows:
#include <iostream>
using namespace std;
void main()
{
int a;
cout << "Input the number: ";
cin >> a;
if (a>50)
{
cout << a << endl;
a=a+10;
cout << a;
}
}
The output of the above program is
if..else statement:
If has several levels of condition checking. When the if statement condition1 evaluates to false, then another condition in the false block must be placed by using an else if statement. If the condition1 evaluates to true, then all the other else if blocks are ignored.
If condition1 is false, then condition2 is checked, and in case of false, the next condition is checked and continued. If all statements are false when the programmer places an else statement, the statements in this block are executed. This is referred to as a nested if..else statement.
if (condition1)
{
statement 1;
statement 2;
...
}
else if (condition2)
{
statement 1;
statement 2;
...
}
...
...
else
{
statement 1;
statement 2;
...
}
for example:
#include <iostream>
using namespace std;
void main()
{
int grade;
cout << "Input the Training Grade Level: ";
cin >> grade;
if ( grade > 100 )
{
cout << "Best Level ";
cout << grade;
}
else if ( grade == 100 )
{
cout << "Good Level ";
cout << grade;
}
else
{
cout << "Needs Improvement";
}
}
The output of the above program is
Since the input was greater than 100 the, if block got executed.
Suppose the input is 90 the output would be because here the if and the else if condition both failed and therefore statement in else block got executed. The output of the above program is
}switch statement:
If there are a number of decision making conditions instead of making an if..else construct, the programmer can use switch statement.
The general syntax is:
switch (expression)
{
case constant1:
group of statements 1;
break;
case constant2:
group of statements 2;
break;
.
.
.
default:
default group of statements
}
In the above example, the expression is first evaluated and the value id checked with the constant1 in the case statement. If this is evaluated with the same value, then the group of statements is executed. When a break statement is encountered, the control is switched to the end of the switch statement. If the value of the expression is not equal to constant1, it is checked with constant2. If it evaluates the same value, then the group of statements (in the case of constant2) is executed. When a break statement is encountered, the control is then switched to the end of the switch statement. This proceeds until the value of expressions equal one of the constant values given. If the value of the expression is not equal to any of the constant values given, then, by default, the statements present are executed.
For example:
#include <iostream>
using namespace std;
void main()
{
int x;
cout<<"Enter Input Value: ";
cin>>x;
switch(x)
{
case 10:
cout<< "Value is 10";
break;
case 20:
cout<<"Value is 20";
break;
default:
cout<<"Invalid Input";
}
}
In the above example, the output produces:
- Enter Input Value:10
- Value is 10
Since the entered input is 10, which is equal to the first case value, the statement in the first case block is executed.
- Enter Input Value:50
- Invalid Input
Since the entered input is 50, which is not equal to any of the case values, the default statement is executed.
Suppose the programmer wants the same block of statements to be executed for a number of case values. This is performed by removing the break statement. If a break statement is removed, then the control inside the end of a switch statement will move to the next case statement and proceed until it finds a break statement.
Important care must be taken not to place variables in case values. Only constants can be placed and expressions of the switch statement can be compared only with constants in case statements.
conditional operator:
As we have seen before in the earlier chapter covering operators in C++, the conditional operator actually is a form of if…else statement.
if(a < b)
m=a;
else
m=b;
could be written using conditional operators as
m=(a < b)? a:b;