1. 程式人生 > >continue 在 switch中使用 (switch巢狀在while迴圈中)

continue 在 switch中使用 (switch巢狀在while迴圈中)

如果你在switch中使用continue,continue生效是對於while迴圈
如果你在switch中使用break,break生效是對於switch。

#include <stdio.h>

int main()
{
    int i_index;

    for (i_index = 0; i_index < 5; ++i_index) {
        switch (i_index) {
            case 0:
                printf("%d/n", i_index);
                break;
            case 1:
                printf("%d/n", i_index);
            case 2:
                printf("%d/n", i_index);
                break;
            case 3:
                printf("%d/n", i_index);
                break;
            case 4:
                printf("%d/n", i_index);
                break;
        }

        printf("out of switch/n");
    }

    return 0;
}

每次迴圈都會輸出out of switch

若將break換成continue, 則不會輸出out of switch。因為continue已經跳出本次迴圈了,continue生效是對於while迴圈。

break在switch中使用表示跳出本次選擇。