1

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?

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. 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. Commented Mar 11 at 14:57
  • Re “The order seems to be changed here on breaks”: What order? 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. 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){}. Commented Mar 11 at 15:07

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

It is possible to break out of the while loop also from inside the switch statement, by using a goto statement.
Thank you very much for the information, got to learn something new.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.