The if-else
statement in C++ is used for decision-making. It allows the program to execute one block of code if a condition is true and another block if the condition is false.
1️⃣ Basic if-else
Statement
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 0) {
cout << "The number is positive." << endl;
} else {
cout << "The number is not positive." << endl;
}
return 0;
}
Output:
Enter a number: 5
The number is positive.
📌 If num
is negative or zero, it will print "The number is not positive."
.
2️⃣ if-else with Even/Odd Check
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0) {
cout << "The number is even." << endl;
} else {
cout << "The number is odd." << endl;
}
return 0;
}
Output:
Enter a number: 7
The number is odd.
3️⃣ if-else with Multiple Conditions (if-else if-else
)
If there are multiple conditions, we use if-else if-else
.
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 90) {
cout << "Grade: A" << endl;
} else if (marks >= 75) {
cout << "Grade: B" << endl;
} else if (marks >= 50) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F (Fail)" << endl;
}
return 0;
}
Output:
Enter your marks: 80
Grade: B
📌 The conditions are checked in order, and the first matching condition executes.
4️⃣ Nested if-else
You can place an if
statement inside another if
statement, called nested if
.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
if (age >= 60) {
cout << "You are a senior citizen." << endl;
} else {
cout << "You are an adult." << endl;
}
} else {
cout << "You are a minor." << endl;
}
return 0;
}
Output:
Enter your age: 65
You are a senior citizen.
📌 Inner if
executes only if the outer if
condition is true.
5️⃣ Using Logical Operators (&&
, ||
, !
)
Example: Checking Voting Eligibility
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18 && age <= 60) { // Both conditions must be true
cout << "You are eligible to work." << endl;
} else {
cout << "You are either too young or retired." << endl;
}
return 0;
}
Output:
Enter your age: 25
You are eligible to work.
📌 &&
ensures both conditions are true before executing the if
block.
6️⃣ Using Ternary Operator (? :
)
A compact alternative to if-else
:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
string result = (num % 2 == 0) ? "Even" : "Odd"; // Ternary operator
cout << "The number is " << result << "." << endl;
return 0;
}
Output:
Enter a number: 9
The number is Odd.
📌 ?
is a shorthand for if-else
when assigning values based on a condition.
Conclusion
- Use
if
when checking a single condition. - Use
if-else
for two-way decision making. - Use
if-else if-else
for multiple conditions. - Use nested
if
for hierarchical decision-making. - Use logical operators (
&&, ||, !
) for complex conditions. - Use ternary operator (
? :
) for shortif-else
statements.
Would you like to see an example of error handling (e.g., invalid input handling)? 🚀
0 Comments