This lesson includes an expert video walkthrough — purchase once for a full year of unlimited replays to master every key point 🎬
Conditional Statements — Complex if-else
I. In-Class Exercises
Programming Exercises
- Grade Classification: L1091
- Xiao Ming's Summer Allowance: L1092
- Piecewise Function: L1093
- Calculate Postage: L1094
- Leap Year Check: L1095
Sample Code
1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int score;
6 cin >> score;
7 if (score >= 86) {
8 cout << "VERY GOOD" << endl;
9 } else {
10 if (score >= 60) {
11 cout << "GOOD" << endl;
12 } else {
13 cout << "BAD" << endl;
14 }
15 }
16 return 0;
17}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int score;
6 cin >> score;
7 if (score >= 90) {
8 cout << score * 3 << endl;
9 } else if (score >= 80) {
10 cout << score * 2 << endl;
11 } else if (score >= 70) {
12 cout << score << endl;
13 } else {
14 cout << 50 << endl;
15 }
16 return 0;
17}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 double x;
6 cin >> x;
7 double y = 0;
8 if (x < 5) {
9 y = -x + 2.5;
10 } else if (x < 10) {
11 y = 2 - 1.5 * (x - 3) * (x - 3);
12 } else {
13 y = x / 2 - 1.5;
14 }
15 printf("%.3f", y);
16 return 0;
17}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int weight;
6 char flag;
7 cin >> weight >> flag;
8 int postage = 8;
9 if (weight > 1000) {
10 weight -= 1000;
11 postage += weight / 500 * 4;
12 if (0 != weight % 500) {
13 postage += 4;
14 }
15 }
16 if ('y' == flag) {
17 postage += 5;
18 }
19 cout << postage << endl;
20 return 0;
21}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int year;
6 cin >> year;
7 if (0 == year % 400) {
8 cout << "Y" << endl;
9 } else {
10 if (0 == year % 100) {
11 cout << "N" << endl;
12 } else {
13 if (0 == year % 4) {
14 cout << "Y" << endl;
15 } else {
16 cout << "N" << endl;
17 }
18 }
19 }
20 return 0;
21}
22#include <bits/stdc++.h>
23using namespace std;
24
25int main() {
26 int year;
27 cin >> year;
28 if (0 == year % 400 || 0 != year % 100 && 0 == year % 4) {
29 cout << "Y" << endl;
30 } else {
31 cout << "N" << endl;
32 }
33 return 0;
34}II. Knowledge Summary
✨ Nested if-else Structure
Syntax
Nested if-else allows you to include another if-else structure inside an if or else block, used for scenarios requiring multi-level condition checks.
1if (boolean_expression_1) {
2 if (boolean_expression_2) {
3 Operation 1
4 } else {
5 Operation 2
6 }
7} else {
8 if (boolean_expression_3) {
9 Operation 3
10 } else {
11 Operation 4
12 }
13}Nested if-else Flowchart
正在渲染流程图...
Notes
Keep the following points in mind when using nested if-else:
- Any if structure within a nested structure can be used independently
- The boolean expression after if must be enclosed in parentheses
- If only one if-else structure follows an if-else, it's considered a single large operation and curly braces can be omitted (but beginners should always use curly braces to avoid errors)
Comprehensive Example: Grade Classification
Classify grades by score: first check if excellent, then check if passing.
正在渲染流程图...
1int score;
2cin >> score;
3if (score >= 90) {
4 if (score >= 95) {
5 cout << "Outstanding" << endl;
6 } else {
7 cout << "Excellent" << endl;
8 }
9} else {
10 if (score >= 60) {
11 cout << "Pass" << endl;
12 } else {
13 cout << "Fail" << endl;
14 }
15}✨ if-else if-else Chain
Syntax
The if-else chain checks multiple conditions sequentially and executes the corresponding code block. It is a simplified form of nested if-else.
1if (boolean_expression_1) {
2 Operation 1
3} else if (boolean_expression_2) {
4 Operation 2
5} else if (boolean_expression_3) {
6 Operation 3
7} else {
8 Operation 4
9}if-else Chain Flowchart
正在渲染流程图...
Notes
Keep the following points in mind when using if-else chains:
- The boolean expression after if must be enclosed in parentheses
- if-else if can be used without a final else (the last else is optional)
- If multiple lines of code need to be executed after if-else, they must be enclosed in curly braces
Comprehensive Example: Leap Year Check
Divisible by 400 → leap year; divisible by 100 but not 400 → not a leap year; divisible by 4 → leap year; otherwise → not a leap year.
正在渲染流程图...
1int year;
2cin >> year;
3if (year % 400 == 0) {
4 cout << "Leap Year" << endl;
5} else if (year % 100 == 0) {
6 cout << "Not a Leap Year" << endl;
7} else if (year % 4 == 0) {
8 cout << "Leap Year" << endl;
9} else {
10 cout << "Not a Leap Year" << endl;
11}