<C Primer Pus>6 Using Relational Operators and Exprssions

本文通过几个具体的C语言代码示例介绍了如何使用while循环来实现不同的功能,包括数字比较、字符计数及浮点数累加等。文章强调了在进行浮点数比较时应注意精度问题,并给出了正确的代码实践。
 1 while(number < 6){
 2     printf("Your number is too small.\n");
 3     scanf("%d", &number);                
 4 }
 5 
 6 
 7 while(ch != '$'){
 8     count++;
 9     scanf("%c", &ch);
10 }    
11 
12 
13 while(scanf("%f", &num) == 1){
14     sum = sum +num;
15 }
 1 #include <stdio.h>
 2 #include <math.h>
 3 int main(void){
 4     const double ANSWER = 3.14159;
 5     double respone;
 6 
 7     scanf("%lf", &respone);
 8     while (fabs(respone - ANSWER) > 0.0001){
 9         printf("Try again.\n");
10         scanf("%lf", &respone);
11     }
12 
13     printf("Close enough.\n");
14 
15     return 0;
16 }

REMEBER :

You should limit yourself to using only < and > in floating-point comparisons.

while(status == 1)  is ture.  and  while(status = 1) is ture ....so advice to write like that  while(1 == status).

转载于:https://www.cnblogs.com/michael2016/p/6658693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值