Deep into the difference between C language continue and break _c language

Source: Internet
Author: User

The person who thinks the C language is still a beginner seems to overestimate himself. I didn't think there was such an interesting thing in this place. Some words seem to be carefully studied to really get started ah ....

================================================================================

Copy Code code as follows:

#include <stdio.h>
int main (void)
{
int flag=0;
for (int j=0 J <2; J + +) {
if (j==0) {
Switch (j) {
Case 0:

Continue
}
flag=1;
}
}
printf ("flag:%d\n", flag);
}


Output:
flag:0

If the code is:
Case 0:break;
Output:
Flag:1

======================================================================

Look again:

Copy Code code as follows:

#include <stdio.h>

void Main ()
{
int flag = 0;
int j = 0;
For (J=0 J <2; J + +) {
if (j==0) {
if (j==0) {
Continue

}
flag=1;

}

}
printf ("flag:%d\n", flag);
}


Output:
flag:0

If the code is:
Break
Output:
flag:0

==========================================

In the C language book, this is the description of continue and break:

The break statement can exit from the inner loop or switch statement.

The continue statement can only appear in the for, while the Do loop

=========================================

Conclusion:The Continue is only valid for the loop body directly containing it (i.e., the for,while, and not the circulation body in switch{}), and the break is the For,while,switch block that directly contains it.


For example, if there is a break or continue in code that does not contain a switch, the break jumps out of the loop, and continue jumps out of the loop.

For code that inserts a switch statement in a loop, the break jumps out of the inner block only, and the block jumps out of the switch only if it is a switch.

and continue even if it appears in the switch block, because the scope of the continue is only for a while and so on loop statements, so it does not work, still out of this cycle.

If you don't pay attention, you'll also think you're jumping out of a switch statement block.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.