else Statement in C++
In our previous discussions, we learned how to use the if statement to run code when a condition is true. But what happens when that condition is false? Instead of the program simply doing nothing, we can use the else statement to provide a secondary path.
Definitions
Before we look at the code, let’s define the core terms:
| Term | Description |
| else Statement | A keyword used to define a block of code that runs only when the preceding if condition is false. |
| Default Branch | The “last resort” or backup plan in a program that executes if no other specific conditions are met.
|
| Mutual Exclusivity | A logic rule where only one path can be taken—either the if block runs or the else block runs, but never both at the same time. |
How the else Statement Works
The Backup Plan
The else statement is an extension of the if statement. It doesn’t have its own condition in parentheses because it automatically handles every case where the if statement’s condition was false.
Relatable Example: Imagine you decide: “If the shop is open, I will buy milk. Else (otherwise), I will go back home”. If the shop is closed, you don’t just stand there forever; the else instruction tells you exactly what to do next.
Syntax and Structure
In C++, the else statement follows the closing brace of an if block:
if (condition) {
// Runs if true
} else {
// Runs if false
}
Mutual Exclusivity
If the condition is true, the if block runs and the else block is completely skipped. If the condition is false, the if block is skipped and the else block runs.
The “Catch-All” Role
In complex programs with many choices, the else statement often serves to catch errors or “invalid” data. For example, if you are checking for exam marks between 0 and 100, an else statement can be used to tell the user “Invalid Marks” if they enter a negative number or a number over 100.
Examples:
Program Description: This program will take an integer as input and tell the user whether he/she can cast a vote or not using if-else statements.
# include <iostream>
using namespace std;
int main(){
int age=0;
cout<<"Please enter your age: ";
cin>> age;
if(age>=18){
cout<<"You can cast vote!";
}
else{
cout<<"Oops! You are too young to cast vote.";
}
return 0;
}
Program Description: This program will take a username and password from the user and compare them with the predefined password for the user.
| UserName | Password |
| admin | admin@123 |
| Others (can be anyone other than admin) | pass@123 |
# include <iostream>
using namespace std;
int main(){
string username;
string password;
cout<<"Enter username: ";
cin>> username;
cout<<"Enter the password: ";
cin>>password;
if(username == "admin" && password == "admin@123"){
cout << "\t\t\t Welcome! \n\t\t\t Logging In.......";
}
else{
if(password=="pass@123"){
cout<<"\t\t\t Welcome! \n\t\t\t Logging In.......";
}
else{
cout<<"Invalid username or password!";
}
}
return 0;
}
Try It Yourself Challenges:
- The Logic Flip: If you have if (5 > 10), which block will run: the if or the else?
- The Code Cleaner: Write a program that asks for a user’s age. Use an if to check if they are 18 or older to “Vote.” Use an else to print “Too young to vote!” for everyone else.
- What will be the output of the program:
# include <iostream>using namespace std;int main(){int num1=5, num2=45;
if(num1>num2){
cout<<num1 <<“ is greater than ”<<num2;
}
else{
cout<< num2 <<“ is greater than ”<<num1;
}
return 0;
}
- Number Comperator:
Write a program that ask user for two numbers and use if-else statement to print greater number.
Point to Ponder:
Why do you think an else statement does not need a condition like if (condition)? How does this make your code easier to read?
Conclusion
The else statement is essential for creating robust programs that can handle any situation, not just the “ideal” ones. By providing a default path, you ensure your program always knows what to do next.
