当前位置:网站首页>5. Difference between break and continue (easy to understand version)
5. Difference between break and continue (easy to understand version)
2022-07-28 20:05:00 【A programmer who loves playing badminton】
break and continue All of them are used in circular statements , Whether it's for loop , still while loop , Or is it do…while The loop can jump out of the loop .
The difference is
1.continue sentence Just end this cycle , Instead of ending the cycle . That is, skip the following unexecuted statements in the loop body , Then, it is determined whether to execute the cycle next time .
2.break The sentence is End the cycle , It is no longer necessary to judge whether the conditions for executing the loop are true . however break You can only jump out of a loop . When there are multiple nested loops ,break Can only jump out of “ The parcel ” Its innermost layer of circulation , Can't jump out of all loops at once .
Let's first understand through a simple program .
1. We use break To jump out of the loop .
int main(int argc, char const *argv[])
{
for (int i = 1; i <= 5; i++)
{
if (i == 4)
{
break;
// continue;
}
printf("i=%d\n", i);
}
return 0;
}
Through the output, we know , stay i=4 When ,break Directly jumped out of this for loop , No longer judge whether the loop can be executed .
2. We use continue To jump out of the loop
int main(int argc, char const *argv[])
{
for (int i = 1; i <= 5; i++)
{
if (i == 4)
{
//break;
continue;
}
printf("i=%d\n", i);
}
return 0;
}
We can see that , stay i=4 When , Just jump out of this cycle , But will then enter this for loop , Instead of completely jumping out for loop .
But we must remember , In a nested loop ,break Just jumped out of the cycle that wrapped it , And the loop nested outside it cannot jump out . We are looking at it through an example .
int main(void)
{
int val; // Defining variables val, Represents the floor
while (1)
{
printf(" Please enter the floor you want to go to :");
scanf("%d", &val);
switch (val)
{
case 1:
printf("1 Stratification !\n");
break; // Jump out of switch
case 2:
printf("2 Stratification !\n");
break; // Jump out of switch
case 3:
printf("3 Stratification !\n");
break; // Jump out of switch
default:
printf(" This layer does not exist , Please re-enter :");
break;
}
}
return 0;
}Here we can see Wrap it directly break The cycle is switch, instead of while, If you jump out of the loop, you just jump out switch, Can't jump out while, in other words while The statement will also execute .

Through the output, we can see ,break Can only jump out of switch The cycle of , And can't jump out of while This cycle , because while Not directly wrapped break The cycle of
边栏推荐
- Implementation of markdown editor in editor.md
- In the second half of 2022, the system integration project management engineer certification starts on August 20
- Redis笔记
- The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
- Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers
- Machine learning -- model evaluation, selection and verification
- There is a 'single quotation mark' problem in the string when Oracle inserts data
- Function fitting based on MATLAB
- Use of strtok and strError
- 软考中级(系统集成项目管理工程师)高频考点
猜你喜欢

Intermediate soft test (system integration project management engineer) high frequency test site

What is the process of swing event processing?

Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)

Edge detection and connection of image segmentation realized by MATLAB

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

Leetcode Day1 score ranking

Cell review: single cell methods in human microbiome research

数字滤波器设计——Matlab

数字图像理论知识(一)(个人浅析)

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
随机推荐
[C language] Fibonacci sequence [recursion and iteration]
adb remount of the / superblock failed: Permission denied
Return and job management of saltstack
C+ + core programming
并发程序设计,你真的懂吗?
ssm中项目异常处理
How navicate modifies the database name
There is a 'single quotation mark' problem in the string when Oracle inserts data
Leetcode day3 employees who exceed the manager's income
爬取IP
Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
Cdga | how can the industrial Internet industry do a good job in data governance?
MIR专题征稿 | 常识知识与推理:表示、获取与应用 (10月31日截稿)
[C language] simulation implementation of pow function (recursion)
[network] communication across regional networks learn how routing tables work
[network] cross area network communication learning classification and calculation of IPv4 address
Basic knowledge of C language
通信网络基础知识01
How to write the SQL statement of time to date?
【经验之谈】关于维修电子设备的几点建议和经验