In-depth explanation of the differences between C-language continue and break

Source: Internet
Author: User

I think that the C language is still a beginner, and it seems that I have overestimated myself. I didn't expect such an interesting thing in this place. In some cases, I think it is necessary to go deeper to get started ....

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

Copy codeThe Code is 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

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

Let's look at it again:

Copy codeThe Code is 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, the description of continue and break is as follows:

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

The continue statement can only appear in the for, while, And do loops.

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

Conclusion:Continue is only valid for the loop body that directly contains it (that is, for, while, the loop body in switch {}). break is effective for, while, switch block.

For example, if the break or continue appears in the code that does not contain the switch, the break jumps out of the loop body and the continue jumps out of this loop.

In the code of nested switch statements in a loop, break only jumps out of the innermost block. If this block is a switch, it only jumps out of the switch.

Even if the continue appears in the switch block, because the range of the continue is only for loop statements such as for while, it does not work, it still jumps out of this loop.

If you do not pay attention to it, you will also think that the switch statement block is jumped out.

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.