Language: EN

programacion-bucle-dowhile

The DOWHILE loop

The loop DO-WHILE is a control structure that repeats a block of code at least once, and then checks a condition to decide whether to continue or exit the loop.

In plain language, the WHILE loop means,

Do this -> while this is true

The DO-WHILE loop is the “brother” of the WHILE loop, and its operation is very similar.

The difference is that in the DO-WHILE loop the condition is checked after executing the body of the loop. Therefore, the loop is executed at least once.

programacion-bucle-do-while

In general, the syntax of a DO-WHILE loop has the following form,

do
{
    // Block of code to execute while the condition is true
}
while(condition)

// rest of the code

The block of code is executed first and then the condition is checked. If the condition evaluates to true, the loop repeats and the block of code is executed again. If the condition evaluates to false, the loop stops and the program continues with the next instruction after the loop.

Equivalence with WHILE loop

In reality, both loops are practically equivalent. In fact, it is possible to easily convert one into the other.

Choosing between the two depends on how your program works. It makes sense to use a DO-WHILE when the condition depends on a process, which is the same process that runs inside the loop.

For example, imagine you have a function that calculatesThings(), and returns a result. If the result meets a condition, for example it is less than 10, you must recalculate it.

That with a WHILE loop looks like this:

result = calculateThings()
while(result < 10)
{
    result = calculateThings()
}

// rest of the code

As we can see, the instruction I have above the WHILE is the same as the one I have in the body of the loop. This situation “smells like a DO-WHILE

Indeed, if you change the loop to a DO-WHILE, it looks like this, which is more concise and clean:

do
{
    result = calculateThings()
}
while(result < 10)

// rest of the code

Examples of DO-WHILE loop in different languages

As we were saying, the DO-WHILE and WHILE loops are close siblings. So we do not expect major differences in the syntax between them.

For example, in C, C++, C#, and Java a DO-WHILE loop looks like this

int counter = 0;
do
{
	// do something
    counter++;
} while (counter < 10);

In JavaScript the syntax of the DO-WHILE loop is identical. In the example, only the way of defining the counter changes, but the syntax of the loop is the same

let counter = 0;
do
{
    console.log("Iteration", counter);
    counter++;
} while (counter < 3);

Similarly, in PHP the DO-WHILE loop is identical, only the way of accessing the variables changes

$counter = 0;
do {
    // do something
    $counter++;
} while ($counter < 10);

Finally, Python does not have a DO-WHILE loop, it only has a WHILE loop. So it would have to be converted to the equivalent, as we have seen above

counter = 0
# do something
while counter < 10:
    # do something
    counter += 1

Internal operation Advanced

The internal operation of a DO-WHILE loop is very similar to that of a WHILE loop. The difference is that the conditional jump is located at the end of the loop body, rather than at the beginning.

programacion-bucle-do-while-saltos

In this case, the program flow enters directly into the body of the loop, so it will always run once.

At the end, it encounters a conditional jump. If it is fulfilled, the control flow returns to the beginning of the loop body. Otherwise, it continues with the program “as friends.”

In reality, for those of you with a finer eye, you will see that in this case the jump is made when the condition is met, whereas in the WHILE loop it was when the condition was not met.

But don’t worry about it either, it’s just an example of the many ways to branch code. In addition, the CPU has “conditional jumps to spare,” including jumps when a condition is met and when a condition is not met. So don’t give it too much importance. 😉.