Programs often need to make decisions. For example:
- If a student scores 40 or more, they pass.
- If someone’s age is 18 or more, they can vote.
- If a number is greater than 0, it is positive.
C++ conditional statements make these decisions possible.
The if Statement
The if statement runs a block of code when a condition is true.
Syntax
if (condition) {
// code to run
}
For example:
#include <iostream>
using namespace std;
int main() {
int age = 18;
if (age >= 18) {
cout << "You can vote.";
}
return 0;
}
Output:
You can vote.
Here, age >= 18 is true, so the message is displayed.
The if...else Statement
Sometimes you want one block of code to run when the condition is true and another block when it is false.
For this, use if...else.
#include <iostream>
using namespace std;
int main() {
int age = 16;
if (age >= 18) {
cout << "You can vote.";
} else {
cout << "You cannot vote.";
}
return 0;
}
Output:
You cannot vote.
The else if Statement
When you need to check more than one condition, you can use else if.
For example, let’s check a student’s marks:
#include <iostream>
using namespace std;
int main() {
int marks = 75;
if (marks >= 90) {
cout << "Grade A+";
} else if (marks >= 75) {
cout << "Grade A";
} else if (marks >= 60) {
cout << "Grade B";
} else {
cout << "Needs improvement";
}
return 0;
}
Output:
Grade A
C++ checks the conditions from top to bottom. Once it finds a true condition, it runs that block and skips the remaining conditions.
Nested if
An if statement can be placed inside another if statement. This is called a nested if.
#include <iostream>
using namespace std;
int main() {
int age = 20;
bool hasID = true;
if (age >= 18) {
if (hasID) {
cout << "You can enter.";
}
}
return 0;
}
Output:
You can enter.
Nested if statements are useful when one condition depends on another.
The switch Statement
The switch statement is useful when you want to choose between several fixed options.
For example:
#include <iostream>
using namespace std;
int main() {
int day = 2;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
default:
cout << "Invalid day";
}
return 0;
}
Output:
Tuesday
How switch Works
The value of day is 2, so C++ looks for:
case 2:
It finds the matching case and runs its code.
- The
breakstatement stops theswitchafter the matching case.
defaultruns when none of the cases match.
Ternary Operator
The ternary operator is a short way to write a simple if...else condition.
Syntax
condition ? valueIfTrue : valueIfFalse;
Example:
#include <iostream>
using namespace std;
int main() {
int age = 20;
string result = (age >= 18) ? "Adult" : "Minor";
cout << result;
return 0;
}
Output:
Adult
The ternary operator is useful for simple conditions, while if...else is usually easier to read for larger decisions.
A Simple Example
Let’s create a program that checks whether a number is positive, negative, or zero.
#include <iostream>
using namespace std;
int main() {
int number = -5;
if (number > 0) {
cout << "Positive number";
} else if (number < 0) {
cout << "Negative number";
} else {
cout << "Zero";
}
return 0;
}
Output:
Negative number
Key Points to Remember
- Conditional statements help a program make decisions.
ifruns code when a condition is true.elseruns when theifcondition is false.else ifchecks additional conditions.- A nested
ifcontains oneifstatement inside another. switchis useful for choosing between fixed options.breakstops aswitchcase.- The ternary operator provides a short way to write simple
if...elseconditions.
Mini Quiz
Mini Project
Mini Project: Restaurant Order System
Question:
Write a C++ program that allows a customer to select a food item and displays the selected item and its price.
- Display a menu with three food options.
- Ask the customer to enter a choice.
- Use
switchto identify the selected food item. - Display the price for the selected item.
- Use
defaultfor an invalid choice. - Use an
if-elsestatement to check whether the selected item costs more than200. - Display an appropriate message based on the price.
View Answer Code
#include <iostream>
using namespace std;
int main() {
int choice;
int price = 0;
cout << "===== Food Menu =====\n";
cout << "1. Pizza - Rs. 250\n";
cout << "2. Burger - Rs. 150\n";
cout << "3. Pasta - Rs. 200\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "You selected Pizza.\n";
price = 250;
break;
case 2:
cout << "You selected Burger.\n";
price = 150;
break;
case 3:
cout << "You selected Pasta.\n";
price = 200;
break;
default:
cout << "Invalid choice.";
return 0;
}
cout << "Price: Rs. " << price << "\n";
if (price > 200) {
cout << "This is a premium item.";
}
else {
cout << "This is an affordable item.";
}
return 0;
}
What’s Next?
In the next chapter, we will learn about C++ Loops and see how to repeat a block of code without writing the same statements again and again.
Frequently Asked Questions (FAQs)
Q1. What are C++ Conditional Statements?
C++ Conditional Statements allow a program to make decisions and execute different blocks of code based on whether a condition is true or false.
Q2. How does the C++ if statement work?
The C++ if statement executes a block of code when its condition is true. For example, if (age >= 18) checks whether the value of age is 18 or greater.
Q3. How does C++ if else work?
C++ if else provides two possible paths. The if block runs when the condition is true, while the else block runs when it is false.
Q4. When should you use the C++ switch statement?
The C++ switch statement is useful when you need to select one option from several fixed values, such as menu choices or day numbers.
Q5. What is the C++ ternary operator?
The C++ ternary operator is a short way to write a simple if...else condition using the ? and : symbols.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
