Suppose, I have a if statement inside a for loop:
for( ; ; )
{
if( )
{
printf(" inside if");
break;
}//if
printf("inside for");
}//for
Now, will the break statement cause the compiler to come out of the for loop or will it only come out of the body of if once the condition in the if becomes satisfied?
The
breakstatement breaks out of the nearest enclosing loop or switch statement.breakdoes not break out of anifstatement, but the nearestlooporswitchthat contains thatifstatement. The reason for not breaking out of anifstatement is because it is commonly used to decide whether you want to break out of theloop.Interestingly, a telephone switch misbehaved because the company that invented C made exactly this bug. They wanted to break out of an
ifstatement and they forgot that it would break out of the entireforstatement.