当前位置:网站首页>C language loop structure program
C language loop structure program
2022-04-23 17:55:00 【Chshyz】
while loop 、do while loop 、for loop 、break End 、continue end
Environmental Science :CentOS7;
The loop condition :① The loop body ;② Cycle end condition ;
characteristic : The computer only deals with conditional loops ( effectiveness 、 Certainty and finiteness ).
- while loop ( First judge recycling , Embedded statement )
Example 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;
}
[[email protected] c]# gcc -o while while.c
[[email protected] c]# ./while
5050
- do while( First cycle, then judge )
Example 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;
}
[[email protected] c]# gcc -o dowhile1 dowhile1.c
[[email protected] c]# ./dowhile1
5050
- for loop ( Assign an initial value to a loop variable ; The loop condition ; Loop variable increment )
#include <stdio.h>
int main() {
int i, sum = 0;
for (i = 1; i <= 100; i++)
{
sum += i;
}
printf("%d\n", sum);
return 0;
}
[[email protected] c]# gcc -o for for.c
[[email protected] c]# ./for
5050
- break sentence ( End the cycle ahead of time , Terminate the entire )
Example : The current shift does not exceed 30 people , Make the program count the average score of the class , End the loop when a negative number is received .
[[email protected] 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;
}
[[email protected] c]# ./break
77
88
99
12
33
44
55
66
-1
n = 8, average = 59.25
#flloat score Define a floating point variable score
#%7.2f Print before the decimal point 7 position ( Less than seven places are filled in 0); Two decimal places ( Less than two people make up for it 0).
- continue sentence ( End this cycle ahead of time , Just terminate this cycle, not the whole )
PS: End this cycle , That is, skip the following unexecuted statements in the loop body , Then proceed to the next time whether 1 Execute loop judgment .
Example : Enter the grade of all the students in a class , Output the results of failed students , And ask for the average score of passing students .
#include <stdio.h>
int main() {
float score, sum = 0, average;
int i, n = 0;
for (i = 1; i < 6; i ++) {
# Suppose that those who fail have 5 individual
printf ("please enter score:");
scanf ("%f", &score);
if (score < 60) {
# If you fail
printf ("Fail:%7.2f\n", score); # End this cycle , Output failing grades
continue;
}
sum = sum + score;
n = n + 1; # Count the number of people who failed
}
average = sum/n; # A passing grade average
printf ("\n n = %d, average = %7.2f\n", n, average); # Output the number of passing students and average score
return 0;
}
[[email protected] c]# gcc -o continue continue.c
[[email protected] 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://yzsam.com/2022/04/202204230547414047.html
边栏推荐
- 2022年茶艺师(初级)考试模拟100题及模拟考试
- Comparison between xtask and kotlin coroutine
- 圆环回原点问题-字节跳动高频题
- Go file operation
- 386. Dictionary order (medium) - iteration - full arrangement
- 2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition
- SystemVerilog (VI) - variable
- Tensorflow tensor introduction
- Amount input box, used for recharge and withdrawal
- Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)
猜你喜欢
Land cover / use data product download
关于gcc输出typeid完整名的方法
JS parsing and execution process
Kubernetes service discovery monitoring endpoints
Summary of floating point double precision, single precision and half precision knowledge
[appium] write scripts by designing Keyword Driven files
Examination question bank and online simulation examination of the third batch (main person in charge) of special operation certificate of safety officer a certificate in Guangdong Province in 2022
Hcip fifth experiment
开源按键组件Multi_Button的使用,含测试工程
Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)
随机推荐
An example of linear regression based on tensorflow
Examination question bank and online simulation examination of the third batch (main person in charge) of special operation certificate of safety officer a certificate in Guangdong Province in 2022
Ring back to origin problem - byte jumping high frequency problem
Special effects case collection: mouse planet small tail
Add animation to the picture under V-for timing
2021长城杯WP
2022年广东省安全员A证第三批(主要负责人)特种作业证考试题库及在线模拟考试
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
209. Minimum length subarray - sliding window
ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
Halo 开源项目学习(二):实体类与数据表
Remember using Ali Font Icon Library for the first time
Utilisation de la liste - Ajouter, supprimer et modifier la requête
The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time
MySQL advanced index [classification, performance analysis, use, design principles]
土地覆盖/利用数据产品下载
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
The ultimate experience, the audio and video technology behind the tiktok
Element calculation distance and event object
云原生虚拟化:基于 Kubevirt 构建边缘计算实例