The if statement is used to execute a block of code only when a specified condition is true. It is one of the most important decision-making statements in C++.
An if statement allows a program to make a decision.
if (condition) {
// code to execute
}
The code inside the braces executes only when the condition is true.
The basic syntax of an if statement is:
if (condition) {
statement;
}
The condition is written inside parentheses, and the statements to execute are written inside curly braces.
int age = 20;
if (age >= 18) {
std::cout << "You are an adult";
}
Because 20 is greater than or equal to 18, the message is displayed.
int number = 10;
if (number > 0) {
std::cout << "Positive number";
}
The condition checks whether the number is greater than zero.
The equality operator == can be used inside an if condition.
int number = 10;
if (number == 10) {
std::cout << "Number is 10";
}
The condition is true, so the message is displayed.
int number = 15;
if (number != 10) {
std::cout << "Number is not 10";
}
Since 15 is not equal to 10, the condition is true.
int marks = 80;
if (marks > 50) {
std::cout << "Good marks";
}
The statement executes because 80 is greater than 50.
int age = 15;
if (age < 18) {
std::cout << "Minor";
}
The condition is true because 15 is less than 18.
int marks = 40;
if (marks >= 40) {
std::cout << "Pass";
}
The condition is true because 40 is equal to the minimum required value.
int age = 18;
if (age <= 18) {
std::cout << "Age is 18 or below";
}
The condition is true because age is exactly 18.
An if block can contain multiple statements.
int marks = 80;
if (marks >= 40) {
std::cout << "Pass" << std::endl;
std::cout << "Congratulations!";
}
Both statements execute when the condition is true.
Curly braces are recommended when writing the body of an if statement, especially when there is more than one statement.
if (marks >= 40) {
std::cout << "Pass";
std::cout << " Well done!";
}
A Boolean variable can be used directly as a condition.
bool isLoggedIn = true;
if (isLoggedIn) {
std::cout << "Welcome";
}
The block executes because isLoggedIn is true.
Multiple conditions can be combined using the logical AND operator &&.
int age = 25;
bool hasID = true;
if (age >= 18 && hasID) {
std::cout << "Allowed";
}
Both conditions must be true.
The logical OR operator || allows the block to execute when at least one condition is true.
int day = 6;
if (day == 6 || day == 7) {
std::cout << "Weekend";
}
The logical NOT operator ! reverses a Boolean condition.
bool loggedIn = false;
if (!loggedIn) {
std::cout << "Please log in";
}
Since loggedIn is false, !loggedIn is true.
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
if (age >= 18) {
std::cout << "You are an adult";
}
return 0;
}
The program displays the message only when the entered age is at least 18.
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
if (number > 0) {
std::cout << "Positive number";
}
return 0;
}
The modulus operator can be used inside an if condition to check whether a number is even.
int number = 20;
if (number % 2 == 0) {
std::cout << "Even number";
}
int marks;
std::cout << "Enter marks: ";
std::cin >> marks;
if (marks >= 40) {
std::cout << "Pass";
}
The message is displayed when the marks are 40 or greater.
The AND operator can be used to check whether a value is inside a range.
int age = 25;
if (age >= 18 && age <= 60) {
std::cout << "Age is within the range";
}
int a = 50;
int b = 30;
if (a > b) {
std::cout << "A is greater than B";
}
The condition compares the values of two variables.
#include <iostream>
#include <string>
int main() {
std::string password = "admin123";
if (password == "admin123") {
std::cout << "Correct password";
}
return 0;
}
C++ std::string objects can be compared using the == operator.
An if statement can be placed inside another if statement. This is called a nested if.
int age = 25;
bool hasID = true;
if (age >= 18) {
if (hasID) {
std::cout << "Access allowed";
}
}
char grade = 'A';
if (grade == 'A') {
std::cout << "Excellent grade";
}
The condition compares the character with 'A'.
#include <iostream>
#include <string>
int main() {
std::string username;
std::string password;
std::cout << "Username: ";
std::cin >> username;
std::cout << "Password: ";
std::cin >> password;
if (username == "admin" && password == "1234") {
std::cout << "Login successful";
}
return 0;
}
The message is displayed only when both username and password match.
#include <iostream>
int main() {
double amount;
std::cout << "Enter purchase amount: ";
std::cin >> amount;
if (amount >= 1000) {
std::cout << "You are eligible for a discount";
}
return 0;
}
The message appears when the purchase amount is at least 1000.
#include <iostream>
int main() {
int marks;
std::cout << "Enter your marks: ";
std::cin >> marks;
if (marks >= 40) {
std::cout << "You passed the examination."
<< std::endl;
if (marks >= 75) {
std::cout << "You scored a high mark.";
}
}
return 0;
}
The second if statement is checked only when the first condition is true.
Example of a common mistake:
if (age >= 18); {
std::cout << "Adult";
}
The semicolon ends the if statement. Avoid adding an unnecessary semicolon after the condition.
| Part | Purpose | Example |
|---|---|---|
| if | Starts a conditional statement | if |
| Condition | Checks whether something is true | age >= 18 |
| { } | Contains statements to execute | { std::cout << "Adult"; } |
if (condition) {
// statements
}
Question: Which keyword is used to execute a block of code only when a condition is true?