switch break, or no break

break下:

#include <stdio.h>

int main()
{
    int x = 1, a = 0, b = 0;
    switch(x) {
        case 0: b++; break;
        case 1: a++; break;
        case 2: a++; b++; break;
    }
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

 

no break:

#include <stdio.h>

int main()
{
    int x = 1, a = 0, b = 0;
    switch(x) {
        case 0: b++;
        case 1: a++;
        case 2: a++; b++;
    }
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

 

 

试着对比下运行结果。

 

posted @ 2020-04-28 21:39  profesor  阅读(193)  评论(0编辑  收藏  举报