while循环

  1. #include "stdio.h"
  2. int main(void){
  3. long num;
  4. long sum = 0L;
  5. int status;
  6. printf("请输入status的值:");
  7. status = scanf("%ld",&num);
  8. printf("status:%d\n",status);
  9. /* while (status == 1)/*== 意思是"等于"*/
  10. /*{
  11. sum = sum + num;
  12. printf("next status:");
  13. status = scanf("%ld",&num);
  14. printf("status:%d\n",status);
  15. } */
  16. while (scanf("%ld",&num) == 1)
  17. {
  18. sum = sum + num;
  19. printf("next status:");
  20. status = scanf("%ld",&num);
  21. printf("status:%d\n",status);
  22. }
  23. printf("%-10ld",sum);
  24. return 0;
  25. }

终止循环

  1. /*when.c -- 终止循环*/
  2. #include "stdio.h"
  3. int main(void){
  4. int n = 5;
  5. while(n-- > 0){
  6. printf("%-5d",n);
  7. }
  8. return 0;
  9. }
  1. /*while1.c -- 注意花括号的使用*/
  2. #include "stdio.h"
  3. int main(void){
  4. int n = 5;
  5. while(n-- > 0)
  6. printf("%-5d",n); //只执行这一条语句
  7. printf("end\n");
  8. return 0;
  9. }

注意分号位置

  1. /*while2.c -- 注意分号的位置*/
  2. #include "stdio.h"
  3. int main(void){
  4. int n = 5;
  5. while(n-- > 0);
  6. printf("%d\n",n);
  7. return 0;
  8. }

浮点比较

  1. /*cmpflt.c -- 浮点数比较*/
  2. #include "stdio.h"
  3. int main(void){
  4. const double ANSWER = 3.1415926;
  5. double response;
  6. printf("输入response(double):");
  7. scanf("%lf",&response);
  8. while((response - ANSWER) > 0.0001){
  9. printf("Try again!\n");
  10. scanf("%lf",&response);
  11. }
  12. return 0;
  13. }

其他真值

  1. /*truth.c -- 其他真值*/
  2. #include "stdio.h"
  3. void main(void){
  4. char c = 'C';
  5. while(c == 'C'){
  6. printf("已经进入循环,请输入c的值:");
  7. scanf("%c",&c);
  8. printf("%-5c,%d",c,sizeof(c));
  9. }
  10. }

新的_Bool类型

  1. /*boolean.c -- 使用_Bool类型的变量*/
  2. #include "stdio.h"
  3. int main(void){
  4. long num;
  5. long sum = 0L;
  6. _Bool input_is;
  7. printf("是否开始循环?(1 or 0):");
  8. scanf("%ld",&input_is);
  9. while(input_is == 1){
  10. printf("num的值:");
  11. scanf("%ld",&num);
  12. sum = sum + num;
  13. printf("是否继续循环?(1 or 0):");
  14. scanf("%ld",&input_is);
  15. }
  16. printf("%ld",sum);
  17. }

优先级和关系运算符

运算符优先级

运算符优先级(优先级从高到低) 结合律
() 从左往右
-,+,++,—,sizeof 从右往左
*,/,% 从左往右
+,- 从左往右
<,>,<=,>= 从左往右
==,!= 从左往右
= 从右往左

for循环

  1. /*boolean.c -- 使用_Bool类型的变量*/
  2. #include "stdio.h"
  3. int main(void){
  4. long num;
  5. long sum = 0L;
  6. _Bool input_is;
  7. printf("是否开始循环?(1 or 0):");
  8. scanf("%ld",&input_is);
  9. while(input_is == 1){
  10. printf("num的值:");
  11. scanf("%ld",&num);
  12. sum = sum + num;
  13. printf("是否继续循环?(1 or 0):");
  14. scanf("%ld",&input_is);
  15. }
  16. printf("%ld",sum);
  17. }

逗号运算符

  1. /*postage.c -- 逗号运算符*/
  2. #include "stdio.h"
  3. int main(void){
  4. int i,j;
  5. for(i=0,j = 0;i+j<10;i++,j++)
  6. printf("i=%d,j=%d,i+j=%d\n",i,j,i+j);
  7. printf("i=%d,j=%d,i+j=%d\n",i,j,i+j);
  8. return 0;
  9. }

当Zone遇到for循环

  1. /*zeno.c -- 求序列的和*/
  2. #include "stdio.h"
  3. int main(void){
  4. int ct;
  5. double time,power_of_2;
  6. int limit;
  7. printf("limit的值:");
  8. scanf("%d",&limit);
  9. for(time=0, power_of_2=1, ct=1; ct <= limit;ct++, power_of_2=2.0){
  10. time += 1.0 / power_of_2;
  11. printf("time=%f,ct=%d\n",time,ct);
  12. // printf("power_of_2=%f\n",power_of_2);
  13. }
  14. return 0;
  15. }

出口条件循环: do..while()

  1. /*do_while.c -- 出口条件循环*/
  2. #include "stdio.h"
  3. int main(void){
  4. int i = 0;
  5. int a = 0;
  6. do{
  7. printf("a=%d\n",++a);
  8. if(a < 5)
  9. i++;
  10. }while(i == 0);
  11. return 0;
  12. }

嵌套循环

  1. /*rowsl.c -- 嵌套循环*/
  2. #include "stdio.h"
  3. int main(void){
  4. for(int i=1;i<10;i++){
  5. printf("i=%d\n",i);
  6. for(int j=1; j<=i; j++){
  7. printf("i=%d,j=%d,i*j=%d\n",i,j,i*j);
  8. }
  9. }
  10. }

数组

  1. /*scores_in.c -- 使用循环遍历数组*/
  2. #include "stdio.h"
  3. #define SIZE 10
  4. void arrSum();
  5. int main(void){
  6. arrSum();
  7. return 0;
  8. }
  9. void arrSum(){
  10. int arrInt[SIZE] = {1,32,56,35,10};
  11. for(int i=0; i<SIZE;i++){
  12. if(i == 5){
  13. break;
  14. }
  15. printf("arrInt[%d]:%5d\n",i,arrInt[i]);
  16. }
  17. }