C语言之循环语句

1.前导程序

View Code 

C风格的读循环:status=scanf("%ld",&num);while(status==1){..status=scanf("%ld",&num);}   ----->【while(scanf("%ld",&num)==1){...}】
2.while循环

(1)是使用入口条件的有条件循环,只有位于判断条件之后的单个语句才是循环的部分(一个单独的分号也算作一个语句)。

复制代码
 1 //关于什么时候退出循环
 2 #include<stdio.h>
 3 int main(void)
 4 {
 5     int n=5;
 6     while(n<7)
 7     {
 8         printf("n=%d\n",n);
 9         n++;
10         printf("Now n=%d\n",n);
11     }
12     printf("The loop has finished.\n");
13     return 0;
14 }
复制代码

(2)使用关系运算符和表达式  如:<、>、<=、>=、!=、==(关系运算符可以用于字符的比较,但是不能比较字符串,可以比较浮点数,但要小心)

复制代码
 1 //浮点数大小的比较,利用fabs()函数。
 2 //不能使用关系运算符来比较字符串
 3 //浮点数比较重只能使用<和>,但舍入误差可能造成两个逻辑上相等的数不等
 4 #include<stdio.h>
 5 #include<math.h>//包含fabs()原型
 6 int main(void)
 7 {
 8     const double ANSWER=3.14159;//定义一个常量
 9     double response;
10     printf("What's is the value of pi?\n");
11     scanf("%lf",&response);
12     while(fabs(response-ANSWER)>0.0001)
13     {
14         printf("Try again!\n");
15         scanf("%lf",&response);
16     }
17     printf("Close enough!\n");
18     return 0;
19 }
复制代码

3.真值问题

  • 对于C来说一个真表达式的值为1,一个假表达式的为为0。更一般的,所有的非零值都被认为是真,只有0被认为是假。
  • 表达式实际上是数值的。
复制代码
 1 //哪些值为真?
 2 #include<stdio.h>
 3 int main(void)
 4 {
 5     int n=3;
 6     while(n)
 7         printf("%2d is true\n",n--);
 8     printf("%2d is flase.\n",n);
 9     n=-3;
10     while(n)
11         printf("%2d is true.\n",n++);
12     printf("%2d is flase.\n",n);
13     return 0;
14 }
15 //所有的非零值都被认为是真,只有0被认为是假。表达式实际上是数
复制代码

4.关于运算符

  • =和==   a=5;(把a的值赋为5)a==5;(检查a的值是否等于5,它的结果为0或1),【5=a;】这样的形式,便于编译器发现错误
  • 赋值运算符(=)<关系运算符优先级 <算数运算符如(+、—等)
  • 关系表达式的值为1或为0。
  • +=、-=、*=、/=和%=     (x*=3*y+12   等于   x=x*(3*y+12)
  • 逗号运算符【,】a、保证了呗分开的表达式按照从左到右的次序计算;b、整个逗号表达式的值是右边成员的值。x=(y=3,(z=++y+2)+5);的效果是首先把Y赋值给3,把Y递增为4,然后把4加上2,再把结果6赋值给z,接下来把z加5,最后把x赋为结果值11。

5.for循环

使用for循环实现一个立方表
  • for循环把初始化、测试、更新三个动作都放在了一起。如(:  for(secs=5;secs>0;secs--)  //注意,secs--是在单次循环结束之后才执行的。如果中间的控制表达式为空,则会被认为是真,则这个循环成为死循环。

6.do while循环 

  • do{....}while(判断条件);  
  • 退出条件循环,循环体内的语句至少执行一次,do while循环本身是一个语句,因此需要一个结束的分号。
复制代码
 1 //入口条件循环
 2 //应该把do while循环体仅仅用于那些至少需要执行一次循环的情况
 3 #include<stdio.h>
 4 int main(void)
 5 {
 6     const int secret_code=13;
 7     int code_entered;
 8 
 9     printf("To enter the triskaidekaphobia therapy club,\n");
10     printf("please enter the secret code number:");
11     scanf("%d",&code_entered);
12     while(code_entered!=secret_code){
13 
14         printf("To enter the triskaidekaphobia therapy club,\n");
15         printf("please enter the secret code number:");
16         scanf("%d",&code_entered);
17     }
18     
19     printf("Congratulations! You are cured!\n");
20     return 0;
21 }
复制代码

7.数组
(1)一个数组就是线性存储的一系列相同类型的值,数组元素编号是从0开始的。

(2)数组可以使任意数据类型的数组。

(3)如果字符数组包含了空字符\0,那么字符数组的内容就构成一个字符串。

复制代码
 1 //使用循环进行数组的处理
 2 #include<stdio.h>
 3 #define SIZE  10
 4 #define PAR   72
 5 int main(void)
 6 {
 7     int index,score[SIZE];
 8     int sum=0;
 9     float average;
10 
11     printf("Enter %d golf scores:\n",SIZE);
12     for(index=0;index<SIZE;index++)
13         scanf("%d",score[index]);//输入10个分数 注意这里没有用&
14     printf("The scores read in are as follows:\n");
15     for(index=0;index<SIZE;index++)
16         scanf("5%d",score[index]);//验证输入
17     printf("\n");
18     for(index=0;index<SIZE;index++)
19         sum+=score[index];//求他们的和
20     average=(float)sum/SIZE;
21     printf("sum of scores=%d,average=%.2f\n",sum,average);
22     printf("That's a handicap of %.0f.\n",average-PAR);
23     return 0;
24 }
复制代码

8.使用函数返回值的循环

(1)在调用函数中,可以把返回值赋给另一个变量;可以把他作为一个表达式的值;可以把它作为另一个数的参数,例如printf("%f",power(6.28,3));也可以直接忽略。

(2)使用具有返回值函数的基本要素:声明函数、调用函数、定义函数和使用return关键字。函数的前向声明是必不可少的。

(3)scanf()函数返回接受输入的项目个数,printf()函数返回打印字符的数目。

复制代码
 1 //计算数值的整数次幂
 2 #include<stdio.h>
 3 double power(double n,int p);
 4 int main(void)
 5 {
 6     double x,xpow;
 7     int exp;
 8 
 9     printf("Enter a number and the positive integer power");
10     printf("to witch.\n the number will be raised,Enter q.");
11     printf("to quit.\n");
12 
13     while(scanf("%lf%d",&x,&exp)==2)
14     {
15         xpow=power(x,exp);//函数调用
16         printf("%.3g to the power %d is %.5g\n",x,exp,xpow);
17         printf("Enter next pair of number or q to quit.\n");
18     }
19     printf("Hope you enjoyed this power trip--bye!\n");
20     return 0;
21 }
22 //求n的p次幂的函数
23 double power(double n,int p)//函数定义
24 {
25     double pow=1;
26     int i;
27 
28     for(i=1;i<=p;i++)
29         pow*=n;
30     return pow;//返回Pow的值
31 }
复制代码

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值