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

Kurssisisältö

Introduction to JavaScript

Introduction to JavaScript

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

book
The `while` Loop

The while loop is another kind of loop supported by most programming languages including JavaScript.

The while loop is mainly used when we want to execute a block of code as long as a condition is true. Although it may behave similar to a for loop in certain circumstances, it is mainly used in cases where we don't know exactly how many times a block of code needs to be executed.

The general syntax of a while loop is:

python
Note
Note

If the condition of a while loop is always true, it will execute forever. Such a loop is known as an infinite loop.

The process of a while loop can be better understood by looking at it's flowchart:

Following is an example program which utilizes a while loop to find the first number that is divisible by both 11 and 12:

123456
let i = 13; while(i % 11 != 0 && i % 12 != 0) { i += 1; } console.log("The first number divisible by both 11 and 12 is:", i);
copy

1. What is the main difference between a for loop and a while loop?

2. How many times will the following loop execute?

3. What will happen if the condition in a while loop is always true?

question mark

What is the main difference between a for loop and a while loop?

Select the correct answer

question mark

How many times will the following loop execute?

Select the correct answer

question mark

What will happen if the condition in a while loop is always true?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 6. Luku 3

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

course content

Kurssisisältö

Introduction to JavaScript

Introduction to JavaScript

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

book
The `while` Loop

The while loop is another kind of loop supported by most programming languages including JavaScript.

The while loop is mainly used when we want to execute a block of code as long as a condition is true. Although it may behave similar to a for loop in certain circumstances, it is mainly used in cases where we don't know exactly how many times a block of code needs to be executed.

The general syntax of a while loop is:

python
Note
Note

If the condition of a while loop is always true, it will execute forever. Such a loop is known as an infinite loop.

The process of a while loop can be better understood by looking at it's flowchart:

Following is an example program which utilizes a while loop to find the first number that is divisible by both 11 and 12:

123456
let i = 13; while(i % 11 != 0 && i % 12 != 0) { i += 1; } console.log("The first number divisible by both 11 and 12 is:", i);
copy

1. What is the main difference between a for loop and a while loop?

2. How many times will the following loop execute?

3. What will happen if the condition in a while loop is always true?

question mark

What is the main difference between a for loop and a while loop?

Select the correct answer

question mark

How many times will the following loop execute?

Select the correct answer

question mark

What will happen if the condition in a while loop is always true?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 6. Luku 3
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt