Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda while and do...while Loops | Building Conditions and Repetition
Conditional Statements and Loops in JavaScript

bookwhile and do...while Loops

Imagine you are playing a game where you roll a standard six-sided die over and over, hoping to get a six. You keep rolling until you finally see that six appear. You do not know how many times you will have to roll—it might be the first try, or it might take several attempts. This is a lot like how a while loop works in JavaScript: it keeps repeating a block of code as long as a certain condition is true. When the condition changes (you roll a six), the loop stops.

1234567
// Simulate rolling a die until a six appears using a while loop let roll = 0; while (roll !== 6) { roll = Math.floor(Math.random() * 6) + 1; console.log("You rolled a " + roll); } console.log("You finally rolled a six!");
copy
1234567
// Simulate rolling a die until a six appears using a do...while loop let roll = 0; do { roll = Math.floor(Math.random() * 6) + 1; console.log("You rolled a " + roll); } while (roll !== 6); console.log("You finally rolled a six!");
copy

1. Which type of loop will always run its code at least once, even if the condition is false to begin with?

2. Fill in the blank to complete the while loop so it stops when count reaches 5.

question mark

Which type of loop will always run its code at least once, even if the condition is false to begin with?

Select the correct answer

question-icon

Fill in the blank to complete the while loop so it stops when count reaches 5.

let count = 0; while () { console.log(count); count++; }
0
1
2
3
4

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

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

Suggested prompts:

What is the difference between a while loop and a do...while loop in JavaScript?

Can you explain how the random die roll works in the code?

Why does the output sometimes show different numbers of rolls before getting a six?

Awesome!

Completion rate improved to 7.69

bookwhile and do...while Loops

Deslize para mostrar o menu

Imagine you are playing a game where you roll a standard six-sided die over and over, hoping to get a six. You keep rolling until you finally see that six appear. You do not know how many times you will have to roll—it might be the first try, or it might take several attempts. This is a lot like how a while loop works in JavaScript: it keeps repeating a block of code as long as a certain condition is true. When the condition changes (you roll a six), the loop stops.

1234567
// Simulate rolling a die until a six appears using a while loop let roll = 0; while (roll !== 6) { roll = Math.floor(Math.random() * 6) + 1; console.log("You rolled a " + roll); } console.log("You finally rolled a six!");
copy
1234567
// Simulate rolling a die until a six appears using a do...while loop let roll = 0; do { roll = Math.floor(Math.random() * 6) + 1; console.log("You rolled a " + roll); } while (roll !== 6); console.log("You finally rolled a six!");
copy

1. Which type of loop will always run its code at least once, even if the condition is false to begin with?

2. Fill in the blank to complete the while loop so it stops when count reaches 5.

question mark

Which type of loop will always run its code at least once, even if the condition is false to begin with?

Select the correct answer

question-icon

Fill in the blank to complete the while loop so it stops when count reaches 5.

let count = 0; while () { console.log(count); count++; }
0
1
2
3
4

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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