while循环
#include "stdio.h"
int main(void){
long num;
long sum = 0L;
int status;
printf("请输入status的值:");
status = scanf("%ld",&num);
printf("status:%d\n",status);
/* while (status == 1)/*== 意思是"等于"*/
/*{
sum = sum + num;
printf("next status:");
status = scanf("%ld",&num);
printf("status:%d\n",status);
} */
while (scanf("%ld",&num) == 1)
{
sum = sum + num;
printf("next status:");
status = scanf("%ld",&num);
printf("status:%d\n",status);
}
printf("%-10ld",sum);
return 0;
}
终止循环
/*when.c -- 终止循环*/
#include "stdio.h"
int main(void){
int n = 5;
while(n-- > 0){
printf("%-5d",n);
}
return 0;
}
/*while1.c -- 注意花括号的使用*/
#include "stdio.h"
int main(void){
int n = 5;
while(n-- > 0)
printf("%-5d",n); //只执行这一条语句
printf("end\n");
return 0;
}
注意分号位置
/*while2.c -- 注意分号的位置*/
#include "stdio.h"
int main(void){
int n = 5;
while(n-- > 0);
printf("%d\n",n);
return 0;
}
浮点比较
/*cmpflt.c -- 浮点数比较*/
#include "stdio.h"
int main(void){
const double ANSWER = 3.1415926;
double response;
printf("输入response(double):");
scanf("%lf",&response);
while((response - ANSWER) > 0.0001){
printf("Try again!\n");
scanf("%lf",&response);
}
return 0;
}
其他真值
/*truth.c -- 其他真值*/
#include "stdio.h"
void main(void){
char c = 'C';
while(c == 'C'){
printf("已经进入循环,请输入c的值:");
scanf("%c",&c);
printf("%-5c,%d",c,sizeof(c));
}
}
新的_Bool类型
/*boolean.c -- 使用_Bool类型的变量*/
#include "stdio.h"
int main(void){
long num;
long sum = 0L;
_Bool input_is;
printf("是否开始循环?(1 or 0):");
scanf("%ld",&input_is);
while(input_is == 1){
printf("num的值:");
scanf("%ld",&num);
sum = sum + num;
printf("是否继续循环?(1 or 0):");
scanf("%ld",&input_is);
}
printf("%ld",sum);
}
优先级和关系运算符
运算符优先级
运算符优先级(优先级从高到低) | 结合律 |
---|---|
() | 从左往右 |
-,+,++,—,sizeof | 从右往左 |
*,/,% | 从左往右 |
+,- | 从左往右 |
<,>,<=,>= | 从左往右 |
==,!= | 从左往右 |
= | 从右往左 |
for循环
/*boolean.c -- 使用_Bool类型的变量*/
#include "stdio.h"
int main(void){
long num;
long sum = 0L;
_Bool input_is;
printf("是否开始循环?(1 or 0):");
scanf("%ld",&input_is);
while(input_is == 1){
printf("num的值:");
scanf("%ld",&num);
sum = sum + num;
printf("是否继续循环?(1 or 0):");
scanf("%ld",&input_is);
}
printf("%ld",sum);
}
逗号运算符
/*postage.c -- 逗号运算符*/
#include "stdio.h"
int main(void){
int i,j;
for(i=0,j = 0;i+j<10;i++,j++)
printf("i=%d,j=%d,i+j=%d\n",i,j,i+j);
printf("i=%d,j=%d,i+j=%d\n",i,j,i+j);
return 0;
}
当Zone遇到for循环
/*zeno.c -- 求序列的和*/
#include "stdio.h"
int main(void){
int ct;
double time,power_of_2;
int limit;
printf("limit的值:");
scanf("%d",&limit);
for(time=0, power_of_2=1, ct=1; ct <= limit;ct++, power_of_2=2.0){
time += 1.0 / power_of_2;
printf("time=%f,ct=%d\n",time,ct);
// printf("power_of_2=%f\n",power_of_2);
}
return 0;
}
出口条件循环: do..while()
/*do_while.c -- 出口条件循环*/
#include "stdio.h"
int main(void){
int i = 0;
int a = 0;
do{
printf("a=%d\n",++a);
if(a < 5)
i++;
}while(i == 0);
return 0;
}
嵌套循环
/*rowsl.c -- 嵌套循环*/
#include "stdio.h"
int main(void){
for(int i=1;i<10;i++){
printf("i=%d\n",i);
for(int j=1; j<=i; j++){
printf("i=%d,j=%d,i*j=%d\n",i,j,i*j);
}
}
}
数组
/*scores_in.c -- 使用循环遍历数组*/
#include "stdio.h"
#define SIZE 10
void arrSum();
int main(void){
arrSum();
return 0;
}
void arrSum(){
int arrInt[SIZE] = {1,32,56,35,10};
for(int i=0; i<SIZE;i++){
if(i == 5){
break;
}
printf("arrInt[%d]:%5d\n",i,arrInt[i]);
}
}