Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between break and continue in a loop

Shulou Source: shulou.com Published: 2022-06-02 10:28:24 09月26日 Update

In this issue, the editor will bring you about the difference between break and continue in the cycle. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

I believe that people who are new to programming are easily confused by the two keywords break,continue, both of which mean to jump out of the loop, but what's the difference between them?

In fact, it is very simple, break is to end the whole cycle, continue is to end the current single cycle, do not understand, look at the following procedures and run the results I think you will understand!

Int x = 0 position while (x = 10)

{

If (x = = 3)

{

Break

}

Printf ("% d\ r\ n", x)

} the running result of the program is: 1 / 2

Analysis: because the program starts to output numbers after entering the while loop, when the if statement is executed and the break statement is run, the break jumps out of the whole loop, so the next step is to execute the code behind the while, and the program ends.

Int x = 0

While (Xerox + < 10)

{

If (x = = 3)

{

Continue

}

Printf ("% d\ r\ n", x)

}

Operation result: 1 2 4 5 6 7 8 9 10

Analysis: this cycle uses continue to jump out of the single loop. When the program runs to XFormul3, execute the continue statement, and the program will skip the subsequent unfinished code in this cycle and directly execute it for 4 times.

The above is the difference between break and continue shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Loop program run analysis result statement code content specialty two small and medium key words rich content that is loop body meaning numbers articles more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS MariaDB Linux OPPO Reno