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 |3 Answers
Reset to default 3C 2024 6.8.7.4 says:
A
break
statement terminates execution of the innermost enclosingswitch
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 enclosingswitch
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.
break;
you are hitting (which of the 2 does not matter) will break you out of theswitch
, neither will break you out of the loop. Your code is an infinite loop, independently of the values ofa
andb
. – mch Commented Mar 11 at 14:57break
statement will only match the closest surrounding loop orswitch
. Inside yourswitch
, allbreak
statements will be for theswitch
. – Some programmer dude Commented Mar 11 at 14:57break
. It is a simple statement by itself with no associated body of statement or any nesting. When execution reaches it, control is transferred. The firstbreak
is inside anif
statement. Both thatif
statement and the followingbreak
are inside aswitch
. Neitherbreak
is inside the other. – Eric Postpischil Commented Mar 11 at 15:04if(b==1) break; else break;
All of which can be replaced withbreak;
. The whole code can be replaced withwhile(1){}
. – Lundin Commented Mar 11 at 15:07