Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 ?

share|improve this question
there is no such thing as C/C++. – Abyx Mar 2 at 19:18
5  
It doesn't even make sense conceptually. Revise your mental model of control flow. – delnan Mar 2 at 19:22
2  
Are you curious about the control structure? or the issue of AT&T's outage? – MichaelT Mar 2 at 19:45
1  
you could wrap your if statements into a for loop that executes exactly one time. then you could use break to get out of if statements. – SkyDan Mar 2 at 19:47
1  
@MichaelT and SkyDan - "Looks like a turd, smells like a turd, feels like a turd, tastes like a turd, it's a what?". It's a goto in disguise - worse of an evil than writing Goto, if you nee a goto, use goto, don't hide it behind semantics....... – mattnz Mar 3 at 2:38
show 6 more comments

closed as not a real question by World Engineer, gnat, MichaelT, BЈовић, Martijn Pieters Mar 2 at 21:29

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.

3 Answers

up vote 3 down vote accepted

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 if(foo) { doStuff(); if(bar) break; doOtherStuff(); } This control structure can be done in a mulitude of other ways that don't need a break: if(foo) { doStuff(); if(!bar) { doOtherStuff(); } }

break is often used as part of an if block: while(true) { ... if(foo) { break; } ... } If break was to be breaking out of the if context rather than the while context, then labels would be required nearly everywhere: LOOP: while(true) { ... if(foo) { break LOOP; } ... }. Some people like using labels everywhere (and some people deplore the use of a labeled break), but it certainly adds to the clutter in the code.

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 if scope and if such design choice was made, it would have made code much uglier and harder to read (labels are global - every loop would need to be labeled in this global name space).

Related StackOverflow reading:

share|improve this answer

If you want to break out of the if block while still inside it, use a goto statement. Alternatively, rewrite your code so that you don't need to break out of it in the first place, which is usually a better idea.

Also, this question is better asked on Stack Overflow. This site is for conceptual questions, not implementation questions.

share|improve this answer
I dont actually do this kind of code, I just felt curious about ¿how something like this crashed AT&T's system in the 90's? – Chilam Balam Mar 2 at 19:37
I have no idea, to be honest. I wasn't around to program in the 90's, being born in 93 myself. – Joe Z. Mar 2 at 19:37

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.

share|improve this answer
1  
But, isn't switch also a conditional mechanism that also supports the use of break ? – Chilam Balam Mar 2 at 19:34
1  
You usually won't use "break" the way you're using it, though. – Joe Z. Mar 2 at 19:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.