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?
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 ofaandb.breakstatement will only match the closest surrounding loop orswitch. Inside yourswitch, allbreakstatements will be for theswitch.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 firstbreakis inside anifstatement. Both thatifstatement and the followingbreakare inside aswitch. Neitherbreakis inside the other.if(b==1) break; else break;All of which can be replaced withbreak;. The whole code can be replaced withwhile(1){}.