in C or C++:
why can't we use break; to break an if statement?
if( color == red ) {
...
if( car == hyundai ) break;
...
}
But, why is it exactly, that it does no work ?
|
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.
|
The simplest reason is "its not how the language was designed." You just can't. The reason why you can't is speculation (I haven't been able to find any writings about it being a good idea), though thinking about it, there are likely good reasons for this that can be identified. There is no good reason for a structure that is
At this point (having to label loops) it doesn't look too bad, it would mean that any breakable structure would need to be labeled if there is another breakable structure nested within it. This rapidly becomes a potential nightmare to code to. This boils down to "there is no good use case for a break to break out of an Related StackOverflow reading: |
||||
|
|
|
If you want to break out of the Also, this question is better asked on Stack Overflow. This site is for conceptual questions, not implementation questions. |
|||||
|
|
An if statement is a conditional branching mechanism to alter conditional flow of logic in a thread or process. There is no breaking out of it because it is not a loop. |
|||||||||
|