The if
statement in C++ is used for decision-making. It allows the program to execute a block of code only if a specified condition is true.
1️⃣ Basic if
Statement
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 0) { // Condition
cout << "The number is positive." << endl;
}
cout << "Program ended." << endl;
return 0;
}
Output (if input is 5
):
The number is positive.
Program ended.
📌 If the condition num > 0
is false, the cout
statement inside if
will be skipped.
2️⃣ if-else
Statement
The if-else
statement provides two possible execution paths:
if
block executes when the condition is true.else
block executes when the condition is false.
#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 (if input is 7
):
The number is odd.
3️⃣ if-else if-else
Ladder
When multiple conditions need to be checked, 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 (if input is 80
):
Grade: B
4️⃣ Nested if
Statements
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 (if input is 65
):
You are a senior citizen.
5️⃣ Using if
with Logical Operators
C++ supports logical operators to combine multiple conditions:
&&
(AND) → Both conditions must be true.||
(OR) → At least one condition must be true.!
(NOT) → Reverses the condition.
#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 (if input is 25
):
You are eligible to work.
6️⃣ Using if
with Ternary Operator (? :
)
A compact alternative to if-else
is the ternary operator (? :
).
#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 (if input is 9
):
The number is Odd.
Conclusion
- Use
if
for single conditions. - Use
if-else
for two-way decision making. - Use
if-else if-else
for multiple conditions. - Use nested
if
for more complex logic. - Use logical operators (
&&, ||, !
) to combine conditions. - Use ternary operator (
? :
) for short conditional expressions.
Would you like an example of handling user input errors (e.g., entering text instead of numbers)? 🚀
0 Comments