do...while statement is similar to the
while loop, but with one key difference - the code block is executed first, and then the condition is checked. This means that the code block will always execute at least once, even if the condition is initially false.
Let's consider an example where we want to prompt the user for a number and keep prompting them until they enter a positive number. We can use the
do...while statement to achieve this:
In this example, the code block prompts the user for a number and assigns it to the variable
num. The loop will continue executing and prompting the user until they enter a positive number (i.e. the condition
num <= 0 becomes false).
do...while loop that executes the following steps:
i by 1.
i is less than or equal to 10.
Sign up to start coding
Already have an account?