A break that only exited an if statement would not be useful. Consider this code:
if (test)
{
A;
break;
B;
}
Since the break exits the if statement, this is equivalent to:
if (test)
{
A;
}
So why write the break; and the code after it at all? To be useful here, the break would need to be conditional; it would have to be something like:
if (test)
{
A;
if (want to exit)
break;
B;
}
But now the break is inside another if, so it would only exit that if, not the outer one. So it cannot do the job of exiting the outer if.
Also, if break exited an if statement, rather than a containing loop or switch, then it would not be useful in loop statements.
break is used in loops (for, while, and do) to exit loops when some condition is reached. For this use, the break statement must be inside an if. For example, a loop for searching for some element in an array would have a form like:
for (int i = 0; i < N; ++i)
if (array[i] matches conditions)
break;
Given that a useful break in a for loop must be inside an if statement, a break that terminated an if statement would not be useful. If the break above only exited the if statement, it would not be useful, and the for loop would continue exiting even though we want it stopped.
If you do have a need to exit an if statement, you can do it with goto:
if (test)
{
A;
if (error occurs)
goto EndOfIf:
B;
}
EndOfIf:
or with an enclosing loop or switch:
switch (0)
{
default:
if (test)
{
A;
if (error occurs)
break;
B;
}
}
breakwere able to terminate anifclause, it would be impossible to use it to terminate a loop or a switch case, such as withif(condition) { break; }. Without being inside a conditional, the loop would not loop, and the case code would not run to completion.