当前位置:网站首页>7 C控制语句:分支和跳转
7 C控制语句:分支和跳转
2022-07-28 08:26:00 【你回到了你的家】
6 循环辅助:continue和break
-般而言,程序进入循环后,在下一次循环测试之前会执行完循环体中的所有语句。continue 和break语句可以根据循环体中的测试结果来忽略一部分循环内容, 甚至结束循环。
6.1 continue语句
3种循环都可以使用continue语句。执行到该语句时,会跳过本次迭代的剩余部分,并开始下一轮迭代。如果continue语句在嵌套循环内,则只会影响包含该语句的内层循环。程序清单7.9中的简短程序演示了如何使用continue。
程序7.9
/* skippart.c -- uses continue to skip part of loop */
#include <stdio.h>
int main(void){
const float MIN = 0.0f;
const float MAX = 100.0f;
float score;
float total = 0.0f;
int n = 0;
float min = MAX;
float max = MIN;
printf("Enter the first score (q to quit): ");
while (scanf("%f", &score) == 1){
if (score < MIN || score > MAX){
printf("%0.1f is an invalid value. Try again: ", score);
continue; // jumps to while loop test condition
}
printf("Accepting %0.1f:\n", score);
min = (score < min)? score: min;
max = (score > max)? score: max;
total += score;
n++;
printf("Enter next score (q to quit): ");
}
if (n > 0){
printf("Average of %d scores is %0.1f.\n", n, total / n);
printf("Low = %0.1f, high = %0.1f\n", min, max);
}
else
printf("No valid scores were entered.\n");
return 0;
}
在程序清单7.9中,while循环读取输入,直至用户输入非数值数据。循环中的if语句筛选出无效的分数。假设输入188,程序会报告: 188 is an invalid value。 在本例中,continue语句让程序跳过处理有效输入部分的代码。程序开始下一轮循环,准备读取下一个输入值。
continue可用作占位符。例如,下面的循环读取并丢弃输入的数据,直至读到行末尾:
while (getchar() != '\n')
;
当程序已经读取-行中的某些内容, 要跳至下一行开始处时, 这种用法很方便。问题是,- 般很难注意到一个单独的分号。如果使用continue,可读性会更高:
while (getchar() != '\n')
continue;
那么,从何处开始继续循环?对于while和do while循环,执行continue语句后的下一个行为是对循环的测试表达式求值。考虑下面的循环:
count=0;
for (count = 0; count < 10; count++){
ch = getchar();
if (ch == '\n')
continue;
putchar(ch);
count++;
}
该循环读取10个字符(除换行符外,因为当ch是换行符时,程序会跳过count++;语句)并重新显示它们,其中不包括换行符。执行continue后,下一个被求值的表达式是循环测试条件。
对于for循环,执行continue后的下一个行为是对更新表达式求值,然后是对循环测试表达式求值。例如,考虑下面的循环:
for (count = 0; count < 10; count++){
ch = getchar();
if (ch == '\n')
continue;
putchar(ch);
}
该例中,执行完continue后,首先递增count,然后将递增后的值和10作比较。因此,该循环与上面while循环的例子稍有不同。while循环的例子中,除了换行符,其余字符都显示;而本例中,换行符也计算在内,所以读取的10个字符中包含换行符。
6.2 break语句
边栏推荐
- Realize batch data enhancement | use of keras imagedatagenerator
- Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]
- Solution and implementation of APP accelerating reading and displaying IPFs pictures
- 12 common design ideas of design for failure
- JSON 文件存储
- Feign调用异常[Running, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = n]
- VR全景拍摄,助力民宿多元化宣传
- [cloud computing] several mistakes that enterprises need to avoid after going to the cloud
- 49 opencv deep analysis profile
- Div tags and span Tags
猜你喜欢

linux初始化mysql时报错 FATAL ERROR: Could not find my-default.cnf

Image batch processing | necessary skills

Why is the text box of Google material design not used?

Introduction to official account

Mongodb (compare relational database, cloud database, common command line, tutorial)

The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.

c语言数组指针和指针数组辨析,浅析内存泄漏

Go synergy

golang 协程的实现原理

Eight ways to solve EMC and EMI conducted interference
随机推荐
侯捷STL标准库和泛型编程
How to obtain the subordinate / annotation information of KEGG channel
12 common design ideas of design for failure
In addition to exporting the incremental data captured by Oracle golden gate to Oracle, where can it be exported? Can be similar
mysql主从架构 ,主库挂掉重启后,从库怎么自动连接主库
VS2015使用dumpbin 查看库的导出函数符号
一款入门神器TensorFlowPlayground
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
Marketing play is changeable, and understanding the rules is the key!
Implementation principle of golang synergy
KEGG通路的从属/注释信息如何获取
Linux initializes MySQL with fatal error: could not find my-default.cnf
Centralized log management with sentry
SQL injection - pre Foundation
完善的交叉编译环境记录 peta 生成的shell 脚本
Chapter 2-2 calculation of piecewise function [1]
Learn to draw with nature communications -- complex violin drawing
一年涨薪三次背后的秘密
Machine learning: self paced and fine tuning
This flick SQL timestamp_ Can ltz be used in create DDL