Notes
Detailed Explanation of If Statements in C++
In C++, if statements are used to make decisions in your program. They allow you to execute a specific block of code only if a given condition is true. If the condition is false, that block of code is skipped, and execution continues with the next statements. This is a fundamental concept for controlling the flow of your program based on certain conditions.
Letβs break down how if statements work and explore how you can use them in your programs:
1. Basic If Statement:
The basic structure of an if statement in C++ involves:
- The keyword
if. - A condition inside parentheses
(). - A block of code (within curly braces
{}) that executes if the condition evaluates to true.
Syntax:
if (condition) {
// Code to be executed if the condition is true
}
Example:
Here, we ask the user for their age and check if they are allowed to enter a site based on their age.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "Welcome to the site!" << endl;
}
return 0;
}
If the user enters an age of 21, the output would be:
Welcome to the site!
If the user enters 12, there will be no output because the condition is false, and the block of code inside the if statement is skipped.
2. If-Else Statement:
The else statement is used when the condition is false. If the if condition is not true, the code inside the else block will execute.
Syntax:
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "Welcome to the site!" << endl;
} else {
cout << "You are not old enough to enter." << endl;
}
return 0;
}
For age = 12, the output would be:
You are not old enough to enter.
If age = 21, the output would be:
Welcome to the site!
3. Else-If Statement:
An else-if statement allows you to check multiple conditions. Itβs used when you want to evaluate a series of conditions in order, and execute the block of code that matches the first true condition.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if neither condition1 nor condition2 are true
}
Example:
Here, we check several conditions like if the user is underage, of age, or too old for a site.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age < 0) {
cout << "You haven't been born yet!" << endl;
} else if (age >= 100) {
cout << "You are too old to enter the site." << endl;
} else if (age >= 18) {
cout << "Welcome to the site!" << endl;
} else {
cout << "You are not old enough to enter." << endl;
}
return 0;
}
For age = -10, the output would be:
You haven't been born yet!
For age = 120, the output would be:
You are too old to enter the site.
For age = 21, the output would be:
Welcome to the site!
For age = 15, the output would be:
You are not old enough to enter.
4. Order of Conditions:
The order in which you write the if, else-if, and else statements matters. These conditions are checked sequentially from top to bottom. Once a true condition is found, the corresponding block of code is executed, and the program skips the rest of the conditions.
Example:
In this example, the first condition (age >= 100) would catch any age greater than or equal to 100, preventing further checks for eligibility to enter the site:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 100) {
cout << "You are too old to enter the site." << endl;
} else if (age >= 18) {
cout << "Welcome to the site!" << endl;
} else {
cout << "You are not old enough to enter." << endl;
}
return 0;
}
For age = 120, the output would be:
You are too old to enter the site.
If you had placed the age >= 100 condition after the else if (age >= 18), the program would mistakenly print "Welcome to the site!" for ages over 100 because the condition checking for ages over 100 would be missed.
5. Comparison Operators:
In the above examples, we used comparison operators to check conditions. Hereβs a list of common comparison operators in C++:
==: Equal to (checks if two values are equal).!=: Not equal to (checks if two values are not equal).<: Less than (checks if the value on the left is less than the one on the right).>: Greater than (checks if the value on the left is greater than the one on the right).<=: Less than or equal to (checks if the value on the left is less than or equal to the one on the right).>=: Greater than or equal to (checks if the value on the left is greater than or equal to the one on the right).
6. Logical Operators:
You can combine multiple conditions in an if statement using logical operators:
&&: Logical AND (both conditions must be true).||: Logical OR (at least one condition must be true).!: Logical NOT (reverses the result of a condition).
Example with logical operators:
#include <iostream>
using namespace std;
int main() {
int age;
bool has_permission;
cout << "Enter your age: ";
cin >> age;
cout << "Do you have permission? (1 for Yes, 0 for No): ";
cin >> has_permission;
if (age >= 18 && has_permission) {
cout << "Welcome to the site!" << endl;
} else {
cout << "You cannot enter the site." << endl;
}
return 0;
}
In this case, the user must be both at least 18 years old and have permission to enter.
Conclusion:
ifstatements are the building blocks of conditional logic in C++.- They allow your program to make decisions based on conditions.
- You can use
elseandelse-ifto handle multiple possible conditions and provide different outputs. - The order of your conditions matters because the first true condition is executed, and the rest are skipped.
This makes if statements essential for writing dynamic and interactive programs that respond to user input and other conditions.