programacion-bucle-dowhile

The DOWHILE loop

  • 4 min

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 DO-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 loop body. Therefore, the loop executes at least once.

programacion-bucle-do-while

DO-WHILE Diagram

In general, the syntax of a DO-WHILE loop is as follows,

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

// rest of the code
Copied!

The code block is executed first and then the condition is checked

  • If the condition evaluates to true, the loop repeats and the code block executes 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

Actually both loops are practically equivalent (in fact, it’s easy to convert one into the other).

Choosing between them 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 executed inside the loop.

For example, imagine you have a function calculateThings(), and it returns a result. If the result meets a condition, for example it’s less than 10, you need to calculate it again.

That with a WHILE loop looks like this:

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

// rest of the code
Copied!

As we see, the instruction above the WHILE is the same as the one inside the loop body. 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 cleaner:

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

// rest of the code
Copied!

Examples of DO-WHILE Loop in Different Languages

As we said, the DO-WHILE and WHILE loops are close siblings. So we don’t expect major syntax differences 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);
Copied!

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);
Copied!

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);
Copied!

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
Copied!

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😉.