In the C++ language decision making statement executes if the given condition is true otherwise conditional block will never execute.
C++ language assumes non-zero and non-null values as true, and zero or null is assumed as false value.
There are following types of decision making statements in C++ programming language.
if statement
Syntax of an ‘if’ statement −
if(Condition) {
/* statement will execute if the condition is true */
}
In a ‘if’ statement if the Condition is true, then the block of statement of the ‘if’ statement will be executed.
If the Condition is false, then the block of statement of the ‘if’ statement will not executed and control sent to the next line of if block.
Example: Write a program to demonstrate the ‘if’ statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> using namespace std; int main() { int x = 10; int y = 25; if (x < y) // condition x < y is true therefore if block will execute { cout << "Sum of x+y = " << (x + y); } cout << "\n after if statement"; return 0; } |
Output
1 2 | Sum of x+y = 35 after if statement |
In the above program if reverse the ‘if’ condition of ‘if’ statement then the condition will be false and ‘if’ block will never execute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> using namespace std; int main() { int x = 10; int y = 25; if (x > y) // condition x>y is false therefore if block will not execute { cout << "Sum of x+y = " << (x + y); } cout << "\n after if statement"; return 0; } |
Output
1 | after if statement |
if-else statement
Syntax of an ‘if-else’ statement −
if(Condition) {
/* if-statement will execute if the Condition is true */
}
else
{ if the condition is false then else block will execute
}
In a ‘if-else’ statement if the Condition is true, then the block of statements of the ‘if’ statement will be executed.
If the Condition is false, then the block of code of the ‘else’ statement will executed.
Example: Write a program to take two numbers from user and find the grater between them using ‘if-else’ statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; int main() { int x; int y; cout << "enter 1st number"; cin >> x; // 10 cout << "\n enter 2nd number"; cin >> y; // 30 if (x > y) // check condition x>y { cout << "\n grater value= " << x; } else { cout << "\n grater value= " << y; } return 0; } |
Output
1 | 1st number 10 // user enter 10 in variable x enter 2nd number 30 // user enter 30 in variable y grater value= 30 |
Nested -if statement
In a C language we can use if statement inside another if statement(s).
Syntax of an ‘nested -if’ statement −
if(condition 1) {
/* statement will execute if the condition 1 is true */
If (condition 2) { /* statement will execute if the condition 2 is true */
}
}
Example: Write a program to take two numbers from user if both number are between 1 to 9 then print “Good” using nested-if statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> using namespace std; int main() { int x; int y; cout << "enter 1st number"; cin >> x; // 5 cout << "\n enter 2nd number"; cin >> y; // 7 if (x < 10) // check condition x < 10 { if (y < 10) // nested-if, check condition y < 10 { cout << "\n Good"; } } return 0; } |
Output
1 | enter 1st number 5 // user enter 5 in variable x enter 2nd number 7 // user enter 7 in variable y Good |
Nested if-else statements
In a C language we can use if or if-else statement inside another if or if-else statement(s).
Syntax of an ‘if-else’ statement −
if(condition 1) { /* statement will execute if the condition 1 is true */
If (condition 2) {
/*if- statement will execute if the condition 1 & 2 both are true */
}
else {
/* else-statement will execute if the condition1 is true Condition2 is false */
}
}
else {
If (condition 3) {
/* statement will execute if the condition 3 is true */
}
}
Example: Write a program to take three numbers from user and find the grater among them using ‘nested if-else’ statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include <iostream> using namespace std; int main() { int x; int y; int z; cout << "enter 1st number"; cin >> x; // 6 cout << "\n enter 2nd number"; cin >> y; // 2 cout << "\n enter 3rd number"; cin >> z; // 10 if (x > y) // check condition x>y { if (x > z) // check condition x>z { cout << "\n grater value= " << x; } else { cout << "\n grater value= " << z; } } else { if (y > z) // check condition x>z { cout << "\n grater value= " << y; } else { cout << "\n grater value= " << z; } } return 0; } |
Output
1 2 3 4 5 6 7 | enter 1st number 6 // user enter 6 in variable x enter 2nd number 2 // user enter 2 in variable x enter 3rd number 10 // user enter 10 in variable x grater value= 10 |