This lesson includes an expert video walkthrough — purchase once for a full year of unlimited replays to master every key point 🎬
Conditional Statements — switch & Ternary Operator
I. In-Class Exercises
Programming Exercises
Sample Code
1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int n;
6 cin >> n;
7 switch (n) {
8 case 1:
9 cout << "one" << endl;
10 break;
11 case 2:
12 cout << "two" <<endl;
13 break;
14 case 3:
15 cout << "three" <<endl;
16 break;
17 case 4:
18 cout << "four" <<endl;
19 break;
20 case 5:
21 cout << "five" <<endl;
22 break;
23 case 6:
24 cout << "six" <<endl;
25 break;
26 case 7:
27 cout << "seven" <<endl;
28 break;
29 case 8:
30 cout << "eight" <<endl;
31 break;
32 case 9:
33 cout << "nine" <<endl;
34 break;
35 default:
36 cout << "out" << endl;
37 }
38 return 0;
39}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int weight;
6 cin >> weight;
7 weight /= 10;
8// weight = weight / 10;
9 switch (weight) {
10 case 0:
11 cout << "A" << endl;
12 break;
13 case 1:
14 cout << "B" << endl;
15 break;
16 case 2:
17 case 3:
18 cout << "C" << endl;
19 break;
20 case 4:
21 cout << "D" << endl;
22 break;
23 default:
24 cout << "E" << endl;
25 }
26 return 0;
27}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int a, b, c;
6 cin >> a >> b >> c;
7 int max = (a > b ? a : b) > c ? (a > b ? a : b) : c;
8// max = max > c ? max : c;
9 cout << max << endl;
10 return 0;
11}II. Knowledge Summary
✨ switch Structure
When to Use
When the variable being checked has a finite number of distinct values, the switch structure is suitable — for example, checking day of the week, month, grade level, etc.
Syntax
1switch (expression) {
2 case value1:
3 Operation 1;
4 break;
5 case value2:
6 Operation 2;
7 break;
8 case value3:
9 Operation 3;
10 break;
11 default:
12 Default operation;
13}switch Flowchart
正在渲染流程图...
Notes
Keep the following points in mind when using switch:
- Values after case can only be numbers or constants, not variables or expressions containing variables
- If two cases share the same operation, you cannot use an OR expression — you must write two cases (see the weekend example below)
- Each case must have a break; otherwise execution will fall through to the next case
- default is optional; break inside default is not required
Comprehensive Example: Day of the Week
Input an integer from 1-7, output the corresponding day.
正在渲染流程图...
1int day;
2cin >> day;
3switch (day) {
4 case 1:
5 cout << "Monday" << endl;
6 break;
7 case 2:
8 cout << "Tuesday" << endl;
9 break;
10 case 3:
11 cout << "Wednesday" << endl;
12 break;
13 case 4:
14 cout << "Thursday" << endl;
15 break;
16 case 5:
17 cout << "Friday" << endl;
18 break;
19 case 6:
20 case 7:
21 cout << "Weekend" << endl;
22 break;
23 default:
24 cout << "Invalid input" << endl;
25}✨ Ternary Operator
What is the Ternary Operator
The ternary operator (also called the conditional operator) is a concise way to write conditional logic in a single line, replacing simple if-else structures.
It uses the ? and : operators with three expressions, hence the name "ternary":
- Left of
?is the condition expression - Left of
:is the return value when the condition is true - Right of
:is the return value when the condition is false
Syntax
1// Syntax: condition ? expression1 : expression2
2// If condition is true → returns expression1
3// If condition is false → returns expression2
4
5int a = 10, b = 20;
6int max_ab = a > b ? a : b; // a > b is false, returns b, so max_ab = 20The ternary operator is essentially shorthand for if-else. The above code is equivalent to:
1int max_ab;
2if (a > b) {
3 max_ab = a;
4} else {
5 max_ab = b;
6}Notes
Keep the following points in mind when using the ternary operator:
- The ternary operator is suitable for simple conditional assignments; use if-else for complex logic to maintain readability
- Ternary operators can be nested, but avoid too many levels of nesting
- The ternary operator is an expression with a return value that can be used directly for assignment or output
Comprehensive Example: Check Odd or Even
int n;
cin >> n;
// Use ternary operator directly in output
cout << (n % 2 == 0 ? "Even" : "Odd") << endl;III. Homework
Programming Exercises
- Max and Min Characters: L1104
- Positive or Negative: L1105
- Days in a Month: L1106
- Maximum of Four Numbers: L1107