最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - Switch inside while and break statement - Stack Overflow

programmeradmin2浏览0评论

Suppose I have a piece of C code

     while(1)
     {
         switch(a){
         case 1:
           if(b==1)
             break;//first break
           break;//second break   
         }    
     }

Will the second break get me to continue the while(1) loop and the first break get me to break out of the while(1) loop even though the first break is inside second break?

The order seems to be changed here on breaks. What is the C language implementation standard on this?

Suppose I have a piece of C code

     while(1)
     {
         switch(a){
         case 1:
           if(b==1)
             break;//first break
           break;//second break   
         }    
     }

Will the second break get me to continue the while(1) loop and the first break get me to break out of the while(1) loop even though the first break is inside second break?

The order seems to be changed here on breaks. What is the C language implementation standard on this?

Share Improve this question asked Mar 11 at 14:53 TurboTurbo 871 silver badge10 bronze badges 5
  • The first break; you are hitting (which of the 2 does not matter) will break you out of the switch, neither will break you out of the loop. Your code is an infinite loop, independently of the values of a and b. – mch Commented Mar 11 at 14:57
  • 1 The break statement will only match the closest surrounding loop or switch. Inside your switch, all break statements will be for the switch. – Some programmer dude Commented Mar 11 at 14:57
  • Re “The order seems to be changed here on breaks”: What order? – Eric Postpischil Commented Mar 11 at 15:03
  • Re “… even though the first break is inside second break?”: There is nothing inside a break. It is a simple statement by itself with no associated body of statement or any nesting. When execution reaches it, control is transferred. The first break is inside an if statement. Both that if statement and the following break are inside a switch. Neither break is inside the other. – Eric Postpischil Commented Mar 11 at 15:04
  • 1 This code is equivalent to if(b==1) break; else break; All of which can be replaced with break;. The whole code can be replaced with while(1){}. – Lundin Commented Mar 11 at 15:07
Add a comment  | 

3 Answers 3

Reset to default 3

C 2024 6.8.7.4 says:

A break statement terminates execution of the innermost enclosing switch or iteration statement.

Therefore a break whose innermost enclosing switch or iteration statement is a switch statement terminates execution of that switch statement.

The first break statement executes only when the if condition b==1 is true and it exits the switch statement, not the while(1) loop. The second break statement also exits only the switch statement and not the while(1) loop.

So, the conclusion is that the while(1) loop will be continue running indefinitely or in infinite loop until an explicit break statement outside of the switch statement is written.

Both break statements are at the same "level", i.e. inside of the switch statement, so in both cases that's what gets broken out of.

Section 6.8.7.4p2 of the C standard regarding the break statement states:

A break statement terminates execution of the innermost enclosing switch or iteration statement

An iteration statement is defined as either a while, do, or for statement. An if is not an iteration statement, so it doesn't matter that one is inside the if statement and one is outside.

This also means that your while loop is an infinite loop because, the way your code is currently written, there's no method to break out of it.

发布评论

评论列表(0)

  1. 暂无评论