C++ if, if...else, and Nested if...else
In C++, the if
statement is used to make decisions in your program. It allows you to execute certain blocks of code based on whether a specified condition is true
or false
. C++ also provides the if...else
and nested if...else
statements to handle multiple conditions and complex decision-making processes.
In this blog post, we'll cover:
if
statement: A basic conditional statement.if...else
statement: Executes one block of code if the condition is true, and another if it's false.nested if...else
statement: Allows you to test multiple conditions inside other if
statements.if
Statement in C++The if
statement is the simplest form of decision-making in C++. It evaluates a condition and, if the condition is true
, executes the corresponding block of code.
if (condition) {
// Code to be executed if the condition is true
}
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10;
// Check if the number is positive
if (a > 0) {
cout << "The number is positive." << endl;
}
return 0;
}
Output:
The number is positive.
Explanation:
a > 0
is evaluated. Since it is true
, the statement inside the if
block is executed, printing "The number is positive."if...else
Statement in C++The if...else
statement provides an alternative block of code to be executed if the condition is false
. It helps in situations where you need to check a condition and handle both true and false cases.
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 a = -5;
// Check if the number is positive or negative
if (a > 0) {
cout << "The number is positive." << endl;
} else {
cout << "The number is negative." << endl;
}
return 0;
}
Output:
The number is negative.
Explanation:
a > 0
is evaluated. Since a
is -5, the condition is false
, so the code inside the else
block is executed, printing "The number is negative."else if
StatementThe else if
statement allows you to test multiple conditions. This is useful when you have more than two possibilities and want to check multiple conditions sequentially.
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if all previous conditions are false
}
Example:
#include <iostream>
using namespace std;
int main() {
int a = 0;
// Check if the number is positive, negative, or zero
if (a > 0) {
cout << "The number is positive." << endl;
} else if (a < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}
return 0;
}
Output:
The number is zero.
Explanation:
a > 0
) is false.a < 0
) is also false.else
block, printing "The number is zero."if...else
Statements in C++A nested if...else
statement is an if
or else
block inside another if
or else
block. It is used when you have multiple conditions that need to be checked sequentially and need more than one level of decision-making.
if (condition1) {
if (condition2) {
// Code to execute if both condition1 and condition2 are true
} else {
// Code to execute if condition1 is true, but condition2 is false
}
} else {
// Code to execute if condition1 is false
}
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
// Check if a is greater than b, and if b is positive
if (a > b) {
if (b > 0) {
cout << "a is greater than b and b is positive." << endl;
} else {
cout << "a is greater than b but b is non-positive." << endl;
}
} else {
cout << "a is not greater than b." << endl;
}
return 0;
}
Output:
a is greater than b and b is positive.
Explanation:
if
statement checks if a > b
. Since this is true, it proceeds to the nested if
statement.if
checks if b > 0
, which is also true, so it prints "a is greater than b and b is positive."if
, if...else
, and Nested if...else
These conditional statements are commonly used in real-world applications such as:
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 90) {
cout << "You got an A grade!" << endl;
} else if (marks >= 75) {
cout << "You got a B grade!" << endl;
} else if (marks >= 50) {
cout << "You got a C grade!" << endl;
} else {
cout << "You failed the exam." << endl;
}
return 0;
}
Explanation:
if...else if
statements to print the grade the user received.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are eligible to vote." << endl;
} else {
cout << "You are not eligible to vote." << endl;
}
return 0;
}
Explanation:
if...else
statement to determine voting eligibility.