views:

134

answers:

4

I found myself confronted with an interview question where the goal was to write a sorting algorithm that sorts an array of unsorted int values:

int[] unsortedArray = { 9, 6, 3, 1, 5, 8, 4, 2, 7, 0 };

Now I googled and found out that there are so many sorting algorithms out there! Finally I could motivate myself to dig into Bubble Sort because it seemed pretty simple to start with.

I read the sample code and came to a solution looking like this:

    static int[] BubbleSort(ref int[] array)
    {
        long lastItemLocation = array.Length - 1;
        int temp;
        bool swapped;

        do
        {
            swapped = false;
            for (int itemLocationCounter = 0; itemLocationCounter < lastItemLocation; itemLocationCounter++)
            {
                if (array[itemLocationCounter] > array[itemLocationCounter + 1])
                {
                    temp = array[itemLocationCounter];
                    array[itemLocationCounter] = array[itemLocationCounter + 1];
                    array[itemLocationCounter + 1] = temp;

                    swapped = true;
                }
            }

        } while (swapped);

        return array;
    }

I clearly see that this is a situation where the do { //work } while(cond) statement is a great help to be and prevents the use of another helper variable.

But is this the only case that this is more useful or do you know any other application where this condition has been used?

+5  A: 

do...while guarantees that the body of code inside the loop executes at least once. This can be handy under certain conditions; when coding a REPL loop, for example.

Robert Harvey
REPL Loop? Are you referring to Read-Eval-Print-Loop?
Shaharyar
@Shaharyar: If you are coding a REPL loop, not using one. It's just a very simple example.
Robert Harvey
+11  A: 

In general:

  • use do...while when you want the body to be executed at least once.
  • use while... when you may not want the body to be executed at all.

EDIT: I'd say the first option comes up about 10% of the time and the second about 90%. You can always re-factor to use either, in either circumstance. Use the one that's closest to what you want to say.

Peter
This is why I use one or the other.
Will
+1  A: 

Anytime you need to loop through some code until a condition is met is a good example of when to use do...while or while...

A good example of when to use do...while or while... is if you have a game or simulation where the game engine is continuously running the various components until some condition occurs like you win or lose.

Of course this is only one example.

Waleed Al-Balooshi
You provided an example, but then you didn't state which `while` pattern is appropriate for your example.
Robert Harvey
@Robert based on the question "when are do while and while do" helpful made me believe the OP wanted examples of when to use either and not an explanation of when to use one over the other. As for my example relating to games, I have seen both used in that particular situation. Thus, I didn't specify one.
Waleed Al-Balooshi
A: 

The above posts are correct regarding the two conditional looping forms. Some languages have a repeat until form instead of do while. Also there's a minimalist view where only the necessary control structures should exist in a language. The while do is necessary but the do while isn't. And as for bubble sort you'll want to avoid going there as it's the slowest of the commonly known sorting algorithms. Look at selection sort or insertion sort instead. Quicksort and merge sort are fast but are hard to write without using recursion and perform badly if you happen to chose a poor pivot value.

Khorkrak
It may be purist to only use `do` `while`, but most modern languages include the alternate form, because it simplifies the logic in the cases where it is needed.
Robert Harvey