Master C++ Conditional Statements: if, else, switch, and Ternary Operator
This tutorial explains C++ conditional control structures—including if, if-else, else-if, switch, and the ternary ?: operator—detailing their syntax, usage, and providing clear code examples that demonstrate how to implement each construct for decision-making in programs.
In programming, conditional statements allow execution of different code blocks based on conditions. C++ provides several control structures, the most common being if, else, and switch. This tutorial details their syntax and usage with examples for beginners.
1. if statement
ifstatement is the basic conditional structure used to execute a specific code block when a condition is true.
Basic syntax
if (condition) {
// code executed when condition is true
}Example
#include <iostream>
using namespace std;
int main() {
int age = 18;
if (age >= 18) {
cout << "You are an adult." << endl;
}
return 0;
}Output:
You are an adult.2. if-else statement
When a different block of code should run if the condition is false, the else statement is used.
Syntax
if (condition) {
// code when true
} else {
// code when false
}Example
#include <iostream>
using namespace std;
int main() {
int age = 16;
if (age >= 18) {
cout << "You are an adult." << endl;
} else {
cout << "You are a minor." << endl;
}
return 0;
}Output:
You are a minor.3. else if statement (multiple conditions)
For checking multiple conditions, else if adds additional branches.
Syntax
if (condition1) {
// code for condition1
} else if (condition2) {
// code for condition2
} else {
// code when none match
}Example
#include <iostream>
using namespace std;
int main() {
int score = 85;
if (score >= 90) {
cout << "Grade: A" << endl;
} else if (score >= 80) {
cout << "Grade: B" << endl;
} else if (score >= 70) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F" << endl;
}
return 0;
}Output:
Grade: B4. switch statement
The switch statement is suitable for multiple fixed-value comparisons, offering a cleaner alternative to many if-else statements.
Syntax
switch (expression) {
case value1:
// code for value1
break;
case value2:
// code for value2
break;
default:
// code when no match
}Example
#include <iostream>
using namespace std;
int main() {
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;
}
return 0;
}Output:
Good!5. Ternary operator ( ?: )
C++ provides a concise conditional expression that can replace simple if-else statements.
Syntax
(condition) ? true_expression : false_expression;Example
#include <iostream>
using namespace std;
int main() {
int age = 20;
string status = (age >= 18) ? "Adult" : "Minor";
cout << status << endl;
return 0;
}Output:
AdultSummary
ifis used for single‑condition checks. if‑else provides a two‑way branch. else if handles multiple conditions. switch is clearer for matching many fixed values compared to multiple if‑else statements.
The ternary ?: operator simplifies simple conditional assignments.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
