功能:获取到值对应成立不同表达式。

优点:switch 语句执行效率比if语句要快,switch是通过开关选择的方式执行,而if语句是从开头判断到结尾。

缺点:不能判断多个区间。

案例

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    int score;
    scanf("%d", &score);
    // 根据键盘指令指定进入输出
    // 如果没有break会依次输出所有输出
    // default:所有没有满足条件的
    // case:表达式值
    // switch (表达式){case{表达式}}
    switch (score/10)
    {
    case 10:
        printf("非常完美\n");
        break;
    case 9:
        printf("优秀\n");
        break;
    case 8:
        printf("良好\n");
        break;
    case 7:
        printf("及格\n");
    default:
        printf("不及格\n");
        break;
    }
    return 0;
}
switch 使用案例

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-13
  • 2022-12-23
猜你喜欢
  • 2021-11-07
  • 2022-12-23
  • 2021-07-10
  • 2022-12-23
  • 2021-12-14
  • 2021-04-27
  • 2022-01-16
相关资源
相似解决方案