Jump to content
  • Advertisement
Sign in to follow this  
Arclight

Breaking out of loops, in C?

This topic is 5085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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 :(

Share this post


Link to post
Share on other sites
Using break isn't nice, but surely it isn't dangerous. Loops without break are easier to analize or proof.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
It's probably along the same lines as people that say you should only have 1 return point at the end of a function.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!