当前位置:网站首页>C语言循环语句介绍(foe、while、do...while)
C语言循环语句介绍(foe、while、do...while)
2022-06-24 10:04:00 【华为云】
这篇文章主要是以案例题目的形式介绍循环语句的用法,使用场景。 通过以while循环、for循环、do..while循环,这3种不同的循环方式完成同样的练习题,比较3种循环语法的区别,加深理解。
1.1 练习题目讲解(练习:for)
1. 输出所有200~400以内能被3整除且个位数字为6的整数。
示例:
#include <stdio.h> /*1.输出所有200~400以内能被3整除且个位数字为6的整数 */ int main() { int i,data; for(i=200;i<=400;i++) { data=i%10; if(i%3==0 && data==6) { printf("%d ",i); } } return 0; } |
2. 判断10~100直接的所有素数。 (素数:只能被1和本身整除的数。比如: 2,3,7)
示例:
#include <stdio.h> /*2.判断10~100直接的所有素数。*/ int main() { int i,j,stat=0,cnt=0; for(i=10;i<100;i++) //4 7 { for(j=2;j<i;j++) { if(i%j==0) { stat=1; } cnt++; } if(stat==0) { printf("%d ",i); } else { stat=0; } } printf("\n"); printf("总次数: %d\n",cnt); //4725 return 0; } |
3. 输出所有的水仙数。(水花数是一个三位数,个十百位上的立方和等于该数本身 123)
示例:
#include <stdio.h> /*3. 输出所有的水仙数。*/ int main() { int i; int a,b,c; //123 for(i=100;i<=999;i++) { a=i/100; b=i%100/10; c=i%10/1; if((a*a*a+b*b*b+c*c*c) == i) { printf("%d ",i); } } printf("\n"); return 0; } |
4. 输出100~500直接的所有回文数
示例:
#include <stdio.h> /*4.输出100~500直接的所有回文数*/ int main() { int i; int a,b; //123 for(i=100;i<=500;i++) { a=i/100; b=i%10; if(a==b) { printf("%d ",i); } } printf("\n"); return 0; } |
5. 编程计算1*2*3+4*5*6+。。。。+97*98*99的值。
示例:
#include <stdio.h> /*1*2*3+4*5*6+。。。。+97*98*99*/ int main() { int i; int sum=0; for(i=1;i<=99;i+=3) //i+=3 =i=1+3 { sum=sum+(i)*(i+1)*(i+2); } printf("%d\n",sum); return 0; } |
6. 编程计算1!+2!+3!+。。+10!的值。(阶乘)
示例:
#include <stdio.h> /*6. 编程计算1!+2!+3!+。。+10!的值。(阶乘)*/ int main() { int i,sum=0,tmp=1; for(i=1;i<=10;i++) { tmp=tmp*i; sum+=tmp; } printf("%d\n",sum); return 0; } |
7. 输出 9*9 乘法口诀表
示例:
#include <stdio.h> /*7. 输出 9*9 乘法口诀表*/ int main() { int i,j; for(i=1;i<=9;i++) { for(j=1;j<=i;j++) { printf("%d*%d=%d ",j,i,i*j); } printf("\n"); } return 0; } |
1.2 循环语句相关练习题
1. 累加和:1+2+3+…+n
示例:
#include <stdio.h> /*1.累加和:1+2+3+…+n */ int main() { int n,sum=0,i; printf("请输入一个整数: \n"); scanf("%d",&n); for(i=1;i<=n;i++) { sum=sum+i; } printf("sum=%d\n",sum); return 0; } |
2. 百元买百鸡问题:公鸡每只5元,母鸡每只3元,小鸡3只一元,问一百元买一百只鸡有几种买法。
(先计算每种鸡最大的能买多少只)
示例:
#include<stdio.h> int main() { int m,g,x; int m_max; int g_max; int x_max; int q,cnt; //cnt=数量 q:钱 printf("请输入要买鸡的数量和人民币:\n"); scanf("%d%d",&cnt,&q);// 100 100 /*1. 判断输入的数据是否合理*/ if(q>0&&cnt>0) { m_max=cnt/3; // 母鸡 100 /3 =33 g_max=cnt/5; // 公鸡 100 /5 =20 x_max=(cnt/1)*3; // 小鸡 100 /1*3 =300 for(g=0;g<g_max;g++) // 循环判断可能 { for(m=0;m<m_max;m++) { x=cnt-g-m; //总数量-公鸡数量-母鸡数量 =小鸡的数量 if(x+g+m==cnt && x/3+g*5+m*3==q) //买鸡的总数等于总鸡数和总钱数等于买鸡的钱数就输出 { printf("公鸡=%d\t",g); printf("母鸡=%d\t",m); printf("小鸡=%d\t\n",x); } } } } else printf("输入有误\n"); return 0; } |
3. 求 n 的 m 次方。(n和m从键盘上输入)
示例:
#include <stdio.h> /*3.求 n 的 m 次方。(n和m从键盘上输入)*/ int main() { int n,m,i,data=1; //比如: n=3 m=2 printf("计算 n 的 m 次方,请输入n和m的值:"); scanf("%d%d",&n,&m); for(i=0;i<m;i++) { data=data*n; } printf("%d\n",data); return 0; } |
1.3 循环语句
1.3.1 for循环
for(<初始化>;<条件表达式>;<自增/自减>) { //语句; } //如果以上3个条件表达式不写(分号不能少),for就是死循环 |
只有0为为假。 非0都是为真。
for用于已知循环。
1.3.2 while循环
while(<条件表达式>) { <语句>; } |
while 用于未知循环。
示例:
#include<stdio.h> int main() { int i=0; while(i<5) { i++;// 1 2 3 4 5 } printf("%d",i); // return 0; } |
1.3.3 do..while循环
do { <语句>; }while(<条件表达式>); |
1.3.4 goto跳转语句
goto <标签>; 标签: |
goto跳转语句。可以在一个函数内随意跳转位置。 (一般不多用)
标签命名规则:与变量名称一样。
示例:
#include<stdio.h> int main() { int i=0; printf("1\n"); n: printf("2\n"); //5次 i++; if(i==5)goto k; goto n; k: printf("3\n"); return 0; } |
1.3.5 break语句
格式: break; |
功能:1. 跳出最近的一层循环。 2. 终止switch语句的执行
1.3.6 continue语句
格式:continue; |
功能:结束当前一次循环。
#include<stdio.h> int main() { int i=0,j=0,cnt=0; for(i=0;i<5;i++) { cnt++; if(i==3)continue; printf("8888\n"); //打印4次 } printf("%d\n",cnt);// return 0; } |
边栏推荐
- TP-LINK 1208路由器教程(2)
- Cloud vendor secondary virtualization restrictions
- Qt: judge whether the string is in numeric format
- What characteristics should a good design website have?
- 《opencv学习笔记》-- 感兴趣区域(ROI)、图像混合
- PHP短信通知+语音播报自动双呼
- Shell脚本(.sh文件)如何执行完毕之后不自动关闭、闪退?
- Influence of DEX optimization on arouter lookup path
- How to make a good video? What are the operation methods?
- Preparation for a series of courses on WordPress applet generation
猜你喜欢

Programmers spend most of their time not writing code, but...

Maui的学习之路 -- 开篇

“一次编写,运行各端”,高通重磅发布 AI 软件栈!

喜歡就去行動

齐次坐标的理解

【毕业季·进击的技术er】绕树三匝,何枝可依?

"One good programmer is worth five ordinary programmers!"

脚本之美│VBS 入门交互实战

Visual presentation of pictures effectively enhances the attraction of large screen

TP-LINK 1208 router tutorial (2)
随机推荐
Any and typevar make the automatic completion of IDE better
Redis
Opencv optical flow prediction and remap remapping function usage
What is the knowledge map? What does it do
Extremenet: target detection through poles, more detailed target area | CVPR 2019
“一次编写,运行各端”,高通重磅发布 AI 软件栈!
Centripetalnet: more reasonable corner matching, improved cornernet | CVPR 2020 in many aspects
Internship experience sharing in ByteDance 𞓜 ten thousand word job guide
Can text pictures be converted to word? How to extract text from pictures
Which is a good CAD drawing software? How to select good software
Disaster recovery series (II) -- enterprises' one-stop disaster recovery construction with the help of cloud platform?
Clickhouse deployment and basic usage 1
How does easydss use go fastdfs distributed file servers to reduce service pressure?
Reliable remote code execution (1)
How to use data analysis tools to deal with emergencies in retail industry
When the data security law comes, how can enterprises prepare for a rainy day? Tencent security has something to say
Cloud vendor secondary virtualization restrictions
Today's sleep quality record 76 points
08. Tencent cloud IOT device side learning - device shadow and attributes
PPT绘图相关,快捷键,美观度