Better code with if

Which one is better:

1. if + continue
1
2
3
4
5
6
7
8
9
10
11
12
while (1) {
  some_thing_and_many_more;
  ...;
  if (condition)
  {
    many_thing;
    continue;
    // repeat loop, of course
  }
  another_thing;
  // repeat loop
}

2. if-else
1
2
3
4
5
6
7
8
9
10
11
12
13
while (1) {
  some_thing_and_many_more;
  ...;
  if (condition)
  {
    many_thing;
  }
  else
  {
    another_thing;
  }
  // repeat loop
}


within program approach. Please tell me the specific detail about what is happening inside the program in both conditions.
Let say there are more than 1 statement for each "thing"
Last edited on
Are you asking if both forms are equivalent? If you are: yes, they are equivalent.
Which one is better:


Neither. Both will achieve the same result with very similar instructions :)
In my opinion (a opinion of a beginner, I have to mark), it depends of the rest of the code

Yes the result is equal, as helios e Zeita said, but the first way is a little more quick than the second.

In the first case, after the condition met, the looping process start from beginning without reading the rest

In the second case, after the condition met, the compiler have to read and interper the code of continue before starting again an other looping process.

The second way, even if quicker, can be the worst in some cases

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (1) {
  some_thing_and_many_more;
  ...;
  if (condition)
  {
    many_thing;
  }
  else
  {
    another_thing;
  }
  common_things both if condition met or not
  // repeat loop
}


The example is not so beautifull, but shows you a case where the use of "continue" is wrong (becose if you use "continue", compiler will not execute "common things" you have to execute also if condition met and inserted at the end of loop)
Last edited on
Zaita: Haha. Oops. I didn't see that.

In the second case, after the condition met, the compiler have to read and interper the code of continue before starting again an other looping process.

Huh? After the end of the true block, execution will jump to after the end of the false block. From there, it will jump back to the top of the loop. (Two inconditional jumps.)
continue will jump back to the top of the loop. (One inconditional jump.)
I hardly think that one more inconditional jump carries the same weight as "interpreting a section of code". Which is incorrect, anyway, as code is not interpreted.
But, of course, it goes without saying that you shouldn't use continue if you expect to execute more in the same cycle, just as you shouldn't use break if you expect to continue the loop.

Just wanted to make that clear.
From a purely subjective viewpoint, IMHO the second option is best from a readability standpoint. One could implement every conditional branch in a program using either of the above two methods, but I would hate to have to maintain a large amount of code that exclusively used the first approach.

Only under very rare circumstances would I use the first approach. For example, if there is a lot of code and/or nesting, it can be hard to visualize the nesting/flow even with an editor that matches braces because you can't see both on the screen at the same time. In that case, I would be inclined to reduce the amount of nesting by using a continue. However, having said all that, I'd first try to functionize the code to shorten it up so nesting isn't a problem.

Definitely the second approach is best for readability. In the first approach, someone glancing over the code may assume that another_thing is always executed during the loop if they didn't spot the continue statement in the if branch. The if/else structure makes it very clear to the reader which statements belong to which case.
Ok. I think you guys are missing the point here. Neither of them is bad, and you should not exclude one or the other from your development.

Personally. I typically use the first if my if-statement is a 1 liner (see below). Otherwise I would use the second.

e.g
1
2
3
4
5
6
7
8
          for (int k = 0; k < iBaseColCount; ++k) {
            dCurrent = pBase->getValue( (*vPtr), k);
            if(isZero(dCurrent))
               continue;

            pDiff->subValue( (*vPtr), k, dCurrent);
            pDiff->addValue( (*vPtr), (k+1), dCurrent);
          }


Nothing wrong with the readability of that.
Last edited on
If you take a look at you binary code you will see that it depends on the situation.
If you have a lot of code within your if-clause the first version may be better, because your program can make a short instead of a long jump.
Jumps - especially long jumps - take more time than anything else.
Therefor you should avoid to make long jumps. A short jumb uses a 8 bit signed operand, a near jump uses a 16 or 32 bit signed operand.
Long jumps on the other hand use a full segment base.
... because your program can make a short instead of a long jump.

Wew! I just noticed that loop do jump on each loop.

Two more thing. First, try to reduce curly brackets.
Second:
1
2
3
4
5
6
7
8
9
10
11
while (1) {
  some_thing_and_many_more;
  ...;
  if (condition)
  {
    many_thing;
    continue;
  }
  one_thing; // or many_thing;
  // repeat loop (I mean it, no more codes :) )
}

is better than:
1
2
3
4
5
6
7
8
9
10
11
while (1) {
  some_thing_and_many_more;
  ...;
  if (condition)
  {
    many_thing;
  }
  else
    one_thing; // or {many_thing;}
  // repeat loop
}

especially when using many_thing;.

Please tell me if I'm wrong
Topic archived. No new replies allowed.