Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

break statement does not work outside of switch or loop?

I was just messing around in Xcode and I discovered that, when I tried to use the break statement to exit a block of code, I got an error ("break statement not within loop or switch"). I thought the break statement was supposed to be able to be used to exit any block of code at any time, so I opened my Mac OS X Programming book by Wrox (a bit outdated, uses older version of Xcode/Interface Builder, but applicable enough), and the book confirmed that I should be able to use the break statement within any block of code, not just switch/case construction and loops. I assume something has changed in the newer version of Xcode since this book has been published, but I'm curious, so can anyone explain this to me? Thanks!

MacBook, Mac OS X (10.5.7)

Posted on Aug 31, 2009 2:14 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 31, 2009 9:56 AM

It sounds like the book is wrong. 'break' will only get you out of the innermost loop or switch. You can use 'return' to exit out of a function at any time.
5 replies
Sort By: 

Sep 1, 2009 1:44 AM in response to Tron55555

Yeah, that seems to be the case, and that's the only way I've ever used "break," but I did think it could be used to exit any block of code, especially having read it in the book I have. The book is Mac OS X Programming by Wrox, and on page 175 in the "The C Language" chapter, it says:


"break" is not only useful for "switch" and "while" statements; it can actually be used anytime you want to escape a block of code enclosed in braces. Execution is immediately transferred to the closing brace, and continues from there."

Anyways, I guess it is possible that in some older standard of C the "break" statement was allowed in these cases, since I can't imagine the book being out and out wrong, but you're right, it certainly seems to be -- interesting...
Reply

Sep 1, 2009 8:37 AM in response to Tron55555

Sounds like your book is mistaken - I checked the K&R C book, and section 9.6 (Jump Statements) says "A break statement may appear only in an iteration statement or a switch statement, and terminates execution of the smallest enclosing such statement".

And it makes sense too - you can "escape" from a method with "return" , and you can skip code in other situations with an if/else. I don't know what a "break" outside a case would be expected to do.
Reply

Sep 1, 2009 9:51 AM in response to Tron55555

'break' is really only a restricted form of 'goto' anyway. Ideally, you want a single point of exit from any block of code. You should really only use 'break' in a switch statement because that is the only way to make it work. In any other context, there are better ways to accomplish the same thing. The same applies to 'continue'.
Reply

Sep 4, 2009 11:24 PM in response to Tron55555

Thanks for the replies, guys. I've been hearing so many people make reference to K&R in the threads I've started here that I figured I'd buy a copy. Anyways, it gets here today, so I figure I'll be able to save some time and effort and refer to it myself for some of these questions I have. Thanks again for your posts.
Reply

break statement does not work outside of switch or loop?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.