β Detailed Explanation of C Language Switch Statement Exercises
Yesterday, I assigned 3 mandatory exercises, and today we will analyze them in detail.
β By mastering this, you will firmly grasp the real application scenarios of switch!
π Exercise 1: Number to Day of the Week (1~7)
π Input: An integer (1-7) π Output: Corresponding day of the week
β Analysis Approach
- 1~7 arefixed enumeration values β using switch is most suitable
- Use
<span>default</span>to avoid out-of-bounds input
β Complete Code
#include <stdio.h>
int main() {
int day;
printf("θ―·θΎε
₯δΈδΈͺζ°ε(1-7)οΌ");
scanf("%d", &day);
switch(day) {
case 1: printf("ζζδΈ\n"); break;
case 2: printf("ζζδΊ\n"); break;
case 3: printf("ζζδΈ\n"); break;
case 4: printf("ζζε\n"); break;
case 5: printf("ζζδΊ\n"); break;
case 6: printf("ζζε
\n"); break;
case 7: printf("ζζ倩\n"); break;
default: printf("θΎε
₯ιθ――οΌθ―·θΎε
₯1-7οΌ\n");
}
return 0;
}
β Example Execution
Input: 3
Output: ζζδΈ
π Must-master technique: default is used to handle invalid input
π Exercise 2: Letter Grade A~F Output Evaluation
π Input: Character (A B C D E F) π Output: Grade evaluation
β Analysis Approach
- Input may be lowercase β use case-insensitive writing
- First absorb the newline:
<span>scanf(" %c", &grade);</span>
β Complete Code
#include <stdio.h>
int main() {
char grade;
printf("θ―·θΎε
₯ζη»©ηηΊ§(A-F)οΌ");
scanf(" %c", &grade); //εε η©Ίζ ΌιΏε
ε车干ζ°
switch(grade) {
case 'A':
case 'a':
printf("δΌη§οΌ\n"); break;
case 'B':
case 'b':
printf("θ―ε₯½οΌ\n"); break;
case 'C':
case 'c':
printf("δΈηοΌ\n"); break;
case 'D':
case 'd':
printf("εζ ΌοΌ\n"); break;
case 'E':
case 'e':
case 'F':
case 'f':
printf("δΈεζ ΌοΌιθ¦εͺεοΌ\n"); break;
default:
printf("θΎε
₯ζ ζοΌ\n");
}
return 0;
}
β Example Execution
Input: b
Output: θ―ε₯½οΌ
π Technique: Merging case for uppercase and lowercase, structure isclear and elegant
π Exercise 3: Score Conversion to 5-Point Grading System
π Requirements
| Score Range | Grade |
|---|---|
| 90~100 | A |
| 80~89 | B |
| 70~79 | C |
| 60~69 | D |
| <60 | E |
β Analysis Approach: The score isrange judgment β is if more suitable? No! We can use the range compression technique π
β
score / 10 100/10 β 10 85/10 β 8 64/10 β 6
Thus, we can use switch for grouped judgment β
β Complete Code
#include <stdio.h>
int main() {
int score, level;
printf("θ―·θΎε
₯εζ°(0-100)οΌ");
scanf("%d", &score);
if(score < 0 || score > 100){
printf("θΎε
₯ιθ――οΌ\n");
return 0;
}
level = score / 10;
switch(level){
case 10: //ζ»‘ε100ε½ε
₯A
case 9: printf("ηηΊ§οΌA\n"); break;
case 8: printf("ηηΊ§οΌB\n"); break;
case 7: printf("ηηΊ§οΌC\n"); break;
case 6: printf("ηηΊ§οΌD\n"); break;
default: printf("ηηΊ§οΌE\n"); break;
}
return 0;
}
β Example Execution
Input: 86
Output: ηηΊ§οΌB
π Technique: β Range conversion to interval integers β Multiple case merging β Input validity check
β Summary
Through these three exercises, you have mastered:
| Technique | Applied Exercise Number |
|---|---|
| Switch fixed option branches | β Exercise 1 |
| Character judgment + reducing duplicate code | β Exercise 2 |
| Range judgment compression technique | β Exercise 3 |
| Default handling | β All |
| Input exception handling (robustness) | β Exercise 3 |
β
π― Switch is one of the most important structures in menus, categories, and device control. Knowing how to use it + optimize it = you have surpassed ordinary beginners βοΈ
π Next Lesson Preview
Coming soon π
β
π₯ Detailed Explanation of C Language Loop Structures (for/while/do-while) + Multiplication Table + Summation + Console Effects