Home  >  Article  >  Web Front-end  >  How to use the while loop statement in JavaScript

How to use the while loop statement in JavaScript

不言
不言Original
2018-12-17 16:40:215309browse

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(条件表达式){ 
循环处理
}

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 (条件表达式)

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>

If this variable count is less than 10 (count

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

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>

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>

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>

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