The `do-while` Loop
The do-while is very similar to a while loop, except it is always executed at least once, even if the loop condition is false.
Another difference is that the code block is executed before the loop condition is checked.
The general syntax of a do-while loop is the following:
do {
// code to execute
} while(boolean_expression);
The flowchart describes the execution process of a do-while loop:
For example, following is a program which uses a do-while loop to print the first ten even numbers:
123456let i = 1; do { console.log(i * 2); i += 1; } while (i <= 10);
Even if we change the value of i, such that the condition becomes false, the code block will still execute at least once:
123456let i = 11; do { console.log(i * 2); i += 1; } while (i <= 10);
1. What is the key difference between a while loop and a do-while loop?
2. What will be the output of the following code?
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Чудово!
Completion показник покращився до 1.33
The `do-while` Loop
Свайпніть щоб показати меню
The do-while is very similar to a while loop, except it is always executed at least once, even if the loop condition is false.
Another difference is that the code block is executed before the loop condition is checked.
The general syntax of a do-while loop is the following:
do {
// code to execute
} while(boolean_expression);
The flowchart describes the execution process of a do-while loop:
For example, following is a program which uses a do-while loop to print the first ten even numbers:
123456let i = 1; do { console.log(i * 2); i += 1; } while (i <= 10);
Even if we change the value of i, such that the condition becomes false, the code block will still execute at least once:
123456let i = 11; do { console.log(i * 2); i += 1; } while (i <= 10);
1. What is the key difference between a while loop and a do-while loop?
2. What will be the output of the following code?
Дякуємо за ваш відгук!