Breaking out of loops, in C?
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]
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 :(
<Quote>Break - Not used in For dangerous! Only used in Switch.</Quote>
And he penialised me down to a high C grade :(
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.
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.
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.
Git......... well he didnt have to cut my grade in half to show it. A simple postit would have been enough.
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.
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.
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
Popular Topics
Advertisement