C++ break and continue statements

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 55
Reputation: addicted is an unknown quantity at this point 
Solved Threads: 0
addicted's Avatar
addicted addicted is offline Offline
Junior Poster in Training

C++ break and continue statements

 
0
  #1
Apr 28th, 2007
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...
00110101
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: C++ break and continue statements

 
0
  #2
Apr 28th, 2007
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 552
Reputation: eXceed69 is an unknown quantity at this point 
Solved Threads: 2
eXceed69's Avatar
eXceed69 eXceed69 is offline Offline
Posting Pro

Re: C++ break and continue statements

 
0
  #3
Apr 28th, 2007
After setting up your Flow chart make your pseusocode just for anlyzing where/when you should put those syntax.
I dream to exceed to the highest peak.
http://www.ronaldalversado.com
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,813
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: C++ break and continue statements

 
0
  #4
Apr 28th, 2007
>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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC