Break、continue、return用法(C++)
2024-10-12 17:08:12
(1)break
直接调出当前循环体。如果是嵌套循环,他只能调出一层循环体。
Exp-1:
程序:
#include<iostream> using namespace std; int main() { for(int i=;i<;i++) { cout<<"the first circle"<<" "<<i<<endl; for(int j=;j<;j++) { if(j==) break; cout<<"the second circle"<<" "<<j<<endl; } } return ; }
运行结果:
the first circle 0
the second circle 0
the first circle 1
the second circle 0
the first circle 2
the second circle 0
请按任意键继续. . .
(2) continue
忽略循环体中continue后面的的语句,进入下一次循环开始前的条件判断,不跳出该循环。
Exp-2:
程序:
#include<iostream> using namespace std; int main() { for(int i=;i<;i++) { cout<<"the first circle"<<" "<<i<<endl; for(int j=;j<;j++) { if(j==) continue; cout<<"the second circle"<<" "<<j<<endl; } } return ; }
运行结果:
the first circle 0
the second circle 0
the second circle 2
the first circle 1
the second circle 0
the second circle 2
the first circle 2
the second circle 0
the second circle 2
请按任意键继续. . .
(3) return
return表示中止当前函数的运行,并将操作权返回给调用者。
如果是在main函数中,表示将操作权返回给操作系统。
最新文章
- 分享10款非常有用的 Ajax 插件
- android常犯错误记录
- 【Tomcat】tomcat报错 removeGeneratedClassFiles failed
- Win10 安装Vs2015 社区版和企业版各个问题汇总
- 论文笔记之:Co-saliency Detection via A Self-paced Multiple-instance Learning Framework
- c# MessageBox使用
- JAVA swing中JPanel如何实现分组框的效果以及设置边框颜色 分类: Java Game 2014-08-16 12:21 198人阅读 评论(0) 收藏
- cocos2d-x 读取 json 文件并用 jsoncpp 做解析
- SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了
- python 发送163邮件
- 南昌邀请赛I.Max answer 单调栈+线段树
- mac新手使用
- CocoaPods 简介
- [No0000E8]C# 方法 参数传递
- 基于tensorflow的MNIST手写识别
- thymeleaf 处理模板为字符串
- jsp路径问题之base
- 4569: [Scoi2016]萌萌哒
- “通用类型系统”(CTS)
- maven springmvc spring data jpa hibernate sqlserver demo