zoukankan      html  css  js  c++  java
  • c语言中continue语句

    c语言中continue语句;执行continue语句后,循环体的剩余部分就会被跳过。

    例子;

    1、原始程序。输出矩形。

    #include <stdio.h>
    
    int main(void)
    {
        int i, j, height, width;
        puts("please input the height and width.");
        do
        {
            printf("height = "); scanf("%d", &height);
            if (height <= 0)
                puts("the range of height is > 0 ");
            printf("width = "); scanf("%d", &width);
            if (width <= 0)
                puts("the range of width is > 0 "); 
        }
        while (height <= 0 || width <= 0);
        
        for (i = 1; i <= height; i++)
        {
            for (j = 1; j <= width; j++)
            {
                putchar('*');
            }
            putchar('\n');
        }
        return 0;
    }

    当height和width都大于0时,程序正常输出矩形。 

    当height <= 0时,此时程序已经满足do语句重新执行的条件,但是任然执行width的输入,因此需要改进。以下使用break改进。

    2、使用break

    #include <stdio.h>
    
    int main(void)
    {
        int i, j, height, width;
        puts("please input the height and width.");
        do
        {
            printf("height = "); scanf("%d", &height);
            if (height <= 0)
            {
                puts("the range of height is > 0 ");
                break;
            }
            printf("width = "); scanf("%d", &width);
            if (width <= 0)
                puts("the range of width is > 0 ");
        }
        while (height <= 0 || width <= 0);
        
        for (i = 1; i <= height; i++)
        {
            for (j = 1; j <= width; j++)
            {
                putchar('*');
            }
            putchar('\n');
        }
        return 0;
    }

    当height和width都大于0时程序正常执行,但是当height小于等于0时,程序就直接退出了。 以下使用continue语句改进。

    3、使用continue

    #include <stdio.h>
    
    int main(void)
    {
        int i, j, height, width;
        puts("please input the height and width.");
        do
        {
            printf("height = "); scanf("%d", &height);
            if (height <= 0)
            {
                puts("the range of height is > 0 ");
                continue;
            }
            printf("width = "); scanf("%d", &width);
            if (width <= 0)
                puts("the range of width is > 0");
        }
        while (height <= 0 || width <= 0);
        
        for (i = 1; i <= height; i++)
        {
            for (j = 1; j <= width; j++)
            {
                putchar('*');
            }
            putchar('\n');
        }
        return 0;
        
    }

    当height小于等于0时,程序会跳过循环体的剩余部分。

    执行continue语句后,循环体的剩余部分就会被跳过。

    例子:

    #include <stdio.h>
    
    int main(void)
    {
        int i, j;
        puts("please input an integer.");
        printf("j = "); scanf("%d", &j);
        
        for (i = 1; i <= j; i++)
        {
            if (i == 6)
                break;
            printf("%d ", i);
        } 
        putchar('\n');
        puts("xxxxxyyyyyy");
        
        return 0;
    }

     break语句会直接终止循环。

    下面看continue。

    #include <stdio.h>
    
    int main(void)
    {
        int i, j;
        puts("please input an integer.");
        printf("j = "); scanf("%d", &j);
        
        for (i = 1; i <= j; i++)
        {
            if (i == 6)
                continue;
            printf("%d ", i);
        } 
        putchar('\n');
        puts("xxxxxyyyyyy");
        
        return 0;
    }

     continue语句则是仅仅跳出“6”的那一次执行过程。

  • 相关阅读:
    CDH 下线节点
    Linux下如何查看哪些进程占用的CPU内存资源最多
    CentOS7安装iptables防火墙
    Mysql 编译报错 g++: internal compiler error: Killed (program cc1plus) 解决办法
    Jenkins配置gitlab
    mysql连接卡死,很多线程sleep状态,导致CPU中mysqld占用率极高
    c++0.1-----基于对象知识大综合(非指针篇)
    c++0.0-----挖坑
    python0.16------构造函数/析构函数/self详解/重写/访问限制/对象属性和类属性/@property/运算符重载
    python0.15-----继承和多态
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14683218.html
Copyright © 2011-2022 走看看