C语言while循环标准格式&精简格式
先看一个例子
#include<stdio.h>
int main(void)
{
long num;
long sum = 0L;
int status;
printf("Please enter an integer to be summed:");
printf("(q to quit):");
status = scanf("%ld",&num);
while (status==1)
{
sum=sum+num;
printf("Please enter next integer (q to quit):");
status = scanf("%ld",&num);
}
printf("Those integers sum to %ld.\n",sum);
return 0;
}
标准格式部分解读:
-
获得第一个用于测试的值 status = scanf("%ld",&num);
-
当测试为真时 while (status==1)
? 处理值 sum=sum+num;
? 获得下一个值 printf("Please enter next integer (q to quit):");
status = scanf("%ld",&num);
标准格式
status = scanf("%ld", &num);
while (status == 1)
{
status = scanf("%ld", &num);
}
精简格式
while (scanf("%ld",&num)==1)
{
}
|