Home Web Front-end JS Tutorial How to use the while loop statement in JavaScript

How to use the while loop statement in JavaScript

Dec 17, 2018 pm 04:40 PM
while

Loop statements in How to use the while loop statement in How to use the while loop statement in How to use the while loop statement in JavaScript We have already introduced the for loop statement in the previous article. In the next article, we will introduce the usage of the while loop statement.

How to use the while loop statement in How to use the while loop statement in How to use the while loop statement in JavaScript

How to use the for loop statement in How to use the while loop statement in How to use the while loop statement in How to use the while loop statement in JavaScriptIn this article we already know that the while loop statement is suitable for use when the number of loops is not fixed , so let’s take a look at the specific use of while loop statements.

There are two statements in the while statement: while and do while

Let’s take a look at how to use these two statements

The syntax of while statement

while(条件表达式){ 
循环处理
}
Copy after login

In the case of while statement, the conditional expression is first calculated and execution starts in a loop until the conditional expression matches.

During the loop process, the conditional expression in () is true, and the loop execution in {}

At this time, if the conditions are not restricted, it will continue to loop

The syntax of do while statement

do {
循环处理
} while (条件表达式)
Copy after login

Compared with the while statement, in the case of the do while statement, loop processing is performed first, and then the conditional expression of while is judged. If it is true, continue with the process.

Let’s look at a specific example

The variable is set to count

while statement

<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>How to use the while loop statement in How to use the while loop statement in How to use the while loop statement in JavaScript</title>
</head>
<body>
<script>
var count = 0;
while (count < 10) {
document.write (count);
count++
}
</script>
</body>
</html>
Copy after login

If this variable count is less than 10 (count<10), continue to use document.write output value for processing.

The last count represents the count that is incremented each time the process is looped. Let us know if it doesn't exist you will keep the value of 0 for the while loop.

Then, when count becomes 0,1,2,... When count is 10, the conditional expression of count <10 becomes false, and the loop can be exited.

So it will be output in the browser as follows.

How to use the while loop statement in How to use the while loop statement in How to use the while loop statement in JavaScript

do while statement

<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>How to use the while loop statement in How to use the while loop statement in How to use the while loop statement in JavaScript</title>
</head>
<body>
<script>
var count = 0;
do {
document.write (count);
count++;
} while (count < 10);
</script>
</body>
</html>
Copy after login

It will be output in the browser as follows without any changes

How to use the while loop statement in How to use the while loop statement in JavaScript

When the value assigned to count from the beginning is 10 or greater

Execution of while statement

<script>
var count = 20;
while (count < 10) {
document.write (count);
count++
}
</script>
Copy after login

Because the judgment is false, nothing is output in the while statement.

do while statement execution

<script>
var count = 20;
do {
document.write (count);
count++;
} while (count < 10);
</script>
Copy after login

In the case of do while, processing will be performed regardless of the condition in the first one, and it will be judged only after the second time true or false

The operation result is as follows

How to use the while loop statement in How to use the while loop statement in How to use the while loop statement in JavaScript

The above is the detailed content of How to use the while loop statement in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In C language, what is the difference between while(1) and while(0)? In C language, what is the difference between while(1) and while(0)? Aug 31, 2023 am 10:45 AM

We know that in C language, 'while' keyword is used to define a loop that works based on the condition passed to the loop. Now, since the condition can have two values, true or false, the code inside the while block will be executed repeatedly if the condition is true and will not be executed if the condition is false. Now, by passing parameters to the while loop, we can differentiate between while(1) and while(0) because while(1) is a loop where the condition is always considered true and hence the code inside the block will start executing repeatedly. Furthermore, we can state that it is not 1 that is passed to the loop that makes the condition true, but if any non-zero integer is passed to the while loop, then it will be considered as the true condition, so

Is while a keyword in go language? Is while a keyword in go language? Jun 04, 2021 pm 05:01 PM

In the Go language, while is not a keyword. You can use the for statement plus break to achieve the effect of a while loop, such as "for {sum++ if sum>10{break}else{...}}". The go language has 25 keywords such as break, default, func, select, case, defer, go, map, else, goto, for, if, var, etc.

Usage of while Usage of while Sep 25, 2023 am 09:47 AM

The usage of while is "while condition: code block". The condition is an expression. When the condition is true, the code block is executed, and then it is judged again whether the condition is true. If it is true, the code block continues to be executed until the condition is false. . while is a commonly used loop control statement, used to repeatedly execute a block of code when certain conditions are met.

What are the common flow control structures in Python? What are the common flow control structures in Python? Jan 20, 2024 am 08:17 AM

What are the common flow control structures in Python? In Python, the flow control structure is an important tool used to determine the execution order of the program. They allow us to execute different blocks of code based on different conditions, or to execute a block of code repeatedly. The following will introduce common process control structures in Python and provide corresponding code examples. Conditional statements (if-else): Conditional statements allow us to execute different blocks of code based on different conditions. Its basic syntax is: if condition 1: #when condition

Detailed explanation of PHP While loop: Comprehensive mastery of loop principles and application scenarios Detailed explanation of PHP While loop: Comprehensive mastery of loop principles and application scenarios Apr 09, 2024 pm 03:27 PM

While loop is a control flow structure used to execute statements repeatedly until a condition is false. The principle is to check the condition, and if it is true, execute the loop body, and then check the condition until the condition is false. It is suitable for situations where an operation needs to be performed repeatedly, an operation needs to be performed when a condition is true, or a loop needs to be jumped out based on a condition. Practical examples include printing even numbers, calculating prime numbers, and using the break statement to break out of loops.

Detailed explanation of the function and usage of break keyword in PHP Detailed explanation of the function and usage of break keyword in PHP Jun 28, 2023 pm 06:39 PM

Detailed explanation of the role and usage of the break keyword in PHP In PHP programming, break is a control flow statement used to interrupt the current loop or switch statement and jump out of the loop or switch. This article will introduce in detail the role and usage of the break keyword. 1. Break in a loop In a loop structure, the function of break is to terminate the loop early and jump out of the loop body to execute the code after the loop. Common loop structures include for, while and do...while. in for loop

How to use the while statement in java How to use the while statement in java Apr 19, 2023 am 09:28 AM

Note 1. The Chinese meaning of the while keyword is when..., that is, when the condition is established, the corresponding code is executed in a loop. The while statement is the basic structure in the loop statement, and the syntax format is relatively simple. Execution process 2. When executing a while statement, first determine the loop condition. When the loop condition is false, the subsequent code of the while statement is directly executed. When the loop condition is true, the loop body code is executed and the loop condition is determined until the loop condition is not established. Example inti=1;intsum=0;while(i

Using PHP While Loop: Improve Program Efficiency and Readability Using PHP While Loop: Improve Program Efficiency and Readability Apr 09, 2024 pm 12:18 PM

PHPWhile loop improves efficiency and readability: By using termination conditions and encapsulating repeated code, while loops can reduce calculations, enhance readability, and simplify code.

See all articles