6

Is

if(a)
{
    do
    {
        b();
    }
    while(a);
}

exactly like

while(a)
{
    b();
}

?

gdoron
  • 147,333
  • 58
  • 291
  • 367
user1365010
  • 3,185
  • 8
  • 24
  • 43
  • 3
    possible duplicate of [Test loops at the top or bottom? (while vs. do while)](http://stackoverflow.com/questions/224059/test-loops-at-the-top-or-bottom-while-vs-do-while) – Niko Jun 19 '12 at 13:52
  • Test it out :-P http://jsfiddle.net/LvWtY/ – gen_Eric Jun 19 '12 at 13:53
  • NO... they are not exactly the same... do{ .. } while(a) ... will make sure whatever it is is done at least once and then until 'a' is not true.... while(a) { } ... will only do when 'a' is true. – Brian Jun 19 '12 at 13:53
  • I just found an exemple that shows that they aren't the same. Look : http://jsfiddle.net/PTbZS/41/ is working and http://jsfiddle.net/PTbZS/42/ isn't. (I know it's not very clear but look in the function `modify`) – user1365010 Jun 19 '12 at 13:53
  • Also equivalent: `for(;a;b());` – Deestan Jun 19 '12 at 13:54
  • 7
    @Brian: You missed the `if(a)` in the first snippet. – Felix Kling Jun 19 '12 at 13:54
  • 3
    @user1365010: If you know that it is not very clear then make it simpler. The examples contain a lot of unnecessary code for the problem at hand. It is impossible to see any difference unless you carefully go through the code. And to be honest, given these monsters (the examples), I would say it is more likely you did something "wrong" than that these two constructs are not equivalent (because they are). – Felix Kling Jun 19 '12 at 13:55
  • @I wouldn't call this question not real, but not construtive, as it's a yes-no question. – gdoron Jun 19 '12 at 13:56
  • Closing criteria are wrong. It is a real, concrete question that can be reasonably answered in its current form. – Deestan Jun 19 '12 at 13:57
  • Indeed, it's a real question and it's not even an exact duplicate of the other one. – deceze Jun 19 '12 at 13:58
  • Really, apart from the very basic logic, it wouldn't take 2 mins to code a [sample test](http://jsfiddle.net/ult_combo/en2z8/). – Fabrício Matté Jun 19 '12 at 13:58
  • @deceze. It's problem is it's too localized as it's about a specific code, and not construtive as the answer is **Yes**. – gdoron Jun 19 '12 at 13:59
  • @gdoron and what about the fiddles I gave? – user1365010 Jun 19 '12 at 14:00
  • @user1365010. It's too long as Felix told you, and I can't believe you wrote all that in less than 2 minutes... – gdoron Jun 19 '12 at 14:01
  • @gdo It's about a logical construct that is equally applicable to an uncountable number of languages, how is that "too localized"? Also, a question to which the answer is clearly **Yes** is a great question for SO. It may be a simple question, but if this kind of question is not allowed here, then what is? – deceze Jun 19 '12 at 14:02

5 Answers5

1

Yes, they're the same.
You've created a very unreadable way to mock the while loop with do-while loop + if.

You should read this:
Test loops at the top or bottom? (while vs. do while)

Use while loops when you want to test a condition before the first iteration of the loop.

Use do-while loops when you want to test a condition after running the first iteration of the loop.

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
1

Yes, they're equivalent.
If a is false, b will not execute at all.
If a is true, b will execute until a becomes false.
This is equally true for both constructs.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

They are the same and I will provide an example where you might actually want to use the "Do-While" instead of a while loop.

do{
    x = superMathClass.performComplicatedCalculation(3.14);
}
while (x < 1000);

as opposed to

x = superMathClass.performComplicatedCalculation(3.14);
while ( x < 1000);
{
      x = superMathClass.performComplicatedCalculation(3.14);
}

The argument for using a Do-While is shown above. Assume the line x = superMathClass.performComplicatedCalculation(3.14); wasnt just one line of code and instead was 25 lines of code. If you need to change it, in the do while you would only change it once. In the while loop you would have to change it twice.

I do agree do whiles are off and you should avoid them, but there are arguments in their favor also.

Frank Visaggio
  • 3,642
  • 9
  • 34
  • 71
1

The constructs are pretty much interchangeable in this case, but I think the if do while loop is useful in some cases, for example if some variables need to be kept through all iterations of the loop, but are not needed if the loop is never executed. (And it is assumed that it might not be.) For example

        if (condition) {
            LargeComplexObject object = new LargeComplexObject();
            do {
                some action
                ...
                object.someMethod();
                ...
                more actions
            }
            while (condition);
        }

This can be a useful optimization if the initialization of that object is time or memory intensive, and it is expected that this loop will only trigger occasionally and will be skipped in most executions of the relevant method. Of course there are other cases where it could be useful as well, basically I think the if-do-while should be used if there are things that should be executed before your loop starts, only when your loop is entered.

Orange
  • 23
  • 3
0

Yes, but you should prefer the latter one.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126