当前位置:网站首页>C语言循环结构程序
C语言循环结构程序
2022-04-23 05:48:00 【Chshyz】
while循环、do while循环、for循环、break终止、continue结束
环境:CentOS7;
循环条件:①循环体;②循环结束条件;
特点:计算机只处理有条件的循环(有效性、确定性和有穷性)。
- while循环(先判断再循环,内嵌语句)
例子1:1+2+3+…+100
#include <stdio.h>
int main() {
int i, sum = 0;
i = 1;
while (i <= 100) {
sum += i;
i++;
}
printf ("%d\n", sum);
return 0;
}
[root@chenshuyi c]# gcc -o while while.c
[root@chenshuyi c]# ./while
5050
- do while(先循环再判断)
例子1:1+2+3+…+100
#include <stdio.h>
int main() {
int i, sum = 0;
i = 1;
do {
sum += i;
i++;
}
while (i <= 100);
printf ("%d\n", sum);
return 0;
}
[root@chenshuyi c]# gcc -o dowhile1 dowhile1.c
[root@chenshuyi c]# ./dowhile1
5050
- for循环(循环变量赋初值;循环条件;循环变量增值)
#include <stdio.h>
int main() {
int i, sum = 0;
for (i = 1; i <= 100; i++)
{
sum += i;
}
printf("%d\n", sum);
return 0;
}
[root@chenshuyi c]# gcc -o for for.c
[root@chenshuyi c]# ./for
5050
- break语句(提前结束循环,终止整一个)
例子:现班不超过30人,使程序统计出该班的平均成绩,收到负数的时候结束循环。
[root@chenshuyi c]# cat break.c
#include <stdio.h>
int main() {
float score, sum = 0, average;
int i, n;
for (i = 1; i < 31; i ++) {
scanf ("%f", &score);
if (score < 0) break;
sum = sum + score;
}
n = i - 1;
average = sum/n;
printf ("n = %d, average = %7.2f\n", n, average);
return 0;
}
[root@chenshuyi c]# ./break
77
88
99
12
33
44
55
66
-1
n = 8, average = 59.25
#flloat score定义一个浮点型变量score
#%7.2f打印小数点前7位(不足七位补前置0);小数点后两位(不足两位补0)。
- continue语句(提前结束本次循环,只是终止本次循环非整个)
PS:结束本次循环,即跳过循环体中下面尚未执行的语句,接着进行下一次是否1执行循环的判断。
例子:输入一个班全体同学的成绩,把不及格的学生成绩输出,并求及格学生的平均成绩。
#include <stdio.h>
int main() {
float score, sum = 0, average;
int i, n = 0;
for (i = 1; i < 6; i ++) {
#假设不及格的人有5个
printf ("please enter score:");
scanf ("%f", &score);
if (score < 60) {
#如果不及格
printf ("Fail:%7.2f\n", score); #结束本次循环,输出不及格的成绩
continue;
}
sum = sum + score;
n = n + 1; #统计不及格的人数
}
average = sum/n; #及格的平均成绩
printf ("\n n = %d, average = %7.2f\n", n, average); #输出及格学生人数以及平均分
return 0;
}
[root@chenshuyi c]# gcc -o continue continue.c
[root@chenshuyi c]# ./continue
please enter score:44
Fail: 44.00
please enter score:55
Fail: 55.00
please enter score:60
please enter score:70
please enter score:90
n = 3, average = 73.33
版权声明
本文为[Chshyz]所创,转载请带上原文链接,感谢
https://blog.csdn.net/HelloWorld_4396/article/details/120139148
边栏推荐
猜你喜欢
基于Sentinel+Nacos 对Feign Client 动态添加默认熔断规则
[untitled] database - limit the number of returned rows
How SYSTEMd uses / etc / init D script
Robocode教程8——AdvancedRobot
7-21日错题涉及知识点。
Installation and usage skills of idea
Import of data
Addition, deletion, query and modification of data
几行代码教你爬取LOL皮肤图片
Type conversion in C #
随机推荐
Robocode教程5——Enemy类
爬取彩票数据
H. Are You Safe? Convex hull naked problem
[leetcode 19] delete the penultimate node of the linked list
Introduction to virtualization features
深拷贝和浅拷贝的区别
Kalman filter and inertial integrated navigation
Basemap库绘制地图
[transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
爬取手游网站游戏详情和评论(MQ+多线程)
[leetcode 228] summary interval
[leetcode 6] zigzag transformation
渔网道路密度计算
Techniques et principes de détection
Solution to the trial of ycu Blue Bridge Cup programming competition in 2021
Arcpy为矢量数据添加字段与循环赋值
MySQL groups are sorted by a field, and the first value is taken
批量导出Arcgis属性表
1006 finding a mex (hdu6756)
ArcGIS表转EXCEL超出上限转换失败