Decision Making in C++
In real life, decisions are made based on conditions — for example, "if it is raining, take an umbrella." C++ programs work the same way. Decision-making statements allow the program to choose a course of action based on whether a condition is true or false.
The if Statement
The simplest decision-making statement. The block of code inside executes only when the condition is true.
if (condition) {
// code runs if condition is true
}
#include <iostream>
using namespace std;
int main() {
int temperature = 35;
if (temperature > 30) {
cout << "It is hot outside." << endl;
}
return 0;
}
Output:
It is hot outside.The if-else Statement
Adds an alternative path when the condition is false.
if (condition) {
// runs when condition is true
} else {
// runs when condition is false
}
int marks = 45;
if (marks >= 50) {
cout << "Passed!" << endl;
} else {
cout << "Failed. Try again." << endl;
}
Output:
Failed. Try again.The if-else if-else Chain
Used when there are more than two possible paths. Only the first matching condition runs.
int score = 75;
if (score >= 90) {
cout << "Grade: A" << endl;
} else if (score >= 75) {
cout << "Grade: B" << endl;
} else if (score >= 60) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F" << endl;
}
Output:
Grade: BNested if Statements
An if inside another if is called nested. Use with care to avoid deeply nested code.
int age = 20;
bool hasTicket = true;
if (age >= 18) {
if (hasTicket) {
cout << "Welcome to the event!" << endl;
} else {
cout << "No ticket. Entry denied." << endl;
}
} else {
cout << "Under age. Entry denied." << endl;
}
Output:
Welcome to the event!The switch Statement
When a variable needs to be compared against multiple specific values, switch is cleaner than a long chain of if-else if. It matches the expression against each case.
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code when no case matches
}
#include <iostream>
using namespace std;
int main() {
int day = 3;
switch (day) {
case 1: cout << "Monday" << endl; break;
case 2: cout << "Tuesday" << endl; break;
case 3: cout << "Wednesday" << endl; break;
case 4: cout << "Thursday" << endl; break;
case 5: cout << "Friday" << endl; break;
default: cout << "Weekend" << endl;
}
return 0;
}
Output:
WednesdayThe Importance of break in Switch
Without break, execution falls through to the next case — this is called fall-through. This can be a bug or intentional behavior depending on the design.
int x = 2;
switch (x) {
case 1: cout << "One
";
case 2: cout << "Two
"; // no break
case 3: cout << "Three
"; // also runs!
}
Output (due to fall-through):
Two
ThreeSwitch with Characters
char grade = 'B';
switch (grade) {
case 'A': cout << "Excellent" << endl; break;
case 'B': cout << "Good" << endl; break;
case 'C': cout << "Average" << endl; break;
default: cout << "Invalid grade" << endl;
}
Output:
GoodComparison: if-else vs switch
| Feature | if-else | switch |
|---|---|---|
| Condition type | Any boolean expression | Only integer or character equality |
| Range checks | Supported (>, <=) | Not supported |
| Readability | Good for complex conditions | Cleaner for multiple fixed values |
| Performance | May be slower with many branches | Often faster with many cases |
Key Takeaways
ifruns code when a condition is true.if-elseprovides an alternative path when the condition is false.if-else if-elsechains handle multiple conditions in order.switchis ideal for comparing a single variable against multiple fixed values.- Always include
breakin switch cases to prevent fall-through (unless it is intentional).
