1、1234565.1while语句5.1while语句5.1while语句 例5.1计算s=1+2+3+n main()int i,s;i=1;s=0;/*设循环初值*/while(i=1e-4)s=s+item;sign=-sign;i+=2;item=(float)sign/i;printf(pi=%fn,4*s);5.2do-while语句功能功能特点:先执行,后判断。特点:先执行,后判断。5.2do-while语句5.2do-while语句5.2do-while语句v#include stdio.hv main()v v int n=0,s=0;/*设循环初值设循环初值*/v do v
2、v n=n+1;v s=s+n;v while(s1000);v printf(“n=%d,sum=%dn”,n-1,s-n);v 5.2do-while语句5.3for语句功能功能5.3for语句5.3for语句5.3for语句5.3for语句5.3for语句v#includev main()v v float x,min;v int i;v scanf(“%f”,&x);v min=x;v for(i=1;i10;i+)v v scanf(“%f”,&x);v if(xmin)min=x;v v printf(“min=%fn”,min);v 例5.8:计算n!。#include main
3、()float fact;int i,n;fact=1.0;scanf(“%d”,&n);for(i=1;i=n;i+)fact*=i;printf(“%d!=%.0fn”,n,fact);5.3for语句5.3for语句v#includev main()v int i;v long f1,f2,next;v f1=f2=1;v printf(“%8ld%8ld”,f1,f2);v for(i=3;i=25;i+)v v next=f1+f2;v f1=f2;v f2=next;v printf(“%8ld”,next);v if(i%5=0)printf(“n”);v v 5.4循环嵌套5.
4、4循环嵌套5.4循环嵌套5.4循环嵌套 *#include main()int i,j;for(i=1;i=5;i+)/*控制行数*/for(j=1;j=20-i;j+)/*控制每行开始空格个数*/printf(“”);for(j=1;j=2*i-1;j+)/*控制每行*的个数*/printf(“*”);printf(“n”);/*每输出一行后换行*/5.5break语句和continue语句 5.5break语句和continue语句 5.5break语句和continue语句 5.5break语句和continue语句#include stdio.h#include conio.h main()int n;for(n=100;n=200;n+)if(n%3=0)continue;printf(%d ,n);getch();5.5break语句和continue语句 v 等价:等价:v#include stdio.hv main()v v int n;v for(n=100;n=200;n+)v if(n%3!=0)printf(%d ,n);v getch();v 全国二级C考试大纲全国二级C考试大纲C语言程序设计案例引导教程不断学习,贵在坚持