zoukankan      html  css  js  c++  java
  • 循环控制结构程序02 零基础入门学习C语言17

    第五章:循环控制结构程序02

     

    让编程改变世界

    Change the world by program


     

    while语句

     

    使用while语句应注意以下几点:

    1) while语句中的表达式一般是关系表达或逻辑表达式,只要表达式的值为真(非0)即可继续循环。 [codesyntax lang="c"]
    #include <stdio.h>
    
    void main()
    {
        int a=0,n;
    
        printf("n input n:    ");
        scanf("%d",&n);
    
        while( n-- )
        {
            printf("%d  ", a++*2);   //a++*2相当于a*2; a++
        }
    }
    [/codesyntax]   2) 循环体如包括有一个以上的语句,则必须用{}括起来,组成复合语句。  

    do-while语句

      [caption id="attachment_82" align="aligncenter" width="150"] do-while语句[/caption]   这个循环与while循环的不同在于:它先执行循环中的语句,然后再判断表达式是否为真, 如果为真则继续循环;如果为假, 则终止循环。 因此, do-while循环至少要执行一次循环语句。   [caption id="attachment_83" align="aligncenter" width="150"] do-while语句[/caption]   [codesyntax lang="c"]
    #include <stdio.h>
    
    void main()
    {
        int i,sum=0;
    
        i=1;
        do
        {
            sum=sum+i;
            i++;
        }while( i <= 100 )
    
        printf("%dn",sum);
    }
    [/codesyntax]  

    while和do-while循环比较

      [codesyntax lang="c"]
    #include <stdio.h>
    
    void main()
    {
        int sum=0,i;
        scanf(“%d”, &i);
        while( i<=10 )
        {
            sum=sum+i;
            i++;
        }
        printf(“sum=%d”,sum);
    }
    [/codesyntax] [codesyntax lang="c"]
    #include <stdio.h>
    
    void main()
    {
        int sum=0, i;
        scanf(“%d”, &i);
        do
        {
            sum=sum+i;
            i++;
        }while( i<=10 );
        printf(“sum=%d”,sum);
    }
    [/codesyntax] [buy] 获得所有教学视频、课件、源代码等资源打包 [/buy] [Downlink href='http://kuai.xunlei.com/d/LYKZVOQCOQPO']视频下载[/Downlink]
  • 相关阅读:
    Codeforces
    BZOJ
    BZOJ
    HDU
    Codeforces
    codeforces
    HDU-5441 Travel 离线-并查集
    codefoeces-1000E We Need More Bosses(tarjan+最长链)
    牛客网暑期ACM多校训练营(第五场)H-subseq 树状数组
    HDU-3533 Escape (BFS
  • 原文地址:https://www.cnblogs.com/LoveFishC/p/3845957.html
Copyright © 2011-2022 走看看