Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Using the do while Loop for Iteration | Looping Through Data in JavaScript
Introduction to JavaScript

bookUsing the do while Loop for Iteration

Let's consider the do-while loop. This loop is similar to the while loop but has a key distinction.

whiledo-while
Executes code block after the condition check.Executes code block before the condition check.

Syntax

The do-while loop begins with the do keyword, encloses the code block within curly braces {}, and concludes with the while keyword followed by the condition in parentheses () without the need for an additional code block:

do {
  // code block
} while (condition);

Here's an example:

123456
let a = 0; do { console.log(a); a++; } while (a <= 3 && a >= 1);
copy

At the outset, the variable a was initialized to 0, and the condition a >= 1 was evaluated after the a++ operation.

The code block within the do is guaranteed to be executed at least once:

123456789
// `while` loop while (false) { console.log("while"); // Not executed } // `do-while` loop do { console.log("do-while"); // Executed 1 time } while (false);
copy

The do-while loop is particularly useful 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.

question mark

How many times will the following code print "Invalid PIN" to the console?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 2.33

bookUsing the do while Loop for Iteration

Deslize para mostrar o menu

Let's consider the do-while loop. This loop is similar to the while loop but has a key distinction.

whiledo-while
Executes code block after the condition check.Executes code block before the condition check.

Syntax

The do-while loop begins with the do keyword, encloses the code block within curly braces {}, and concludes with the while keyword followed by the condition in parentheses () without the need for an additional code block:

do {
  // code block
} while (condition);

Here's an example:

123456
let a = 0; do { console.log(a); a++; } while (a <= 3 && a >= 1);
copy

At the outset, the variable a was initialized to 0, and the condition a >= 1 was evaluated after the a++ operation.

The code block within the do is guaranteed to be executed at least once:

123456789
// `while` loop while (false) { console.log("while"); // Not executed } // `do-while` loop do { console.log("do-while"); // Executed 1 time } while (false);
copy

The do-while loop is particularly useful 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.

question mark

How many times will the following code print "Invalid PIN" to the console?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 2
some-alt