C Language Learning Notes: Summary of switch-case Statement Usage Techniques

“From today on, study hard and make progress every day”

Repetition is the best method for memory; spend one minute every day to remember the basics of C language.

“C Language Beginner’s Essential Knowledge Notes Series of 100 Articles”

26. Summary of switch-case Statement Usage Techniques: Multi-branch Selection

1. Basic Structure of switch Statement

The switch statement provides multi-branch selection functionality, which is clearer than multiple if-else statements:

switch (expression) {
    case constant1:
        statement1;
        break;
    case constant2:
        statement2;
        break;
    default:
        default_statement;
}

Execution Flow

  1. 1. Calculate the value of the expression
  2. 2. Match with each case constant
  3. 3. Execute the statement of the matching case
  4. 4. Exit upon encountering break or the end of switch

2. Basic Usage Examples

1. Simple Branch

int day = 3;
switch (day) {
    case 1: printf("Monday"); break;
    case 2: printf("Tuesday"); break;
    case 3: printf("Wednesday"); break;
    default: printf("Invalid input");
}

2. Character Judgment

char grade = 'B';
switch (grade) {
    case 'A': printf("Excellent"); break;
    case 'B': printf("Good"); break;
    case 'C': printf("Pass"); break;
    default: printf("Fail");
}

3. Key Syntax Features

1. Expression Limitations

  • • Must be of integer or enumeration type (int, char, enum, etc.)
  • • Cannot be floating-point or string

2. Case Constant Requirements

  • • Must be a compile-time constant expression
  • • Duplicate case values are not allowed

3. Importance of break

int x = 1;
switch (x) {
    case 1: printf("1");  // No break, continues to execute case 2
    case 2: printf("2");  // Outputs "12"
}

4. Advanced Usage Techniques

1. Multiple cases sharing code

int month = 2;
switch (month) {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        printf("31 days"); break;
    case 4: case 6: case 9: case 11:
        printf("30 days"); break;
    case 2:
        printf("28 or 29 days"); break;
}

2. Using Enumeration Types

enum Color {RED, GREEN, BLUE};
enum Color c = GREEN;
switch (c) {
    case RED: printf("Red"); break;
    case GREEN: printf("Green"); break;
    case BLUE: printf("Blue"); break;
}

3. Range Judgment Techniques

int score = 85;
switch (score / 10) {
    case 10: case 9: printf("A"); break;
    case 8: printf("B"); break;
    case 7: printf("C"); break;
    default: printf("D");
}

5. Comparison with if-else

Feature switch-case if-else
Applicable Scenarios Discrete value multi-branch Range judgment or complex conditions
Execution Efficiency Usually higher (jump table) Linear judgment
Readability Clear branches Difficult to read when nested complex
Type Limitations Only integer/enumeration Any expression

6. Common Errors

1. Forgetting break

switch (x) {
    case 1: printf("1");
    case 2: printf("2");  // When x=1, will output "12"
}

Correct approach:

switch (x) {
    case 1: printf("1"); break;
    case 2: printf("2"); break;
}

2. Variable case values

int y = 2;
switch (x) {
    case y: ...  // Error! case must be a constant
}

Correct approach:

#define Y_VALUE 2
switch (x) {
    case Y_VALUE: ...
}

Some students contacted me, wanting to have a study exchange group. I hesitated to create one before due to concerns about advertisements, but I think having a group would indeed be convenient, so I will try to create one this time.

If you need it, hurry up and join; the validity period is 7 days.

C Language Learning Notes: Summary of switch-case Statement Usage Techniques

———- End ———-

[Special Statement: All articles in this public account are original or authorized for publication by the author, please feel free to consume, the views are for learning reference only~~]

C Language Learning Notes: Summary of switch-case Statement Usage Techniques

C Language Learning Notes: Summary of switch-case Statement Usage Techniques

“If you like C, please like it”C Language Learning Notes: Summary of switch-case Statement Usage Techniques Click the bottom right corner to viewC Language Learning Notes: Summary of switch-case Statement Usage Techniques

Leave a Comment