C Language-break and continue

Source: Internet
Author: User

The program that looks at the following switch statement first:

scanf ("%d", &score);

if (score>=0 && score<=100) {

Switch (SCORE/10) {

Case 10:

Case 9:

Grade = ' A ';

Break

Case 8:

Grade = ' B ';

Break

Case 7:

Grade = ' C ';

Break

Case 6:

Grade = ' D ';

Break

Default

Grade = ' N ';

Break

}

printf ("The result is converted to a hierarchy of%c!!! \ n ", grade);

}else

printf ("Enter an illegal!!!! \ n ");

It is visible in the program that there is a break statement behind each case branch, because the switch statement is mutually exclusive, and each of the cases is independent, so regardless of which branch the last program goes to and executes, it jumps out from here to switch. And break plays a role in jumping out of programs.

In fact, break can be used not only in a switch statement, but also in a for loop and a while loop to jump out of a loop.

There is a more similar to break the control, is continue, it is used to control the program out of this cycle, directly into the next loop, the following two programs to interpret:

int 0 ;      for (; i<; i++) {               if (i%2= =0) {            continue ;        } Else {            printf ("%d\t", I);        }            }

Run the above program and print the results as follows:

1    3    5    7    9    0

and run a program, what will be the output?

int 0 ;      for (; i<; i++)               {if (i%2= =0) {            break ;        } Else {            printf ("%d\t", I);        }            }

The output of the program is:

0

That is, when using break, the program does not output anything, this is because the first time the loop is i=0, when the remainder of 0 is 0, in line with the conditions of break, the program directly from here out of the loop, not the next cycle, and the above using continue, The program stops executing this cycle at the continue, directly carries on the first i+1 cycle, that is to say continue skips this cycle, carries on the next cycle, and the break jumps directly out of the loop body.

The following excerpt is from CSDN for deeper understanding:

The differences between reak and continue are as follows:
1. Break can be used for a switch statement to jump out of the entire switch block, while continue cannot be used for a switch statement
2. They can all be used in cyclic bodies of circular statements, and the so-called difference should be that they have different effects on the number of cycles. Break is used to exit the current loop immediately, while continue skips only the secondary loop (other statements after the continue statement are not executed in this loop, but the next loop is executed). An example is described.
int i;
int s = 0;
for (int i = 1; i <=; i++)
{
if (i = = 6) break;
s + = i;
}
The above loop terminates prematurely at I=6 because of the break statement, so that the final value of S is 1+2+3+4+5
If you change the break to continue
int i;
int s = 0;
for (int i = 1; i <=; i++)
{
if (i = = 6) continue;
s + = i;
}
When i=6 does not accumulate I into S, the final value of S is 1+2+3+4+5+7+8+9+10, but one less 6

C Language-break and continue

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.