Technical Training
C++ TutorialsC++ Decision Making Statements
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








