C語言之break和continue

break:cause the innermost enclosing loop or switch to be exited immediately.
continue: cause the next iteration of the enclosing for, while, or do loop to begin. The continue statement appliesonly to loops, not to switch. A continue inside a switch inside a loop causes the next loop iteration.

break:導致最內層的封閉循環或者switch立刻退出;
continue:導致for,while,或者do循環的下一個循環開始執行。continue只用在循環中,不用在switch中。一個循環中switch中的continue會導致下一個循環。
#include <stdio.h>

/* test the break and continue */
int main()
{
    int i;
    for (i = 0; i < 10; i++)
    {
        switch(i)
        {
            case 5:
                break;
        }
        printf("%d", i);
    }
    return 0;
}
輸出:0 1 2 3 4 5 6 7 8 9
break只是退出了switch,執行了後面的printf

#include <stdio.h>

/* test the break and continue */
int main()
{
    int i;
    for (i = 0; i < 10; i++)
    {
        switch(i)
        {
            case 5:
                continue;
        }
        printf("%d", i);
    }
    return 0;
}
輸出:0 1 2 3 4 6 7 8 9
continue直接執行下一個for循環,沒有執行printf

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章