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

Member Avatar for iamthwee

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.

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

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

  // The rest of the loop
}

becomes:

bool done = false;

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

  // The rest of the loop
}

becomes:

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.