C++ program of decision making by using if & if-else statement
ADVERTISEMENTS
C++ program of decision making, There are you will learn how to make control decisions in the C++ program.
C++ follows the same decision structure as the C language.
By using the if statement:
The syntax of the if the statement is:
if (test expression)
{
// statements to be executed if the test expression is true
}
{
// statements to be executed if the test expression is true
}
Let us understand this example through the C++ program:
// C++ program of decision making by using if statement
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer value::\n";
cin >> n;
// true if number is less than 6
if (n < 6) {
cout << "Condition is true.\n";
}
return 0;
}
Output:
Enter an integer value::
5
Condition is true.
By using the if...else statement:
The syntax of the if...else statement is:
if (test expression) {
// statements to be executed if the test expression is true
} else {
// statements to be executed if the test expression is false
}
// statements to be executed if the test expression is true
} else {
// statements to be executed if the test expression is false
}
Example-1: In this example, you will understand how true a condition occurs.
// C++ program of decision making by using if-else statement
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer value::\n";
cin >> n;
// true if number is less than 6 otherwise it will be false
if (n < 6) {
cout << "Condition is true.\n";
} else {
cout << "Condition is false.\n";
}
return 0;
}
Output:
Enter an integer value::
5
Condition is true.
Example-2: In this example, you will understand how false a condition occurs.
// C++ program of decision making by using if-else statement
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer value::\n";
cin >> n;
// true if number is less than 6 otherwise it will be false
if (n < 6) {
cout << "Condition is true.\n";
} else {
cout << "Condition is false.\n";
}
return 0;
}
Output:
Enter an integer value::
7
Condition is false.
By using the if...else ladder:
The syntax of the if...else ladder is:
if (test expression1) {
// statement(s)
} else if(test expression2) {
// statement(s)
} else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
// statement(s)
} else if(test expression2) {
// statement(s)
} else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
Let us understand this example through the C++ program:
// C++ program of decision making by using if-else ladder
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer value::\n";
cin >> n;
if (n == 0) {
cout << "Fisrt condition is true.\n";
} else if (n == 1) {
cout << "Second condition is true.\n";
} else if (n == 2) {
cout << "Third condition is true.\n";
} else {
cout << "Condition failed!\n";
}
return 0;
}
Output:
Enter an integer value::
1
Second condition is true.
By using the nested if...else statement:
The syntax of the nested if...else statement is:
if (test expression 1) {
else if(test expression 2) {
// statement(s)
} else if (test expression 3) {
// statement(s)
}
.
.
else {
// statement(s)
}
} else if (test expression n) {
// statement(s)
}
.
.
else {
// statement(s)
}
else if(test expression 2) {
// statement(s)
} else if (test expression 3) {
// statement(s)
}
.
.
else {
// statement(s)
}
} else if (test expression n) {
// statement(s)
}
.
.
else {
// statement(s)
}
Example-1: In this example, you will understand the first input is less than the second input.
// C++ program of decision making by using the nested if-else statement
#include <iostream>
using namespace std;
int main() {
int n, m;
cout << "Enter the two integer values::\n";
cin >> n >> m;
if (n >= m) {
if (n == m) {
cout << "Result: " << n << " = " << m << "\n";
} else {
cout << "Result: " << n << " > " << m << "\n";
}
} else {
cout << "Result: " << n << " < " << m << "\n";
}
return 0;
}
Output:
Enter the two integer values::
5
7
Result: 5 < 7
Example-2: In this example, you will understand the first input is greater than the second input.
// C++ program of decision making by using the nested if-else statement
#include <iostream>
using namespace std;
int main() {
int n, m;
cout << "Enter the two integer values::\n";
cin >> n >> m;
if (n >= m) {
if (n == m) {
cout << "Result: " << n << " = " << m << "\n";
} else {
cout << "Result: " << n << " > " << m << "\n";
}
} else {
cout << "Result: " << n << " < " << m << "\n";
}
return 0;
}
Output:
Enter the two integer values::
7
5
Result: 7 > 5