Detailed Explanation of C Language Switch Statement Exercises

βœ… 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

Leave a Comment