Language: EN

programacion-bucle-dowhile

The DOWHILE loop

The DO-WHILE loop 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 natural language, the WHILE loop means,

Do this 🡆 while this is true

The DO-WHILE loop is the “sibling” 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 executes at least once.

programacion-bucle-do-while

DO-WHILE Diagram

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 executes again
  • If the condition evaluates to false, the loop stops and the program continues with the next statement after the loop.

Equivalence with WHILE loop

In fact, both loops are practically equivalent (in fact, it is possible to easily convert one to the other).

Choosing between both 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 is executed within the loop.

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

That with a WHILE loop looks like this:

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

// rest of the code

As we see, the statement I have above the WHILE is the same as the one I have in the body of the loop. This situation “smells like 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

DO-WHILE loop examples in different languages

As we mentioned, the DO-WHILE and WHILE loops are close siblings. So we don’t expect major differences in 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 this example, the only change is how to define the counter, but the loop syntax 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 to access 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. Therefore, it would need to be converted to the equivalent, as we saw earlier

counter = 0
# do things
while counter < 10:
    # do things
    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 body of the loop, instead of at the beginning.

programacion-bucle-do-while-saltos

Conditional jump generated by the DO-WHILE

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

At the end it encounters a conditional jump. If it is met, the control flow returns to the start of the body of the loop. Otherwise, it continues with the program (“and so be it”).

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

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