当前位置:网站首页>Detailed comparison of break and continue functions
Detailed comparison of break and continue functions
2022-07-29 04:32:00 【IT.Husky】
Whether it's while
Cycle or for
loop , There are two special statements that can be used , Namely break
Statement and continue
sentence
Next, I'm going to for The program example of loop introduces the function of two termination statements as a comparison
break
During the cycle , have access to break
Statement jumps out of the current loop . The verification procedure is as follows :
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
if (j >= i)
{
cout << "*";
break;
}
}
cout << endl;
}
system("pause");
return 0;
}
notes : The above two results are commented out break And uncommented break Output result of
Compare the output results : When i = 1 also j = 1 when , Meet the conditions j >= i, Output “*”
encounter break Statement to jump out of the current for (int j = 1; j <= 5; j++) loop , Enter into for (int i = 1; i <= 5; i++) Loop execution i = 2 The cycle of
continue
break
Will jump out of the current cycle , The current entire loop is no longer executed . and continue
Is to end this cycle ahead of time , Continue directly to the next loop . The procedure is as follows :
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
if (j >= i)
{
cout << "*";
continue;
cout << " unexecuted continue" << " ";
}
}
cout << endl;
}
system("pause");
return 0;
}
notes : The above two results are commented out continue And uncommented continue Output result of
Compare the output results : When i = 1 also j = 1 when , Meet the conditions j >= i, Output “*”
encounter continue Statement will not be executed this time j = 1 The cycle of continue Subsequent statements , It doesn't output “ unexecuted continue” sentence , Go straight back to for (int j = 1; j <= 5; j++) Loop execution j = 2 The cycle of
Summary
break
Statement can jump out of the current loopbreak
Statements are usually used in conjunction withif
, End the whole cycle in advance when the conditions are metbreak
Statements always jump out of the nearest loopcontinue
Statement can end this cycle ahead of timecontinue
Statements are usually used in conjunction withif
, End this cycle in advance when the conditions are met
边栏推荐
- Kotlin's list, map, set and other collection classes do not specify types
- Target detection learning process
- 9. Delay queue
- Realize the effect of univariate quadratic equation through JS. Enter the coefficients of a, B and C to calculate the values of X1 and x2
- C language: enumerating knowledge points summary
- Pytorch fixed random seed & recurrence model
- Simply change the picture color
- No, just stick to it for 64 days. Find the insertion location
- Machine vision Series 2: vs DLL debugging
- Pyscript cannot import package
猜你喜欢
Pycharm reports an error when connecting to the virtual machine database
Christmas tree web page and Christmas tree application
Not 67 days, square root
不会就坚持65天吧 只出现一次的数字
Machine vision Series 2: vs DLL debugging
Sequence list and linked list
MySQL - 聚簇索引和辅助索引
Pytoch automatic mixing accuracy (AMP) training
Two forms of softmax cross entropy + numpy implementation
TypeError: Cannot read properties of undefined (reading ‘then‘)
随机推荐
[material delivery UAV] record (ROS + Px4 + yolov5 + esp8266 + steering gear)
Mpc5744p introduction and opensda firmware update
Post export data, return
mpc5744p简介与OpenSDA固件更新
STL source code analysis (Hou Jie) notes - STL overview
Implementation of jump connection of RESNET (pytorch)
Pyscript cannot import package
[C language] power table of 3 generated by PTA 7-53
Shell string segmentation
Pycharm reports an error when connecting to the virtual machine database
C language: structure simple syntax summary
Mongo shell interactive command window
使用容器部署Jenkins
MySQL - clustered index and secondary index
Idea small settings
What is the difference between field, variable and property
Basic operation of queue
Simply change the picture color
Common components of solder pad (2021.4.6)
9.延迟队列