当前位置:网站首页>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
边栏推荐
- 用二进制进行权限管理
- Cf1427c the hard work of paparazzi
- Consistent hash algorithm used for redis cache load balancing
- 7-21日错题涉及知识点。
- LockSupport. Park and unpark, wait and notify
- MySQL occasional Caton
- Completely clean up MySQL win
- [leetcode 150] evaluation of inverse Polish expression
- 几行代码教你爬取LOL皮肤图片
- 自动控制原理知识点整合归纳(韩敏版)
猜你喜欢
Techniques et principes de détection
Understanding and installing MySQL
Storing inherited knowledge in cloud computing
Addition, deletion, query and modification of data
Database - sorting data
Explanation of the second I interval of 2020 Niuke summer multi school training camp
Robocode教程8——AdvancedRobot
Kalman filter and inertial integrated navigation
1007 go running (hdu6808) in the fourth game of 2020 Hangzhou Electric Multi school competition
C language file operation
随机推荐
pyppeteer爬虫
Advanced operation of idea debug
Formation à la programmation
进程间通信的方式
Cf515b drazil and his happy friends
SQL -- data definition
Qthread simple test understanding
serde - rust的序列化方案
Gesture recognition research
GDAL+OGR学习
P1018 maximum product solution
爬虫之requests基本用法
1006 finding a mex (hdu6756)
深拷贝和浅拷贝的区别
[leetcode 54] spiral matrix
Customized communication between threads (reentrantlock)
Usage scenario of copyonwritearraylist
Linux 用rpm的方式安装mysql(超简单)
日志
Robocode教程3——Robo机器剖析