搜索
查看: 1469|回复: 0

[分享] C语言——break和continue跳出循环

[复制链接]

该用户从未签到

2586

主题

2613

帖子

0

蝴蝶豆

版主

最后登录
2021-3-16
发表于 2021-3-2 12:00:47 | 显示全部楼层 |阅读模式
首先说明: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:
微信图片_20210302115702.png
continue跳出本次循环,执行下一次循环。
break:
微信图片_20210302115708.png
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
微信图片_20210302115720.png
遇到break后跳出,继续匹配switchswitch 中 无break
微信图片_20210302115723.png
顺序执行每个case。

回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条

Archiver|手机版|小黑屋|论坛-意法半导体STM32/STM8技术社区

GMT+8, 2024-4-29 17:07 , Processed in 0.154472 second(s), 32 queries .

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表