循环语句

05. Loops - while

    5.01  Syntax

          while (表达式){
                  语句;    
                 语句;
        }

    5.02  Description

          01. 表达式是循环条件,语句为循环体;
          02. 首先求表达式的值,如果值为真(非0)时,执行循环体语句,并再次求表达式的值,这一过程一直进行下去,直到该表达式的值为0为止,随后继续执行语句后面的部分;
          03. while语句中的表达式一般是关系表达式或逻辑表达式,只要表达式的值为真(非0)即可继续循环;
          04. 循环体包括一个语句,一个以上的必须加上“{}”括起来,组成复合语句。

    5.03  Picture Show

    5.04  Example Source

          5.4.01  用while语句,求1+2+3+...+10的值?

                 





             #include <stdio.h>

                  int main (void)
                  {
                          int counter = 1, sum = 0;

                          while (counter <= 10) {
                                  sum = sum + counter;   //sum+=counter++;
                                  counter++;
                                    printf(“sum=%d ”,sum);
                                  // sum += counter++;
                          }
                          printf("The summary of 1+2+3...+10 = %d. ", sum);

                          return 0;
                  }

        

    5.05  Diligence

          01. 循环体的结束条件,否则循环将永远执行下去,成为一个死循环;
          02. 边界问题一定注意,是“<=”还是“<”。

06. Iteration - do while

    6.01  Syntax

          do
                 { 语句;}           
          while (表达式);         

    6.02  Description

          01. do-while 与 while循环的不同在于:它先执行循环中的语句,然后再判断表达式是否为真,如果为真则继续循环,如果为假,则终止循环;
          02. do-while循环至少要执行一次循环语句。

    6.03  Picture Show

    6.04  Example Source

        

          实例1. 用do-while语句,求1+2+3+...+10的值?

          #include <stdio.h>

          int main (void)
          {
                  int counter = 1; sum = 0;

                  do {
                          sum = sum + counter;
                          counter++;
                  } while (counter <= 10);

                  printf("The summary of 1+2+3...+10=%d. ", sum);

                  return 0;
          }

            6.05  Diligence

         

07. Loops - for

    7.01  Syntax

          for (表达式1; 表达式2; 表达式3)
                  语句;

        7.02  Description

          01. for语句使用最为灵活,它可以取代 while 语句;
              它等价于如下 while 语句:

              表达式1;
              while (表达式2) {
                      语句;
                      表达式3;
              }

          02. 表达式1和表达式3是赋值表达式或函数调用,表达式2是关系表达式;
          03. 一般地,表达式1为循环开始条件,表达式2为循环结束条件,表达式3为继续循环条件;
          04. for语句执行过程如下:
              01. 先求解表达式1;
              02. 求解表达式2,若其值为真,则执行for语句中指定的内嵌语句,然后执行下面第03.步若其值为假(0),则循环结束,转到下面第05步;
              03. 求解表达式3;
              04. 转回上面第02步继续执行;
              05. 循环结束,执行for语句下面的第一个语句。
          05. 省略表达式1和3就退化成了while语句,3个表达式全省略就成了一个“无限”死循环,分号“;”不可省略;
          06. 当while或for循环语句包含continue语句时,上述二者之间就不一定等价了;
          07. 在设计程序时到底选用while、do-while、for循环语句,主要取决于程序设计人员的个人偏好。

    7.03  Picture Show

    7.04  Example Source

          实例1. 下列语句执行后,k 的值是?

                 int j = 4, i, k = 10;

                 for (i = 2; i != j; i++)
                         k = k - i;

          实例2. 用for语句,求1+2+3+...+10的值?

                #include <stdio.h>

                int main (void)
                {
                        int counter = 1, sum = 0;

                        for (counter = 1; counter <= 10; counter++)
                                sum = sum + counter;

                        printf("The summary of 1+2+3...+10=%d. ", sum);

                        return 0;
                }

       

   08. continue, break, return, exit

    8.01  continue

          8.1.01  Syntax

                  continue;

          8.1.02  Description

                  01. Continue causes control to pass to the end of the innermost enclosing while, do, or for statement, at which point the loop continuation condition is re-evaluated.
                  02. 终止当前这一轮循环,即跳过循环体中位于 continue 后面的语句而立即开始下一轮循环。


        int i,j=3;
        for(i=0;i<5;i++)
        {       sleep(2);
                printf("start...... ");

                if(j==i)
                {       sleep(2);
                        printf("j==i ");
                        continue;
                }
                sleep(2);
                printf("ending...... ");

        }





          8.1.03  Picture Show

          8.1.04  Example Source

          8.1.05  Diligence

                  01. continue 语句只用于循环语句,不用于switch语句;
                  02. 某个循环包含 switch 语句中的 continue 语句,将导致进入下一次循环。

    8.02  break

          8.2.01  Syntax

                  break;

          8.2.02  Description

                  01. The break statement causes control to pass to the statement following the innermost enclosing while, do, for, or switch statement.
                  02. 一般,只用于循环语句或 switch 语句中,也可以用于结束最接近的一层循环(for,while,do while)或者一个 switch。

        

    8.04  exit

         
                  01. exit 用于在程序运行的过程中随时结束程序,exit 的参数是返回给 OS 的;
            


