C++ else Keyword
The else
keyword in C++ is used to provide an alternate block of code that executes if the condition in the preceding if
statement evaluates to false
. It is part of the if-else construct, which allows conditional execution of code blocks based on whether a condition is met.
The else
block ensures that one of two blocks of code will always execute, making it useful for handling situations where a decision must be made between two mutually exclusive outcomes.
Syntax
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
- condition
- A boolean expression evaluated to determine which block of code to execute.
- if block
- The block of code executed if the condition evaluates to
true
. - else block
- The block of code executed if the condition evaluates to
false
.
Examples
Example 1: Using if-else
for a Simple Condition
In this example, we will go through a basic if-else
construct to determine whether a number is positive or negative.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number >= 0) {
cout << "The number is positive." << endl;
} else {
cout << "The number is negative." << endl;
}
return 0;
}
Output:
Enter a number: -5
The number is negative.
Explanation:
- The user inputs a number.
- If the number is greater than or equal to 0, the
if
block executes, printing “The number is positive.” - If the number is less than 0, the
else
block executes, printing “The number is negative.”
Example 2: Using if-else
with Multiple Conditions
In this example, we will learn how to use an if-else
to determine if a number is even or odd.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 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.
Explanation:
- The user inputs a number.
- If the number is divisible by 2 (remainder is 0), the
if
block executes, printing “The number is even.” - If the number is not divisible by 2, the
else
block executes, printing “The number is odd.”
Example 3: Nested if-else
Statements
In this example, we will learn how to use a nested if-else
statement to categorize a number as positive, negative, or zero.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0) {
cout << "The number is positive." << endl;
} else if (number < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}
return 0;
}
Output:
Enter a number: 0
The number is zero.
Explanation:
- The user inputs a number.
- If the number is greater than 0, the first
if
block executes. - If the number is less than 0, the
else if
block executes. - If neither condition is true, the final
else
block executes, printing “The number is zero.”
Key Points about else
Keyword
- The
else
block executes when the condition in the precedingif
statement evaluates tofalse
. - It is always paired with an
if
statement and cannot exist independently. - Nested
if-else
statements can handle multiple conditions and outcomes. - It helps control the flow of the program by ensuring one of two code blocks executes based on a condition.