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 break statement stops the switch after the matching case.
default runs 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.
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.
