循环结构程序
约 319 字大约 1 分钟
2024-07-07
32.do……while语句
#include <stdio.h>
int main(void)
{
int i=1,s = 1,sum =0;
do
{
s=s*i;
sum=sum+s;
++i; //思考i++与++i
}
while(i<=5); //思考while条件
printf("%d\n",sum);
return 0;
}
33.break语句
#include <stdio.h>
int main(void)
{
int a,sum =0;
for(int i =1;i <=10;++i)
{
scanf("%d",&a);
sum+=a;
if(sum>=1000)
break; //break跳出循环语句
}
printf("总的捐款数目为:%d\n",sum);
return 0;
}
34.continue语句
#include <stdio.h>
int main(void)
{
for(int i=1;i<=100;++i)
{
#if 0
if(i%3==0)
continue;
printf("%d\n",i);
#else
if(i%3!=0)
printf("%d\n",i);
#endif
}
return 0;
}
35.循环习题1
#include <stdio.h>
int main(void)
{
for(int j=1;j<=4;++j)
{
for(int i =1;i<=5;++i)
{
j*=i;
printf("%5d\n",j);
}
putchar('\n');
}
return 0;
}
36.循环习题2
#include <stdio.h >
int main(void)
{
int a =1, b= 1,c,d=1;
for(int i=3;d<=10;++i)
{
c = a +b;
a =b;
b = c;
#if 0
if(c%3==0)
{
printf("%d",c);
++d;
}
#else
if(c%2==0)
continue;
printf("%d\n",c);
#endif
}
return 0;
}
37.循环习题3
#include <stdio.h>
int main(void)
{
char a,b,c;
for(char i ='x';i<='z';++i)
{
a=i;
for(char j='x';j<='z';++j)
{
if(i==j)
continue;
b=j;
for(char k='x';k<='z';++k)
{
if(k==i || k==j)
continue;
c=k;
if(a!='X' && c!='X' && c!='Z')
printf("Aand%c,Band%c,Cand%ccompete\n",a,b,c);
}
}
}
return 0;
}