• C Primer Plus(6) 中文版 第7章 C控制语句:分支和跳转 7.6 循环辅助:continue和break


    7.6 循环辅助:continue和break
    continue和break语句可以根据循环体的测试结果来忽略一部分循环内容,甚至结束循环。
    7.6.1 continue语句
    3种循环都可以使用continue语句。执行到该语句时,会跳过本次迭代的剩余部分,并开始下一轮迭代。如果continue语句在嵌套循环内,则只会影响包含该语句的内层循环。
    /* skippart.c  -- uses continue to skip part of loop */
    #include
    int main(void)
    {
        const float MIN = 0.0f;
        const float MAX = 100.0f;
        
        float score;
        float total = 0.0f;
        int n = 0;
        float min = MAX;
        float max = MIN;
        
        printf("Enter the first score (q to quit): ");
        while (scanf("%f", &score) == 1)
        {
            if (score < MIN || score > MAX)
            {
                printf("%0.1f is an invalid value. Try again: ",
                       score);
                continue;   // jumps to while loop test condition
            }
            printf("Accepting %0.1f:\n", score);
            min = (score < min)? score: min;
            max = (score > max)? score: max;
            total += score;
            n++;
            printf("Enter next score (q to quit): ");
        }
        if (n > 0)
        {
            printf("Average of %d scores is %0.1f.\n", n, total / n);
            printf("Low = %0.1f, high = %0.1f\n", min, max);
        }
        else
            printf("No valid scores were entered.\n");
        return 0;

    /* 输出:

    */ 

    注意,有两种方法可以避免使用continue,一是省略continue,把剩余部分放在一个else块中:
    if( score < 0 || score > 100 ){
        /*printf()语句*/ 
    } else{
        /*语句*/ 
    }
    另一种方式是,用以下格式来代替:
    if( score >= 0 && score <= 100 ){
        /*语句*/ 

    这种情况下,使用continue的好处是减少主语句组中的一级缩进。当语句很长或嵌套较多时,紧凑简洁的格式提高了代码的可读性。
    continue还可用作占位符。下面的循环读取并丢弃输入的数据,直至读到行末尾:
    while( getchar() != '\n' )
        ;
    当程序已经读取一行中的某些内容,要跳至下一行开始处时,这种方法很方便。问题是,一般很难注意到一个单独的分号。如果使用continue,可读性会更高:
    while( getchar() != '\n' )
        continue;
    如果用了continue没有简化代码反而让代码更复杂,就不要使用continue。例如:
    while( (ch = getchar()) != '\n' ){
        if( ch == '\t' ){
            continue;
        }
        putchar( ch );

    以上代码这样表示更简洁:
    while( (ch = getchar()) != '\n' ){
        if( ch != '\t' ){
            putchar( ch );
        }

    通常,在这种情况下,把if的测试条件的关系反过来便可避免使用continue。
    对于while和do while循环,continue语句后的下一个行为是对循环的测试表达式求值。
    count = 0;
    while( count < 10 ){
        ch = getchar();
        if( ch == '\n' ){
            continue;
        }
        putchar( ch );
        count++;

    对于for循环,执行continue后的下一个行为是对更新表达式求值,然后是对循环测试表达式求值。 
    for( count = 0; count < 10; count++ ){
        ch = getchar();
        if( ch == '\n' ){
            continue;
        }
        putchar( ch );
    }
    while循环的例子中,除了换行符,其余字符都显示;而本例中,换行符也计算在内,所以读取的10个字符中包含换行符。
    7.6.2 break语句
    程序执行到循环中的break语句时,会终止包含它的循环,并继续执行下一阶段。如果break语句位于嵌套循环内,它只会影响它的当前循环。
    break还可用于因其他原因退出循环的情况。
    循环计算矩形的面积。
    /* break.c -- uses break to exit a loop */
    #include
    int main(void)
    {
        float length, width;
        
        printf("Enter the length of the rectangle:\n");
        while (scanf("%f", &length) == 1)
        {
            printf("Length = %0.2f:\n", length);
            printf("Enter its width:\n");
            if (scanf("%f", &width) != 1)
                break;
            printf("Width = %0.2f:\n", width);
            printf("Area = %0.2f:\n", length * width);
            printf("Enter the length of the rectangle:\n");
        }
        printf("Done.\n");
        
        return 0;
    }

    /* 输出:

    */

    可以这样控制循环:
    while( scanf( "%f %f", &length, &width ) == 2 )
    但是,用break可以方便显示用户输入的值。
    和continue一样,如果用了break代码反而更复杂,就不要使用break。例如:
    while( (ch = getchar()) != '\n' ){
        if( ch == '\t' ){
            break;
        }
        putchar( ch );

    如果把两个测试条件放在一起,逻辑就更清晰了:
    while( (ch = getchar()) != '\n' && ch != '\t' )
        putchar( ch );
    break语句对于稍后讨论的switch语句而言至关重要。
    在for循环中的break和continue的情况不同,执行完break语句后会直接执行循环后面的第1条语句,连更新部分也跳过,嵌套循环内层的break只会让程序员跳出包含它的当前循环。 

  • 相关阅读:
    【Spark NLP】第 14 章:搜索引擎
    day03视图与逻辑
    PatchMatchNet 学习笔记 译文 深度学习三维重建
    【pandas小技巧】--数据转置
    今年副业比主业赚得多...
    从猿六年---C++笔试\面试的不成熟小建议来啦
    大白话 kafka 架构原理
    微软开源了一个 助力开发LLM 加持的应用的 工具包 semantic-kernel
    <vector模拟实现>——《C++初阶》
    关于找暑期实习后的一些反思
  • 原文地址:https://blog.csdn.net/weixin_40186813/article/details/126211744