Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre The `do-while` Loop | Discovering Loops
Introduction to JavaScript
course content

Contenu du cours

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
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:

js

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:

123456
let i = 1; do { console.log(i * 2); i += 1; } while (i <= 10);
copy

Even if we change the value of i, such that the condition becomes false, the code block will still execute at least once:

123456
let i = 11; do { console.log(i * 2); i += 1; } while (i <= 10);
copy

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?

question mark

What is the key difference between a while loop and a do-while loop?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 6. Chapitre 5

Demandez à l'IA

expand
ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

course content

Contenu du cours

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
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:

js

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:

123456
let i = 1; do { console.log(i * 2); i += 1; } while (i <= 10);
copy

Even if we change the value of i, such that the condition becomes false, the code block will still execute at least once:

123456
let i = 11; do { console.log(i * 2); i += 1; } while (i <= 10);
copy

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?

question mark

What is the key difference between a while loop and a do-while loop?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 6. Chapitre 5
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt