C語言中,break和continue都是跳出循環,有啥區別?

首先說明:continue 只能用於循環語句中,而break可用於循環 switch 語句,兩者都是輔助循環;儘管如此,如果 switch 語句在一個循環中,continue便可作爲 switch 語句的一部分;這種情況下,就像在其他循環中一樣,continue 讓程序跳出循環的剩餘部分,包括 switch 語句的其他部分。

一般而言,程序進入循環後,在下一次循環測試之前會執行完循環體內部的所有語句。而continue和break語句可以根據循環體內部的測試結果來忽略一部分循環內容,甚至結束循環。

c 語言中循環語句有 3 種:while();        do     while();         for;且 3 種循環都可以使用 continue 和 break 語句

對於continue語句,執行到該語句時,會跳過本次迭代的剩餘部分,並開始下一輪迭代;但是若 continue 語句在嵌套循環的內部,則只會影響包含該語句(即 continue 語句)的內層循環(即內層循環的後面的語句不會被執行,而跳出內層循環後,外層循環內部的語句正常執行。);

然而對於 while() 和 do  while() 循環,執行 continue 語句後的下一個行爲是對循環的測試表達式求值,看代碼實例:

#include <stdio.h>
 
int main() {
 
    //while()
    char CH;
    int count=0;
    while(count < 10){
        CH = getchar();
        if(CH != ' ')
            continue;
        putchar(CH);
        count++;
    }
    printf("Hello, World!\n");
    return 0;
}

對於 for 循環,執行 continue 之後的下一個行爲是對更新表達式求值,然後是對循環測試表達式求值,下面的代碼示例包括了嵌套循環中使用 continue 的情形:

#include <stdio.h>
int main() {
    char ch;
    int cunt;
    int i;
    for(cunt=0;cunt<10;cunt++){
        ch = getchar();
        for(i=0;i<5;i++){
            if (ch != ' ')
                continue;
            putchar(ch);
            printf("我是內層循環的---小可愛!!!\n");
        }
        printf("我是外層循環的---小可愛!!!\n");
        printf("如果continue語句在嵌套循環內,則只會影響包含continue的內層循環,不影響外層循環!!!\n");
    }
    printf("Hello, World!\n");
    return 0;
}

對於 break 語句:

程序執行到循環中的break語句時,會終止包含它的循環,並繼續執行下一階段;若break位於嵌套循環內部,它隻影響包含它的當前循環。

比較 break 和 continue 對程序執行的不同之處,看下圖:

continue:

????continue跳出本次循環,執行下一次循環。

break:

????break跳出整個循環

下面看代碼 while 示例:

#include <stdio.h>
int main() {
    //while()
    char CH;
    int count=0;
    while(count < 10){
        CH = getchar();
        if(CH != ' ')
            break;
        putchar(CH);
        count++;
    }
    printf("Hello, World!\n");
    return 0;
}

for循環及嵌套循環示例:

注:只會直接跳出內層循環,外層循環正常執行

#include <stdio.h> 
int main() {
    char ch;
    int cunt;
    int i;
    for(cunt=0;cunt<10;cunt++){
        ch = getchar();
        for(i=0;i<5;i++){
            if (ch != ' ')
                break;
            putchar(ch);
            printf("我是內層循環的---小可愛!!!\n");
        }
        printf("我是外層循環的---小可愛!!!\n");
        printf("如果continue語句在嵌套循環內,則只會影響包含continue的內層循環,不影響外層循環!!!\n");
    }
    printf("Hello, World!\n");
    return 0;
}

要想外層循環一併終止;需要在外層在使用 break;

#include <stdio.h>
int main() {
    char ch;
    int cunt;
    int i;
    for(cunt=0;cunt<10;cunt++){
        ch = getchar();
        for(i=0;i<5;i++){
            if (ch != ' ')
                break;
            putchar(ch);
            printf("我是內層循環的---小可愛!!!\n"); 
        }
        if (ch != ' ')
            break;
        printf("我是外層循環的---小可愛!!!\n");
        printf("如果continue語句在嵌套循環內,則只會影響包含continue的內層循環,不影響外層循環!!!\n");
    }
    printf("Hello, World!\n");
    return 0;
}

在多重選擇 switch 語句中使用 continue 和 break的示例:

/* animals.c -- uses a switch statement */
#include <stdio.h>
#include <ctype.h>
int main(void)
{
    char ch;    
    printf("Give me a letter of the alphabet, and I will give ");
    printf("an animal name\nbeginning with that letter.\n");
    printf("Please type in a letter; type # to end my act.\n");
    while ((ch = getchar()) != '#')
    {
        if('\n' == ch)
            continue;
        if (islower(ch))     /* lowercase only          */
            switch (ch)
        {
            case 'a' :
                printf("argali, a wild sheep of Asia\n");
                break;
            case 'b' :
                printf("babirusa, a wild pig of Malay\n");
                break;
            case 'c' :
                printf("coati, racoonlike mammal\n");
                break;
            case 'd' :
                printf("desman, aquatic, molelike critter\n");
                break;
            case 'e' :
                printf("echidna, the spiny anteater\n");
                break;
            case 'f' :
                printf("fisher, brownish marten\n");
                break;
            default :
                printf("That's a stumper!\n");
        }                /* end of switch           */
        else
            printf("I recognize only lowercase letters.\n");
        while (getchar() != '\n')
            continue;      /* skip rest of input line */
        printf("Please type another letter or a #.\n");
    }                        /* while loop end          */
    printf("Bye!\n");    
    return 0;
}

在本例中 continue 的作用與上述類似,但是 break 的作用不同:它讓程序離開 switch 語句,跳至switch語句後面的下一條語句;如果沒有 break 語句,就會從匹配標籤開始執行到 switch 末尾;

注:C語言中的 case 一般都指定一個值,不能使用一個範圍;switch 在圓括號中的測試表達式的值應該是一個整數值(包括 char 類型);case 標籤必須是整數類型(包括 char 類型)的常量 或 整型常量表達式( 即, 表達式中只包含整型常量)。不能使用變量作爲 case 的標籤

switch中有 break

????遇到break後跳出,繼續匹配switch

switch 中 無break

????順序執行每個case。

1.國產替代摸不着門兒?快來回看兆易創新直播課!

2.開源的RISC-V能否成爲中國“缺芯”的解藥?

3.樹莓派Pico:僅4美元的MCU

4.MCU支持AI功能的多種原因~

5.2020年,我學習到的20條軟件工程準則~

6.狀態機思路在嵌入式開發中的應用~

免責聲明:本文系網絡轉載,版權歸原作者所有。如涉及作品版權問題,請與我們聯繫,我們將根據您提供的版權證明材料確認版權並支付稿酬或者刪除內容。

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