Deep Tech Point
first stop in your tech adventure

The do keyword in the do while loop – the basics you have to know

December 6, 2021 | Javascript

“Do” is another reserved keyword in JavaScript that cannot be used as a variable, label, or function name. However, this reserved keyword cannot stand alone and is closely connected to another reserved keyword – “while”. Together “do” and “while” keywords create a loop and in that loop, the do keyword executes (does) some part of the code. Let’s go through a few examples and see what is the purpose of the “do” keyword in the do-while loop.

So, what is the role of the “do” keyword in the do-while loop?

As said in the introduction, the do keyword executes a part of the code, so basically, it does something and then checks if whatever was done is true. We use that do-while loop when we want to run the loop at least one time, even if the condition is not true. Why is that? This is because the block of code that is in the loop is executed (we tell Javascript to do something) before the condition is tested – before checking if the condition is true. Since the condition is evaluated only after the statement has been executed, the do-while loop is also called a post-test loop.

So, this is that one time JavaScript goes through the loop, but the loop repeats as long as the condition is true, and if the condition is always true, we have a never-ending loop, which crashes the browser. For this reason, inside the body of the loop, you need to make changes to some variable (for example, you can increment it) to ensure that the expression evaluates to false after iterations. If you forget to do that, you can have a condition that is always true and there you have it – a never-ending, indefinite loop.

The syntax of the do-while loop is super simple:

do {
  the code block you want to execute - the statement(s)
}
while (the condition);

As you can see in the do-while syntax, the condition is something that is required if you want to run the specific code of the block. So, the loop always runs at least one time – the statement is executed at least once – and then if the condition is true, the loop will start again – a statement is will be executed each time the condition is true. If you want to execute multiple statements within the loop, you should use a block statement ({ … }) to group those statements. And, at the end of the day, if the condition is not true, if it’s false, the loop will end and the control goes to the statement that follows the do-while loop.

A simple example of the do-while loop with incrementation

var number = 1;
do {
    number++;
    console.log("the number is:" + number);
} while (number < 5);

In the example above we've set the variable, called number to 1, and then we also incremented the variable by one for every loop iteration. If we run the code, the loop will continue as long as the number is less than 5. Can you predict what would have happened if we hadn't incremented the variable? The condition would be always true, and the loop would start again and again and we would have a never-ending, indefinite loop.

What is the take-away message in the do-while loop?