A talk C Chestnut bar (180th: where C language instance--break)

Source: Internet
Author: User
Tags switch loop

Hello, crossing, all right, last time we were talking about examples of character and string output functions, and this time we say the example is:where to break. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, our theme for this time is where to break, and before we introduce this topic, let's introduce a break. Break is a keyword in the C language that is followed by a semicolon to form a break statement. Ah! Is that a statement? Crossing was surprised. What I'm saying is this is really a statement, and the break keyword always makes a single statement. Although the break keyword can be a single-handedly constituted statement, the break statement always appears with a loop statement or switch statement. Like the golden Master "The God of the Eagle" in the black and evil, they always appear together, and let people feared. Haha, crossing do not be afraid, we do not have the black and the evil spirit, we only with the Loop statement or switch statement with the break statement. So back to our topic, where is the break statement?

To answer this question, we need to put the break statement and the Loop statement or switch statement together, because they always appear together in the program. A loop statement or a switch statement has its own scope, which is the content in the curly braces range. When you use the break statement in both statements, you jump out of the scope of both statements. We use examples to illustrate, easy to understand.

 int      Index  = 0 ; while     (index  < 5 ) {//do  some thing printf  (" index = %d  \ n ", index );        index  + +; if         (index  = = 3 )            {puts ( "break Loop:while" );        break ; }} puts ( "hello" );  

In the above example, the scope of the while loop statement is the while statement itself and the contents of the curly braces under the statement, after the break statement is used, the scope of the while statement is jumped out, and then the puts statement after the scope is run, and it outputs in the terminal " Hello ". We used in the example is a while loop statement, for or Do/while Loop statement is the same principle, you can analyze and try to do it yourself, the following is the operation of the program results, please refer to:

index0index1index2breakwhile  //index等于3时使用break语句跳出while循环hello              //运行while循环后的语句

Let's go through the example of the break statement in the Loop statement, and then give an example of the break statement in the switch statement, please refer to:

index =3;Switch(index) { Case 1://do some thing        puts("Case 1 runing"); Break; Case 3://do some thing        puts("Case 3 runing"); Break;default://do some thing        puts("Case default runing"); Break; }puts("Hello");

In the above example, the scope of the switch loop statement is the switch statement itself and the contents of the curly braces underneath the statement, and after the break statement is used, it jumps out of the scope of the switch statement and then runs the puts statement after the scope, which in the terminal outputs " Hello ". The following is the results of the operation of the program, please refer to:

case3 runing  //运行case3中的内容,然后使用break跳出switch语句hello          //运行switch语句后的语句

Introduced here, I think you already understand the break statement break where, next we verify that people do not really understand the meaning of it, we show you a piece of code.

index =0; while(Index++ <5)    {printf("index =%d \ n", index);Switch(index) { Case 1://do some thing                puts("Case 1 running, break switch"); Break; Case 2://do some thing                puts("Case 2 running, break switch"); Break;default://do some thing                puts("Default running, break switch"); Break; }if(Index = =3)        {//do some thing            puts("If/else breaking"); Break; }Else{//do some thing            puts("If/else Running"); }puts("Loop:while Running"); }puts("Hello");

Let's read the program first and then write the results of the program yourself. The following is the result of the operation of the program, we have written the results of comparison, is not the same? If the result is different, then you don't really understand where the break statement is.

index =1  Case 1Running Break Switch  // BreakStatement is just BreakToSwitchExtraterritorial scope,if/ElseRunningLoop: whileRunning//The loop is still running, stating BreakStatement does not BreakTo whileCircular action Outside index =2  Case 2Running Break Switch if/ElseRunningLoop: whileRunningindex =3 defaultRunning Break Switch if/else Breaking// BreakStatement BreakTo whileThe function of the loop extraterritorial hello//Run whileStatements after a loop statement

In combination with the above code and the running results of the program, we can see that the previous example is just a single while语句或者switch语句 , and the code here is while循环中嵌套了switch语句 , in contrast, more complex. In the code, switch语句中使用了break语句 the statement is only, however, in scope break到了switch作用域外 switch语句仍然在while循环语句 , so it does not break到while循环语句 outside, which is reflected in the program run result as a constant execution witch语句中的case of s until index等于3时从if/else语句中的break跳出while循环 the scope.

With this example, we can conclude that the breakstatement can break only to its own domain, and if the scope is nested, it does not break out of the nested region. so it might be more abstract, let's make a simple illustration to illustrate:

{    //作用域A    {        //作用域B        break;    //该break语句只能break到作用域B外,不会break到作用域A外;    }    break;       //该break语句可以break到作用域A外;}

Crossing, I put the code in this chapter back into a file, which is located in my resources, you can click here to download the use.

Everybody crossing, here's an example of where the break goes. I want to know what the following example, and listen to tell.

A talk C Chestnut bar (180th: where C language instance--break)

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.