Home > Web Front-end > JS Tutorial > body text

How to use the while loop statement in JavaScript

不言
Release: 2018-12-21 09:09:09
Original
5319 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!