Welcome to The Coding College! In this tutorial, we’ll explore the if…else statement in C++, a fundamental building block for decision-making in programs. You’ll learn how to implement conditional logic, making your programs dynamic and responsive to user input or changing conditions.
What is an If…Else Statement?
The if…else statement allows your program to execute one block of code if a condition is true
and another block if the condition is false
.
Syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
condition
: A Boolean expression (evaluates totrue
orfalse
).- The if block executes when the condition is
true
. - The else block executes when the condition is
false
.
Basic If…Else Example
#include <iostream>
using namespace std;
int main() {
int number = 10;
if (number > 5) {
cout << "The number is greater than 5." << endl;
} else {
cout << "The number is not greater than 5." << endl;
}
return 0;
}
Output:
The number is greater than 5.
If…Else with User Input
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are an adult!" << endl;
} else {
cout << "You are a minor." << endl;
}
return 0;
}
Example Interaction:
Enter your age: 20
You are an adult!
If…Else If…Else Statement
For multiple conditions, use if…else if…else.
Syntax:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if none of the conditions are true
}
Example: Grading System
#include <iostream>
using namespace std;
int main() {
int score;
cout << "Enter your score: ";
cin >> score;
if (score >= 90) {
cout << "Grade: A" << endl;
} else if (score >= 80) {
cout << "Grade: B" << endl;
} else if (score >= 70) {
cout << "Grade: C" << endl;
} else if (score >= 60) {
cout << "Grade: D" << endl;
} else {
cout << "Grade: F" << endl;
}
return 0;
}
Example Interaction:
Enter your score: 85
Grade: B
Nested If…Else
You can nest if…else statements inside one another for complex conditions.
Example: Nested Conditions
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0) {
if (number % 2 == 0) {
cout << "The number is positive and even." << endl;
} else {
cout << "The number is positive and odd." << endl;
}
} else {
cout << "The number is not positive." << endl;
}
return 0;
}
Example Interaction:
Enter a number: 6
The number is positive and even.
Logical Operators in If…Else
Use logical operators to combine multiple conditions.
Example: Check Multiple Conditions
#include <iostream>
using namespace std;
int main() {
int x = 15;
if (x > 10 && x < 20) {
cout << "x is between 10 and 20." << endl;
} else {
cout << "x is not between 10 and 20." << endl;
}
return 0;
}
Output:
x is between 10 and 20.
If…Else with Boolean Variables
#include <iostream>
using namespace std;
int main() {
bool isRaining = true;
if (isRaining) {
cout << "Take an umbrella." << endl;
} else {
cout << "Enjoy the sunshine!" << endl;
}
return 0;
}
Output:
Take an umbrella.
Ternary Operator
For simple if-else conditions, you can use the ternary operator.
Syntax:
condition ? expression_if_true : expression_if_false;
Example:
#include <iostream>
using namespace std;
int main() {
int number = 10;
string result = (number > 5) ? "Greater than 5" : "Not greater than 5";
cout << result << endl;
return 0;
}
Output:
Greater than 5
Common Mistakes
- Missing Curly Braces: Always use braces
{}
for clarity, even for single-line blocks. - Misusing Else If: Remember, only one block in an if…else if…else chain will execute.
Summary
- If…else enables conditional logic in your programs.
- Use if…else if…else for multiple conditions.
- Combine conditions with logical operators for more complex checks.
- Use the ternary operator for simple conditions.
Learn More at The Coding College
Explore detailed tutorials, examples, and projects at The Coding College. Build strong programming skills today!
What’s Next?
- Learn about switch statements for better multi-condition handling.
- Dive into loops and control flow in C++.
- Explore function-based decision-making for modular programs.