944,156 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 32109
  • C++ RSS
Apr 28th, 2007
0

C++ break and continue statements

Expand Post »
hey,
i read that in C++ some programmers consider using break and continue statements as a violation of structured programming practise....... and usually avooid using them except in switch statements..., i will like to know how to use the structured equivalent of these statements.....
i want to know the statement that can replace a break and that that can replace continue statement in a program...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
addicted is offline Offline
57 posts
since Mar 2007
Apr 28th, 2007
0

Re: C++ break and continue statements

Your best bet is to draw a flow chart. That will help you avoid the use or rather misuse of continue because all your arrows should point to something else in the diagram.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 28th, 2007
0

Re: C++ break and continue statements

After setting up your Flow chart make your pseusocode just for anlyzing where/when you should put those syntax.
Reputation Points: 28
Solved Threads: 2
Posting Pro
eXceed69 is offline Offline
559 posts
since Feb 2007
Apr 28th, 2007
0

Re: C++ break and continue statements

>i read that in C++ some programmers consider using break and continue
>statements as a violation of structured programming practise....... and
>usually avoid using them except in switch statements...
That's because they're stupid. It's like the whole single entry single exit crap that structured programming aficionados spew. The result is theoretically "pure", but it's almost always more complicated and the flow of control is harder to follow. That flies in the face of the best advice I can give: write the simplest code you can get away with.

>i want to know the statement that can replace a break and that that can
>replace continue statement in a program...
A break is generally replaced by a status flag in the loop condition:
while ( something ) {
  if ( something else )
    break;

  // The rest of the loop
}
becomes:
bool done = false;

while ( !done && something ) {
  if ( something else )
    done = true;
  else {
    // The rest of the loop
  }
}
A continue is easy to replace by reversing the continue condition and executing the loop logic on the body:
while ( something ) {
  if ( something else )
    continue;

  // The rest of the loop
}
becomes:
while ( something ) {
  if ( !something else ) {
    // The rest of the loop
  }
}
It really depends on both your style and what kind of loop you're writing whether one or the other works better. Most of the time I tend to prefer using break over flags, and I prefer using a reversed condition rather than continue.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Design Tools Tutorials
Next Thread in C++ Forum Timeline: Please help about USER INPUT





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC