views:

155

answers:

8

EDIT: I forgot to add the loop part of the second code.

Looking at the two code styles

while(some_loop_cont_val)
{
    while(pre_x is not done)
    {
        //do action pre_x
    }
    //do action x
}

and

while(some_loop_cont_val)
{
    if(pre_x is not done)
    {
        //do action pre_x
    }
    else
    {
        //do action x
    }
}

The first loop does pre_x (probably an iterative action), then x, the second one does a part of pre_x, then if its not done, continues doing it until its done, then does x. So both do pre_x and then x.

I'm wondering if there are any differences between the two, both efficiency-wise, and in other ways (ie: if there is some subtle effect that would come up only rarely, &c.), or if there is any reason to use one instead of the other in a specific situation, or if it's just a matter of preference.

+4  A: 

In the first example, the second while loop might actually loop.

The second example commits two different actions conditionally.

Noctis Skytower
A: 

Please, please, don't worry about efficiency of such things!!!

The action code would have to be almost nothing for you to ever notice a difference.

And if it is almost nothing, and profiling shows that the program counter spends more than 10% of it's time in that loop, then worry about its efficiency. Then you can unroll the loop or do other cleverness, if you like.

Mike Dunlavey
A: 
  1. break statement from within an inner loop would not allow you to exit outer one. So you'll miss some useful functionality introduced by break/continue statements. Compare:

    while(some_loop_cont_val) {
        if(some_det) {
            break;
        }
    }
    //"break" takes you here
    

    and

    while(some_loop_cont_val){
        while(some_det) {
            break;
        }
      //"break" takes you here
    }
    
  2. Another thing is that "while" loop needs at least two comparisons: to enter it on the 1st iteration and to not enter it on the second one.

Pavel Shved
A: 

Efficiency and preference are irellevent, the two code fragments are not equivalent in any way. if-else is not a loop construct.

Clifford
A: 

The important thing to remember is that that the machine code level it will still be a jump statement of some description. the only thing you've achieved is a maintennance headache in using a language feature for something other than what it designed.

In some low level programming (drivers etc) the this optimisation may well be required - however I doubt it. If you get to that level then writing the thing in assembler or using macro's to hide the ambiguity may well be a better thing to do.

Preet Sangha
A: 

If you make proper changes to the code to make the logic equal, using one loop will be more effecient due to better branch prediction by the cpu (one loop is easier to predict than two - you are likely to have less false predictions this way)

More on branch prediction: http://en.wikipedia.org/wiki/Branch%5Fpredictor

rmn
A: 

The inner loop of the first style will continue to iterate when the outer loop's condition becomes false, if the inner loop's condition remains true.

In contrast, the if-branch can't repeatedly execute when the outer loop's condition becomes false, regardless of whether the if-condition remains true.

(And to repeat what other people have said: if-else is not a loop. Like while, it is a control structure, but it is not a loop.)

Dave Hinton
A: 

The logic of those two is absolutely different.

If some_loop_cont_val is true and *action pre_x* makes some_loop_cont_val false, only your first example will do action x

Shmoopty