Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Another way to use continue keyword in C++

Tags:

c++

visual-c++

Recently we found a "good way" to comment out lines of code by using continue:

for(int i=0; i<MAX_NUM; i++){
  ....
  .... //--> about 30 lines of code
  continue;
  ....//--> there is about 30 lines of code after continue
  ....
}

I scratch my head by asking why the previous developer put the continue keyword inside the intensive loop. Most probably is he/she feel it's easier to put a "continue" keyword instead of removing all the unwanted code...

It trigger me another question, by looking at below scenario:

Scenario A:

for(int i=0; i<MAX_NUM; i++){
  ....
  if(bFlag)
    continue;
  ....//--> there is about 100 lines of code after continue
  ....
}

Scenario B:

for(int i=0; i<MAX_NUM; i++){
  ....
  if(!bFlag){
  ....//--> there is about 100 lines of code after continue
  ....
  }
}

Which do you think is the best? Why? How about break keyword?

like image 491
wengseng Avatar asked Sep 29 '10 07:09

wengseng


People also ask

What can I use instead of continue in C?

Depending on context, return and goto (and sometimes longjmp() if you've made an appropriate call to setjmp() ). Using goto is the ubiquitous, always available, alternative, but is often regarded as anathema (certainly in comparison with break and continue ).

What is the use of continue keyword in C?

The continue statement passes control to the next iteration of the nearest enclosing do , for , or while statement in which it appears, bypassing any remaining statements in the do , for , or while statement body.

Can we use continue in for loop in C?

The continue statement can be used with any other loop also like while or do while in a similar way as it is used with for loop above.

How do I use continue keyword?

Definition and Usage The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.


Video Answer


2 Answers

Using continue in this case reduces nesting greatly and often makes code more readable.

For example:

for(...) {
    if( condition1 ) {
        Object* pointer = getObject();
        if( pointer != 0 ) {
            ObjectProperty* property = pointer->GetProperty();
            if( property != 0 ) {
        ///blahblahblah...
        }
     }
}

becomes just

for(...) {
    if( !condition1 ) {
        continue;
    }
    Object* pointer = getObject();
    if( pointer == 0 ) {
        continue;
    }
    ObjectProperty* property = pointer->GetProperty();
    if( property == 0 ) {
       continue;
    }

    ///blahblahblah...
}

You see - code becomes linear instead of nested.

You might also find answers to this closely related question helpful.

like image 184
sharptooth Avatar answered Oct 15 '22 14:10

sharptooth


For your first question, it may be a way of skipping the code without commenting it out or deleting it. I wouldn't recommend doing this. If you don't want your code to be executed, don't precede it with a continue/break/return, as this will raise confusion when you/others are reviewing the code and may be seen as a bug.

As for your second question, they are basically identical (depends on assembly output) performance wise, and greatly depends on design. It depends on the way you want the readers of the code to "translate" it into english, as most do when reading back code.

So, the first example may read "Do blah, blah, blah. If (expression), continue on to the next iteration." While the second may read "Do blah, blah, blah. If (expression), do blah, blah, blah"

So, using continue of an if statement may undermine the importance of the code that follows it.

In my opinion, I would prefer the continue if I could, because it would reduce nesting.

like image 36
Alexander Rafferty Avatar answered Oct 15 '22 15:10

Alexander Rafferty