The if-else statement is used when a program needs to choose between two possible blocks of code. If the condition is true, the if block executes. If the condition is false, the else block executes.
The if-else statement allows a program to execute one block when a condition is true and another block when the condition is false.
if (condition) {
// code when condition is true
}
else {
// code when condition is false
}
The basic syntax of an if-else statement is:
if (condition) {
statement1;
}
else {
statement2;
}
Only one of the two blocks executes.
int age = 20;
if (age >= 18) {
std::cout << "Adult";
}
else {
std::cout << "Minor";
}
Since the age is 20, the condition is true and Adult is displayed.
The program first checks the condition.
if (condition) {
// True
}
else {
// False
}
int marks = 35;
if (marks >= 40) {
std::cout << "Pass";
}
else {
std::cout << "Fail";
}
Because 35 is less than 40, the output is Fail.
The modulus operator % can be used to check whether a number is even or odd.
int number = 7;
if (number % 2 == 0) {
std::cout << "Even";
}
else {
std::cout << "Odd";
}
int number = -10;
if (number >= 0) {
std::cout << "Positive";
}
else {
std::cout << "Negative";
}
Since -10 is not greater than or equal to zero, the else block executes.
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
if (age >= 18) {
std::cout << "You can vote";
}
else {
std::cout << "You cannot vote";
}
return 0;
}
int a = 50;
int b = 30;
if (a > b) {
std::cout << "A is greater";
}
else {
std::cout << "B is greater or equal";
}
The condition compares the values of a and b.
int a = 10;
int b = 10;
if (a == b) {
std::cout << "Both are equal";
}
else {
std::cout << "They are different";
}
int marks = 80;
if (marks > 50) {
std::cout << "Marks are greater than 50";
}
else {
std::cout << "Marks are 50 or less";
}
int age = 15;
if (age < 18) {
std::cout << "Minor";
}
else {
std::cout << "Adult";
}
int marks = 40;
if (marks >= 40) {
std::cout << "Pass";
}
else {
std::cout << "Fail";
}
The condition includes 40 because the operator is >=.
int age = 18;
if (age <= 18) {
std::cout << "Age is 18 or below";
}
else {
std::cout << "Age is above 18";
}
bool loggedIn = true;
if (loggedIn) {
std::cout << "Welcome";
}
else {
std::cout << "Please login";
}
If the Boolean value is true, the if block executes. Otherwise, the else block executes.
The && operator can combine two conditions.
int age = 25;
bool hasID = true;
if (age >= 18 && hasID) {
std::cout << "Entry allowed";
}
else {
std::cout << "Entry denied";
}
Both conditions must be true for the if block to execute.
The || operator is used when at least one condition can be true.
int day = 6;
if (day == 6 || day == 7) {
std::cout << "Weekend";
}
else {
std::cout << "Working day";
}
#include <iostream>
#include <string>
int main() {
std::string password;
std::cout << "Enter password: ";
std::cin >> password;
if (password == "1234") {
std::cout << "Correct password";
}
else {
std::cout << "Incorrect password";
}
return 0;
}
char grade = 'A';
if (grade == 'A') {
std::cout << "Excellent";
}
else {
std::cout << "Other grade";
}
Characters are written inside single quotes.
#include <iostream>
#include <string>
int main() {
std::string name = "Rahul";
if (name == "Rahul") {
std::cout << "Hello Rahul";
}
else {
std::cout << "Unknown user";
}
return 0;
}
Both the if and else blocks can contain multiple statements.
int marks = 75;
if (marks >= 40) {
std::cout << "Result: Pass" << std::endl;
std::cout << "Congratulations!";
}
else {
std::cout << "Result: Fail" << std::endl;
std::cout << "Try again.";
}
double amount = 1500;
if (amount >= 1000) {
std::cout << "Discount available";
}
else {
std::cout << "No discount";
}
The customer gets the message for a discount when the amount is at least 1000.
#include <iostream>
int main() {
int age;
std::cout << "Enter age: ";
std::cin >> age;
if (age >= 18) {
std::cout << "Eligible";
}
else {
std::cout << "Not eligible";
}
return 0;
}
#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";
}
else {
std::cout << "Invalid login details";
}
return 0;
}
An if statement can contain another if statement.
int age = 25;
bool hasID = true;
if (age >= 18) {
if (hasID) {
std::cout << "Entry allowed";
}
else {
std::cout << "ID required";
}
}
else {
std::cout << "You are underage";
}
#include <iostream>
int main() {
int number;
std::cout << "Enter number: ";
std::cin >> number;
if (number >= 0) {
std::cout << "Positive or zero";
}
else {
std::cout << "Negative";
}
return 0;
}
#include <iostream>
int main() {
int marks;
std::cout << "Enter marks: ";
std::cin >> marks;
if (marks >= 40) {
std::cout << "Pass" << std::endl;
if (marks >= 75) {
std::cout << "Good performance";
}
}
else {
std::cout << "Fail" << std::endl;
std::cout << "Work harder";
}
return 0;
}
For simple two-choice assignments, C++ also provides the conditional or ternary operator. However, a normal if-else statement is often easier for beginners to read.
int age = 20;
if (age >= 18) {
std::cout << "Adult";
}
else {
std::cout << "Minor";
}
The above logic can also be written using a ternary operator, but understanding if-else first is important.
Incorrect:
if (marks >= 40); {
std::cout << "Pass";
}
else {
std::cout << "Fail";
}
Avoid the unnecessary semicolon after the if condition.
| Part | Purpose |
|---|---|
if |
Executes code when the condition is true. |
else |
Executes code when the condition is false. |
== |
Checks equality. |
>, < |
Compare values. |
&& |
Combines conditions where both must be true. |
|| |
Combines conditions where at least one can be true. |
if (condition) {
// true block
}
else {
// false block
}
Question: What happens when the condition of an if statement is false in an if-else statement?