第6章 C控制语句:循环(2)

发布于 2021-04-16  765 次阅读


6.8 出口条件循环:do while

while和for都是入口循环语句。
C语言还有出口条件循环(exit-condition loop),在每次循环以后再检查条件。保证了循环体中的内容至少被执行一次。

//  do_while.c  --出口条件循环
#include<stdio.h>
int main(void)
{
    const int secret_code = 13;
    int code_entered;

    do
    {
        printf("To enter the triskaidekaphobia therapy club,\n");
        printf("please enter the secret code number:");
        scanf("%d",&code_entered);
    }while(code_entered!=secret_code);
    printf("Congratulations! you are cured!\n");

    return 0;
}

file
用while可以写出等价的代码:

#include<stdio.h>
int main(void)
{
    const int secret_code = 13;
    int code_entered;
    printf("To enter the triskaidekaphobia therapy club,\n");
    printf("please enter the secret code number:");
    scanf("%d",&code_entered);
    while(code_entered!=secret_code)
    {
        printf("To enter the triskaidekaphobia therapy club,\n");
        printf("please enter the secret code number:");
        scanf("%d",&code_entered);
    }
    printf("Congratulations! you are cured!\n");

    return 0;
}

file
do while循环的通用形式;

do
    statement
while(expression);

statement可以是一条简单语句或复合语句
file

do
{
    提示用户输入密码
    读取用户输入密码
}while(用户输入的密码不等于密码);

避免这样的写法

do
{
    询问用户是否继续
    其他行为
}while(回答是yes);

小结

file

6.9 如何选择循环
for(;test;)
等价于
while(test)
让while更像for

初始化;
while(测试)
{

    其他语句;
    更新语句;
}
//等价于
for(初始化;测试;更新)
适合while:
while(scanf("%d",&num)==1)
涉及次数的:
for(count=1;count<=100;count++)

6.10 嵌套循环

嵌套循环(nested loop)。

//  rowsl.c --使用嵌套循环
#include<stdio.h>
#define ROWS 6
#define CHARS 10
int main(void)
{
    int row;
    char ch;

    for(row = 0; row <ROWS;row++)
    {
        for(ch='A';ch<('A'+CHARS);ch++)
        {
            printf("%c",ch);
        }
        printf("\n");
    }
    return 0;
}

file

6.10.1 程序分析

外层循环(outer loop)
内层循环(inner loop)
注意:嵌套循环中的内层循环再每次外层循环迭代时都执行完所有的循环。

6.10.2 嵌套变式

//  rows.c  --依赖外部循环的嵌套循环
#include<stdio.h>
int main(void)
{
    const int ROWS = 6;
    const int CHARS = 6;
    int row;
    int ch;

    for(row=0;row<ROWS;row++)
    {
        for(ch=('A'+row);ch<('A'+CHARS);ch++)
        {
            printf("%c",ch);
        }
        printf("\n");
    }
    return 0;
}

file

6.11 数组简介

数组(array)是按顺序存储的一系列相同的值。
整个数组有一个数组名,通过整数下标访问数组中单独的项或元素(element)
float debts[20];
可以把字符串储存在char类型的数组中。如果char类型的数组末尾包含一个表示字符串末尾的空字符‘\0’,则表示了该数组中的内容就成了一个字符串。
file
用于识别元素的数字被称为下标(subscript)、索引(indice)、偏移量(offset)

6.11.1 在for循环中使用数组

//  scores_in.c --使用循环处理数据
#include<stdio.h>
#define SIZE 10
#define PAR 72
int main(void)
{
    int index,score[SIZE];
    int sum = 0;
    float average;

    printf("Enter %d golf scores:\n",SIZE);
    for(index=0;index<SIZE;index++)
        scanf("%5d",&score[index]);
    printf("The score read in are as follows:\n");
    for(index=0;index<SIZE;index++)
        printf("%5d",score[index]);
    printf("\n");
    for(index=0;index<SIZE;index++)
        sum+=score[index];
    average = (float)sum/SIZE;
    printf("SUM of scores = %d,average = %.2f\n",sum,average);
    printf("That's a handicap of %.0f.\n",average-PAR);

    return 0;
}

file
第一:用define指令创建的明示常量(SIZE)来指定数组大小。
第二:for(index=0;index\<SIZE;index++) 可以很方便的处理数组
第三:程序重复显示输入的数组,是很好的编程习惯

6.12 使用函数返回值的循环示例

math.h库中

...
pow()
...
for(i=1;i<=p;i++)
{
    pow*=n;
}

编写一个有返回值的函数:

  1. 定义函数时,确定函数返回的类型;
  2. 使用关键字return表明待返回的值。

    double power(double n,int p)
    {
    double pow =1;
    int i;
    
    for(i=1;i<=p;i++)
    {
        pow*=n;
    }
    return pow;
    }

    要声明函数的返回类型,在函数名面前写出类型即可。关键词return表明该函数将它后面的值返回给主函数。返回值也可以是一个表达式。
    return 2*x+b;

//  power.c --计算数的整数幂
#include<stdio.h>
double power(double n,int p);
int main(void)
{
    double x,xpow;
    int exp;

    printf("Rnter the x and exp(q to quit):\n");

    while(scanf("%lf%d",&x,&exp)==2)
    {
        xpow=power(x,exp);
        printf("%.3g to %d  is %.5g\n",x,exp,xpow);
        printf("Rnter the x and exp(q to quit):\n");
    }
    printf("GOOD LUCK! THANTS FOR YOUR TRY!\n");

    return 0;
}
double power(double x,int exp)
{
    int p=1;
    int i;
    for(i=1;i<=exp;i++)
    {
        p*=x;
    }
    return p;
}

关键概念

file

小结

file


擦肩而过的概率