Conditional Statements in C++: if, else if, switch
Learn conditional statements in C++ using if, else if, and switch to control decision-making and program flow.
In C++ programs, conditional statements are used to execute different code blocks depending on a condition.
Conditional statements control the flow of the program and form the decision-making mechanism.
In this article, we will learn how if, else if, else, and switch structures work with examples.
1. if Statement
The if statement executes the code block beneath it if the given condition is true.
If the condition is false, that block is skipped.
#include <iostream>
using namespace std;
int main() {
int age = 18;
if (age >= 18) {
cout << "You are an adult." << endl;
}
return 0;
}
In this example, if age >= 18 is true, it prints “You are an adult.”
If the condition is not met (for example, if age = 16), nothing is printed.
2. if – else Structure
The if – else structure is used to perform one action when a condition is true and another when it is false.
int age = 16;
if (age >= 18) {
cout << "You are an adult." << endl;
} else {
cout << "You are not an adult yet." << endl;
}
Here, when the condition is false, the second block (else) executes.
3. if – else if – else Chain
To test multiple conditions in sequence, you can use an else if chain.
The program executes the first condition that evaluates to true and then stops checking the rest.
#include <iostream>
using namespace std;
int main() {
int score = 75;
if (score >= 90) {
cout << "Letter Grade: AA" << endl;
} else if (score >= 80) {
cout << "Letter Grade: BA" << endl;
} else if (score >= 70) {
cout << "Letter Grade: BB" << endl;
} else if (score >= 60) {
cout << "Letter Grade: CC" << endl;
} else {
cout << "You failed." << endl;
}
return 0;
}
In this example, since the score is 75, the third condition is true and it prints “Letter Grade: BB”.
4. Combining Multiple Conditions
In if statements, you can combine multiple conditions using logical operators:
int age = 20;
bool hasId = true;
if (age >= 18 && hasId) {
cout << "Access granted." << endl;
} else {
cout << "Access denied." << endl;
}
&&→ Executes if both conditions are true.||→ Executes if at least one condition is true.!→ Negates the condition (reverses true/false).
5. switch Statement
The switch statement runs different code blocks depending on the value of a variable.
It’s especially useful for menus or fixed value selections and is more readable than multiple if – else statements.
#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;
}
In this example, since day = 3, the program executes case 3
and prints “Wednesday”.
6. Using break and default
- break: Exits the
switchblock once the matching case is executed. If omitted, the program continues to execute subsequentcasestatements. - default: Executes when no
casematches the variable value.
Example:
int choice = 2;
switch (choice) {
case 1:
cout << "Option 1 selected" << endl;
case 2:
cout << "Option 2 selected" << endl;
default:
cout << "Default block" << endl;
}
Since there is no break statement, the output will be:
Option 2 selected
Default block
This behavior is known as fall-through.
It is generally undesirable, so adding break; at the end of each case block is considered best practice.
7. Using switch with Characters or Enums
The switch structure can be used only with int, char, or enum types.
It cannot be used with double or string.
char op = '+';
switch (op) {
case '+':
cout << "Addition operation" << endl;
break;
case '-':
cout << "Subtraction operation" << endl;
break;
default:
cout << "Unknown operation" << endl;
}
Here, since op equals '+', it prints “Addition operation”.
8. Example Application: Simple Calculator
The following example combines if and switch structures to perform operations based on user input.
#include <iostream>
using namespace std;
int main() {
double a, b;
char op;
cout << "Enter the first number: ";
cin >> a;
cout << "Enter the second number: ";
cin >> b;
cout << "Choose an operation (+, -, *, /): ";
cin >> op;
switch (op) {
case '+':
cout << "Result: " << a + b << endl;
break;
case '-':
cout << "Result: " << a - b << endl;
break;
case '*':
cout << "Result: " << a * b << endl;
break;
case '/':
if (b != 0)
cout << "Result: " << a / b << endl;
else
cout << "Error: Division by zero!" << endl;
break;
default:
cout << "Invalid operation." << endl;
}
return 0;
}
This program takes two numbers and an operation from the user, calculates the result, and also checks for invalid input such as division by zero.
9. TL;DR
if→ Executes the block if the condition is true.else if→ Adds alternative conditions.else→ Runs if no condition is true.switch→ Cleaner for multiple fixed-value selections.break→ Exits from the switch block.- Combine conditions with
&&,||, and!. - All examples can be run in Visual Studio 2022 or GCC 11+ compilers.