Given an integer input, whether the given input is "Positive" or "Negative" or "Zero" and print the corresponding message
All numbers can be classified into exactly three categories:
1. Negative Numbers (< 0):
if (a < 0)
2. Positive Numbers (> 0):
if (a > 0)
3. Zero (== 0):
if (a == 0)
or use else
✅ These three categories are mutually exclusive - a number can only belong to ONE category!
Our three-way decision structure:
if (condition1) {
// First option
} else if (condition2) {
// Second option
} else {
// Third option (default)
}
How it works:
condition1
firstcondition2
condition2
is TRUE → Execute second block and exitelse
block💡 Key insight: Only ONE block executes, never more than one!
In our problem:
if (a < 0)
→ NEGATIVEelse if (a > 0)
→ POSITIVEelse
→ ZERO (the only remaining possibility)In this problem, you can check in any order since the conditions are mutually exclusive:
Option 1 (Our approach):
if (a < 0) NEGATIVE
else if (a > 0) POSITIVE
else ZERO
Option 2 (Also valid):
if (a > 0) POSITIVE
else if (a < 0) NEGATIVE
else ZERO
Option 3 (Also valid):
if (a == 0) ZERO
else if (a > 0) POSITIVE
else NEGATIVE
✅ All three produce the same result!
⚠️ When order DOES matter:
In problems with overlapping conditions, order is crucial:
// Grade classification example
if (marks >= 90) A
else if (marks >= 80) B
else if (marks >= 70) C
Here, 95 satisfies both >= 90
and >= 80
, but only the first match executes!
Example 1: a = -98
a = -98
if (-98 < 0)
→ TRUE ✅cout << "NEGATIVE";
Example 2: a = 0
a = 0
if (0 < 0)
→ FALSE ❌else if (0 > 0)
→ FALSE ❌else
cout << "ZERO";
Example 3: a = 42
a = 42
if (42 < 0)
→ FALSE ❌else if (42 > 0)
→ TRUE ✅cout << "POSITIVE";
The final else
block acts as a catch-all for anything not matched above.
Why we don't need to check a == 0:
If we reach the else
block, we already know:
a < 0
is FALSE (not negative)a > 0
is FALSE (not positive)a
must equal 0! ✅This could be written explicitly:
else if (a == 0) {
cout << "ZERO";
}
But it's redundant because there's no other possibility!
💡 Best practice: Use else
for the "everything else" case when you've covered all other possibilities.
⚠️ When to be explicit: If there's a chance of unexpected values (like in error handling), explicitly check all conditions.
Approach 1: Nested If-Else (Not recommended)
if (a == 0) {
cout << "ZERO";
} else {
if (a > 0) {
cout << "POSITIVE";
} else {
cout << "NEGATIVE";
}
}
❌ More complex, harder to read
Approach 2: Multiple Ifs (Wrong!)
if (a < 0) cout << "NEGATIVE";
if (a > 0) cout << "POSITIVE";
if (a == 0) cout << "ZERO";
❌ Checks all three conditions even after finding a match (inefficient)
Approach 3: Else-If Ladder (Our solution - Best!)
if (a < 0) cout << "NEGATIVE";
else if (a > 0) cout << "POSITIVE";
else cout << "ZERO";
✅ Clean, efficient, easy to understand
💡 Why else-if is best: It stops checking once a condition is met, making it both efficient and logical!
Where three-way classification is used:
Expanding to more categories:
The else-if ladder can handle any number of categories:
💡 This pattern is fundamental to decision-making in all programming!
💡 Tip: Use else-if ladders when you have mutually exclusive options - it's more efficient than multiple independent if statements!
#include <iostream>
using namespace std;
int main() {
long long a;
cin >> a;
if (a < 0)
cout << "NEGATIVE";
else if (a > 0)
cout << "POSITIVE";
else
cout << "ZERO";
return 0;
}