while int i sum i 1 sum 0














































- Slides: 46
while语句举例 int i, sum; i = 1; sum = 0; while(i<=100) { sum += i; /* 循环体缩进书写 */ i++; /* 使循环趋向结束 */ } Lecture Notes 4 Liu Yong, USTC © 2010
例 【例】 main(){ int a=0, n; printf("n input n: "); scanf("%d", &n); while (n--) printf("%d ", a++*2); } Lecture Notes 5 Liu Yong, USTC © 2010
例 【例】统计从键盘输入一行字符的个数。 #include <stdio. h> main(){ int n=0; printf("input a string: n"); while(getchar()!=‘n’) n++; /*只要从键盘输入的字符不是回车就继续循环 */ printf("%d", n); } Lecture Notes 6 Liu Yong, USTC © 2010
do-while语句举例 int i, sum; i = 1; sum = 0; do { sum += i; i++; }while(i<=100); Lecture Notes 8 Liu Yong, USTC © 2010
【例】while和do-while循环比较 /* program 2 */ main() {int sum=0, i; scanf(“%d”, &i); do {sum=sum+i; i++; } while(i<=10); printf(“sum=%d”, sum); } /* program 1 */ main() {int sum=0, i; scanf(“%d”, &i); while(i<=10) {sum=sum+i; i++; } printf(“sum=%d”, sum); } Lecture Notes 9 Liu Yong, USTC © 2010
for语句 在C语言中,for语句使用最为灵活,它完全可以取代 while 语句。 格式: for (expr 1; expr 2; expr 3) 语句 相当于: expr 1; while (expr 2) { 语句 expr 3; } Lecture Notes 10 Liu Yong, USTC © 2010
for语句举例(注意表达式 3的变化) int i, sum; for (i=1, sum=0; i<=100; i++) sum += i; for (i=1, sum=0; i<=100; ) sum += i++; for (i=0, sum=0; i++<100; ) sum += i; Lecture Notes 21 Liu Yong, USTC © 2010
for循环的嵌套 n 例 int main() { int i, j, k; printf("i j kn"); for (i=0; i<2; i++) for(j=0; j<2; j++) for(k=0; k<2; k++) printf(“%d %d %dn", i, j, k); } Lecture Notes 22 Liu Yong, USTC © 2010
break和continue语句 n break语句 l n continue语句 跳出本层循环 l while(i<j){. . . if(i>100) break; . . . } 结束本次循环 while(i<j){. . . if(i>100) continue; . . . } Lecture Notes 26 Liu Yong, USTC © 2010
break和continue语句 Lecture Notes 27 Liu Yong, USTC © 2010
continue语句举例 n 【例】 int main() { char c; while(c!=‘n’) /*不是回车符则循环*/ { c=getch(); if(c==0 X 1 B) continue; /*若按Esc键则不输出,继续下次循环*/ printf("%cn", c); } } Lecture Notes 28 Liu Yong, USTC © 2010
控制结构综合例题讲解 选择、循环 Lecture Notes 30 Liu Yong, USTC © 2010
【例1】输入三个整数,输出最大数和最小数。 int main(){ int a, b, c, max, min; printf("input three numbers: "); scanf("%d%d%d", &a, &b, &c); if(a>b) {max=a; min=b; } else {max=b; min=a; } if(max<c) max=c; else if(min>c) min=c; printf("max=%dnmin=%d", max, min); } 方法:先比较a和b,较大值存入max,较小值存入min, 比较c和max以及min的大小关系,修正min和max Lecture Notes 31 Liu Yong, USTC © 2010
【例2】计算器程序。用户输入运算数和四则运算符,输出 计算结果。 int main(){ float a, b; char c; printf("input expression: a+(-, *, /)b n"); scanf("%f%c%f", &a, &c, &b); switch(c){ case '+': printf("%fn", a+b); break; case '-': printf("%fn", a-b); break; case '*': printf("%fn", a*b); break; case '/': printf("%fn", a/b); break; default: printf("input errorn"); } } switch语句用于判断运算符,然后输出运算值。 当输入运算符不是+, -, *, /时给出错误提示 Lecture Notes 32 Liu Yong, USTC © 2010
【例3】判断m是否素数。 #include<math. h> #include<stdio. h> int main() { int m, i, k; scanf(“%d”, &m); k=sqrt(m); for(i=2; i<=k; i++) if(m%i==0)break; if(i>=k+1) printf(“%d is a prime numbern”, m); else printf(“%d is not a prime numbern”, m); } Lecture Notes 33 Liu Yong, USTC © 2010
【例3 续】求100至 200间的全部素数 #include<math. h> main() { int m, i, k, n=0; for(m=101; m<=200; m=m+2) { k=sqrt(m); for(i=2; i<=k; i++) if(m%i==0)break; Lecture Notes 35 if(i>=k+1) { printf(“%d”, m); n=n+1; } if(n%10==0)printf( “n”); } printf(“n”); } Liu Yong, USTC © 2010
【例4】用公式求π: #include<math. h> int main() { int s; float n, t, pi; t=1, pi=0; n=1. 0; s=1; while(fabs(t)>1 e-6) {pi=pi+t; n=n+2; s=-s; t=s/n; } pi=pi*4; printf("pi=%10. 6 fn", pi); } Lecture Notes 36 Liu Yong, USTC © 2010
Lecture Notes 38 Liu Yong, USTC © 2010
【例5】求最大公约数(循环求余) #include "stdio. h" int main() { int a, b, t, x; printf("intput 2 positive numbers: "); scanf("%d %d", &a, &b); printf("n. Greatest common devisor of %d and %d is: ", a, b); if (a <= 0 || b <= 0) return; t = a % b; while (t != 0) { a=b; b=t; t = a % b; } x = b; printf(" %dn", x); } Lecture Notes 39 Liu Yong, USTC © 2010
【例5 -】求最大公约数( 循环减法) while (t != 0) { if (t < 0) { t = -t; b = a; a = t; } else { a = b; b = t; } t = a - b; } printf(" %dn", a); } #include "stdio. h" int main() { int a, b, t; printf("intput 2 positive numbers: "); scanf("%d %d", &a, &b); printf("gcd of %d and %d is: ", a, b); if (a <= 0 || b <= 0) return; t = a - b; /* 转右 */ Lecture Notes 40 Liu Yong, USTC © 2010
【例5 -】求最大公约数(递归) #include "stdio. h" int gcd(int a, int b) int main() { int a, b, x; printf("intput 2 positive numbers: "); scanf("%d %d", &a, &b); printf("ngreatest common devisor of %d and %d is: ", a, b); if (a <= 0 || b <= 0) return; x = gcd(a, b); printf(" %dn", x); } { Lecture Notes if (a % b == 0) return (b); else return (gcd(b, a % b)); } 41 Liu Yong, USTC © 2010
【例6】求fibonacci数列前20项 #include <stdio. h> int main() { long int f 1, f 2; int i; f 1=f 2=1; for (i=1; i<=20; i++) { printf(%12 ld “, f 1, f 2); if (i%2==0) printf(“n”); f 1=f 1+f 2; f 2=f 2+f 1; } } Lecture Notes 42 Liu Yong, USTC © 2010
【例7】求水仙花数(是 3位数, 等于每位数字的立方和) main() { int i, j, k, n; printf("'水仙花数'是: "); for (n=100; n<1000; n++) { i=n/100; j=n/10 -i*10; k=n%10; if(i*100+j*10+k==i*i*i+j*j *j+k*k*k) printf("%d ", n); } printf("n"); Lecture Notes 43 int a=1, b=0, c=0,t; while(a<10) { b=0; while(b<10) { c=0; while(c<10) {if((t=a*100+b*10+c) ==a*a*a+b*b*b+c*c*c) printf("%dn", t); c++; } b++; } a++; } } Liu Yong, USTC © 2010
【例9】求s=a+aa+aaaa+aa. . . a的值(a是 0. . 9数字) #include <stdio. h> int main () { long int sn=0, t=0; int a, n, i; printf(“输入a和n: ”); scanf(“%d %d”, &a, &n); for(i=1; i<=n; i++) { t=t*10+a; sn=sn+t; Lecture Notes } printf(“sn=a+aa+aaaa+……=%ldn”, sn ); return 0; } Liu Yong, USTC © 2010 45