当前位置:网站首页>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
边栏推荐
猜你喜欢
![[leetcode 202] happy number](/img/b2/a4e65688aef3a0cec8088bcaba048f.jpg)
[leetcode 202] happy number
![[leetcode 19] delete the penultimate node of the linked list](/img/ba/3c73fba8c4b4e3de7e506670144890.png)
[leetcode 19] delete the penultimate node of the linked list

基于Sentinel+Nacos 对Feign Client 动态添加默认熔断规则

檢測技術與原理

Basemap库绘制地图

The most practical chrome plug-in
![[leetcode 59] spiral matrix II](/img/6e/58e600272797563129d2b325a054b5.png)
[leetcode 59] spiral matrix II

Installation and usage skills of idea

St table template

Addition, deletion, modification and query of MySQL table
随机推荐
Plane semi intersecting plate
[transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
线程和进程的关系和区别是什么
Easy to use data set and open source network comparison website
Import of data
Storing inherited knowledge in cloud computing
Cf6d lizards and fundamentals 2 problem solving
How SYSTEMd uses / etc / init D script
深拷贝和浅拷贝的区别
Busybox initrd and initialization process
Option的正确打开方式
Collection and map thread safety problem solving
Detailed arrangement of knowledge points of University probability theory and mathematical statistics
自动控制原理知识点整合归纳(韩敏版)
程序设计训练
SQL sorts according to the specified content
检测技术与原理
word排版遇到的格式问题
日志
Definition of C class and method