Breaking out of loops, in C?

Started by
42 comments, last by pex22 19 years, 4 months ago
Ive been told by a lecturer at univeristy that you shouldn't use break statements in 'For' loops to exit them. he says this is "dangerous" ................................. why is it? I havent been able to get hold of him to ask so I thought this would be just as good to post it here. [Edited by - Arclight on December 15, 2004 9:27:01 AM]
-----------------------------Like A Midget At A Urinal, Im Always On My Toes.Beer helping ugly people get laid since 4000BC.
Advertisement
He's wrong.

Are you sure he said break and not goto ?
No it was 'Break' exact comment he wrote on my coursework is -

<Quote>Break - Not used in For dangerous! Only used in Switch.</Quote>

And he penialised me down to a high C grade :(
-----------------------------Like A Midget At A Urinal, Im Always On My Toes.Beer helping ugly people get laid since 4000BC.
Ask HIM why, and ask HIM where he got that from. It just doesn't make any sense to me.
Using break isn't nice, but surely it isn't dangerous. Loops without break are easier to analize or proof.
It is debatable, so take it with a grain of salt. The loop control keywords are not universally loved because it is yet another way for the flow of execution to leave that loop. Without them, only the loop condition keyword controls the loop. When you use things like break, you have multiple ways out. So in this sense it isn't something you should use regularly. Ditto with continue. Everything has its place, including goto. Your teacher may overexaggerate (whether intentionally or unintentionally) some things right now in order to prevent you from relying on these constructs in every loop.

However, if he is grading your work, then play by his rules. Ask him about it in private and explain that you think dropping your grade that much is unfair. It is a bit harsh, if I were a teacher I would have deducted 5 points or so.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
So its possible he is just making a point that I shouldnt exit the loop through anything other than the condition, just to teach me a resonable programming sytle?




Git......... well he didnt have to cut my grade in half to show it. A simple postit would have been enough.
-----------------------------Like A Midget At A Urinal, Im Always On My Toes.Beer helping ugly people get laid since 4000BC.
Sounds a bit harsh to me, for something that's valid working code and mainly his opinion that it's dangerous.

FWIW, I use break in a for loop when I'm doing something like looking for the first item that meets some condition. The for loop is set up to search through the entire array, the break is for when I found what I'm looking for. An accompanying comment is enough so that anyone else realizes there's two ways out.

I find it much cleaner than having a bunch of && statements in the loop condition.
that teachter is stupid. i use breaks and continues all the time.
It's probably along the same lines as people that say you should only have 1 return point at the end of a function.

This topic is closed to new replies.

Advertisement