Learn how to use C++ conditional statements like if, if-else, else-if, and nested if with clear examples, syntax, and real-world decision-making logic.
if StatementThe if statement executes a block of code only when its condition is true.
Syntax
if (condition)
{
// Code executes if the condition is true
}
#include <iostream>
using namespace std;
int main()
{
int marks = 85;
if (marks >= 80)
{
cout << "Excellent Performance!";
}
return 0;
}
Understand the code
int marks = 85; stores the student’s marks.marks >= 80 checks whether marks are at least 80.Output:
Excellent Performance!
If the marks are 65, the condition is false, so nothing is displayed.
85 to 75. Observe the output.marks >= 80 to marks >= 70."You are doing well!".Try it: Write a program that displays "Eligible for admission" if a student’s marks are 50 or above.
if-else StatementThe if-else statement provides two possible paths:
if: executes when the condition is true.else: executes when the condition is false.
#include <iostream>
using namespace std;
int main()
{
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 50)
{
cout << "Congratulations! You passed.";
}
else
{
cout << "You failed. Work harder.";
}
return 0;
}
Understand the code
cin >> marks; accepts marks from the user.Example output for input 65:
Enter your marks: 65
Congratulations! You passed.
"Pass" and "Fail".Try it: Write a program that checks whether a student is eligible to vote in a student election if the minimum age is 18.
else-if LadderUse else-if when you need to check multiple conditions.
#include <iostream>
using namespace std;
int main()
{
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 80)
{
cout << "Grade A - Excellent!";
}
else if (marks >= 70)
{
cout << "Grade B - Very Good!";
}
else if (marks >= 60)
{
cout << "Grade C - Good!";
}
else if (marks >= 50)
{
cout << "Grade D - Pass!";
}
else
{
cout << "Fail - Improve your performance.";
}
return 0;
}
Understand the code
The program checks conditions from top to bottom. When it finds the first true condition, it executes that block and skips the remaining conditions.
| Marks | Notification |
|---|---|
| 90 | Grade A - Excellent! |
| 75 | Grade B - Very Good! |
| 65 | Grade C - Good! |
| 55 | Grade D - Pass! |
| 35 | Fail - Improve your performance. |
Important: Write the highest range first. If marks >= 50 comes first, a student with 90 marks will receive the Grade D notification because the first condition is already true.
"Grade A+", for marks of 90 or above."Invalid marks" when marks are below 0 or above 100. Check invalid marks before the grade conditions.Try it: Write a program that displays a scholarship notification:
Full ScholarshipHalf ScholarshipQuarter ScholarshipNo ScholarshipAlso handle marks outside the range 0–100.
if StatementA nested if means an if statement inside another if statement.
It is useful when a decision depends on more than one condition, particularly when the second condition should be checked only after the first condition is true.
Real-life example: A student can register for a course only if they are admitted and have paid the required fee.
#include <iostream>
using namespace std;
int main()
{
int admitted, feePaid;
cout << "Are you admitted? Enter 1 for Yes, 0 for No: ";
cin >> admitted;
cout << "Have you paid the fee? Enter 1 for Yes, 0 for No: ";
cin >> feePaid;
if (admitted == 1)
{
if (feePaid == 1)
{
cout << "You can register for courses.";
}
else
{
cout << "Please pay your semester fee.";
}
}
else
{
cout << "Your admission is not confirmed.";
}
return 0;
}
Understand the code
First condition: Is the student admitted?
Yes (1)
Second condition
Has the student paid the fee?
Yes → Course registration allowed
No → Pay semester fee
No (0)
Notification
Admission not confirmed
The inner if checks the fee only within the admitted student’s branch. If admission is not confirmed, the program displays the admission message.
"You are eligible for enrollment."documentsVerified.Try it: Write a program for a university library:
| Statement | Purpose | Example |
|---|---|---|
if |
Executes code if a condition is true | Check whether marks are 80 or above |
if-else |
Chooses between two alternatives | Pass or fail |
else-if |
Chooses among multiple alternatives | Assign a grade |
Nested if |
Checks a condition inside another condition | Check admission, then fee payment |
✅ Question Statement
Write a C++ program that stores the value 2 in a variable x and checks whether the value is greater than 5, equal to 5, or less than 5.
Display the appropriate message based on the condition.
#include <iostream>
using namespace std;
int main() {
// Store a number in the variable x
int x = 2;
// Check if x is greater than 5
if (x > 5) {
cout << "x is greater than 5." << endl;
}
// Check if x is equal to 5
else if (x == 5) {
cout << "x is equal to 5." << endl;
}
// If both above conditions are false, x must be less than 5
else {
cout << "x is less than 5." << endl;
}
return 0;
}
✅ Question Statement
Write a C++ program that takes three numbers as input from the user and determines which of the three is the largest. Display the maximum number using an if-else.
#include <iostream>
using namespace std;
int main() {
int a, b, c;
// Input three numbers
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
cout << "Enter third number: ";
cin >> c;
if (a >= b && a >= c) {
cout << "The maximum number is: " << a << endl;
}
else if (b >= a && b >= c) {
cout << "The maximum number is: " << b << endl;
}
else {
cout << "The maximum number is: " << c << endl;
}
return 0;
}
Write a C++ program that takes a number as input and checks whether it is positive, negative, or zero. Display the appropriate message based on the entered number.
Hint: Use an if-else-if ladder.
Write a C++ program that takes an integer as input and determines whether it is even or odd. Display an appropriate message based on the result.
Hint: Use the modulus operator (%) to check the remainder when the number is divided by 2.
Write a C++ program that takes a student’s marks as input. If the marks are 50 or above, display “Eligible”; otherwise, display “Not Eligible”.
Hint: Use an if-else statement to check the marks.
Write a C++ program that takes a student’s marks as input and displays the corresponding grade according to the following criteria:
If the entered marks are less than 0 or greater than 100, display “Invalid Marks”.
Hint: Use an if-else-if ladder to check the marks and assign the appropriate grade. First, validate that the marks are between 0 and 100.
Write a C++ program that takes two numbers as input and compares them. Display the larger number. If both numbers are equal, display “Both numbers are equal.”
Hint: Use an if-else-if ladder to compare the two numbers and handle all three possible cases.
Write a C++ program that takes two inputs: a user verification flag and a password verification flag. Display “Login Successful” only if both flags indicate that the username and password are correct. Otherwise, display “Login Failed”.
Hint: Use nested if statements to check the user verification flag first and then the password verification flag.
Write a C++ program that takes a student’s marks and family monthly income as input. The student is eligible for a scholarship only if both of the following conditions are met:
Display “Scholarship Awarded” if the student meets both conditions. Otherwise, display “Not Eligible for Scholarship”.
Hint: Use nested if statements to check the marks first and then the family income.
Course Registration
Write a C++ program that takes three inputs to check whether a student can register:
Display “Registration Allowed” only if all three requirements are satisfied. Otherwise, display “Registration Not Allowed”.
Hint: Use nested if statements with appropriate else branches to check each requirement one by one.
Start with this program and make the requested changes.
#include <iostream>
using namespace std;
int main()
{
int marks = 75;
if (marks >= 80)
{
cout << "Excellent!";
}
else
{
cout << "Keep working hard.";
}
return 0;
}
Tasks
marks to 90 and observe the notification.else-if condition for marks of 70 or above."Fail" for marks below 50.if to check whether a student has paid the semester fee.