do whiledo while

Let's consider the do-while loop. This loop is similar to the while loop but differs.

whiledo-while
Executes code block after the conditional checking.Executes code block before the conditional checking.

Syntax

The do-while loop initiates with the do keyword, encompasses the code block ({}), and concludes with the while keyword followed by the condition (()) without the code block:

Here's an example:

At the start, the variable a was set to 0, but the condition a >= 1 was assessed after the a++ operation.

The code block within the do is always executed at least once:

Note

Including the end-of-command (;) after the do code block results in a SyntaxError.

The do-while loop is handy when you need to prompt the user for input at least once and continue until the user enters valid data or meets a specific condition.

Everything was clear?

Section 5. Chapter 2