循环嵌套    

         练习:


    打印出1-50之间的数  
    打印出1-100之间所有的偶数,并显示个数

    打印出1-50之间所有7的倍数的数,并显示个数

    将1-10的数倒叙输出,不能用数组

    打印出1-100之间所有奇数的和。



要求:程序可以连续一直输入一个整数,直到输入了0停止整数的输入。然后呢,打印出你刚才连续输入的整数中偶数的和,和偶数的总个数还有平均数。


          int num,flat=1,sum=0,i=0;

        while(flat)
        {
        printf("enter a num: ");
        scanf("%d",&num);

        if(!num)        // if(num==0)
        {
        flat=0;
        printf("sum=%d ",sum);
        }
        else if(!(num%2))       //if((num%2)==0)
        {
                sum=sum+num;
                flat=1;

        }

*
**
***
****
*****

int n,i,j;
        printf("enter a num: ");
        scanf("%d",&n);

        for(i=1;i<=n;i++)  //行数
        {
                for(j=0;j<i;j++)        //个数
                {
                        printf("*");
                }
                printf(" ");
        }




   *
  **
 ***
****

int n,i,j,k;
        printf("enter a num: ");
        scanf("%d",&n);

        for(i=1;i<=n;i++)  //行数
        {
                for(k=0;k<n-i;k++)
                {
                        printf(" ");
                }
                for(j=0;j<i;j++)        //个数
                {
                        printf("*");
                }
                printf(" ");
        }

    
  水仙花数:例子:   153=1*1*1+5*5*5+3*3*3

n%10              (取个位)
n/10%10           (取十位)
n/100%10            (取百位)

找出100-999之间所有的水仙花数。

 int i,a,b,c,count=0;
        for(i=100;i<1000;i++)
        {
                a=i%10;
                b=i/10%10;
                c=i/100%10;

                if(i==(a*a*a+b*b*b+c*c*c))
                {
                        printf("%d ",i);
                        count++;
                }

        }
        printf(" count=%d ",count);


1,2,3,4能够组成多少个每位都互不相同的四位数?并将其打印出来。


int a,b,c,d,count=0;

        for(a=1;a<=4;a++)
        for(b=1;b<=4;b++)
        for(c=1;c<=4;c++)
        for(d=1;d<=4;d++)
        {
                if(a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)
                {
                        printf("%d%d%d%d ",a,b,c,d);
                        count++;
                }
        }

        printf("count=%d ",count);

原文地址:https://www.cnblogs.com/rainwz/p/4588933.html