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 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<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.
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
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
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!