954,202 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ break and continue statements

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...:)

addicted
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

After setting up your Flow chart make your pseusocode just for anlyzing where/when you should put those syntax.

eXceed69
Posting Pro
561 posts since Feb 2007
Reputation Points: 28
Solved Threads: 2
 

>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 ( <em>something </em>) {
  if ( <em>something else</em> )
    break;

  // The rest of the loop
}

becomes:

bool done = false;

while ( !done && <em>something </em>) {
  if ( <em>something else</em> )
    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 ( <em>something </em>) {
  if ( <em>something else</em> )
    continue;

  // The rest of the loop
}

becomes:

while ( <em>something </em>) {
  if ( !<em>something else</em> ) {
    // 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You