In js, the loop statements while and do while are also one of our commonly used loop statements. Next I will introduce you to the usage of the while and do-while loops, and hope to help you.
While Loop
The while statement is similar to the if statement and has conditions to control the execution of the statement (or statement block). Its language structure is basically the same:
While (conditions ){
Statements;
}
The while statement differs from the if statement in that in the if condition assumption statement, if the logical condition expression is true, the statements Statement (or statement block) is run only once; when the while LOOP expression is true, statements (or statement blocks) contained in the loop are repeatedly executed ).
Note: The value assignment statement of the while statement is placed in the loop body before the loop body; the value assignment and update statements of the for Loop statement are in parentheses after the for statement. You should pay attention to the differences in programming.
[Example 3-5] use the while () loop to calculate the value of 1 + 2 + 3... + 98 + 99 + 100:
The Code is as follows: |
Copy code |
<Html> <Head> <Title> calculate the value of 1 + 2 + 3... + 98 + 99 + 100 </title> </Head> <Body> <Script language = "JavaScript" type = "text/javascript"> Var total = 0; Var I = 1; While (I <= 100 ){ Total + = I; I ++; } Alert (total ); </Script> </Body> </Html> |
In some cases, the statements Statement (or statement block) in the while LOOP braces may not be executed once, because the operation on the logical conditional expression is executing the statements Statement (or statement block) before. If the calculation result of the logical conditional expression is false, the program directly skips the loop and does not execute the statements Statement (or statement block) at one time ).
Do... while loop
If you want to execute the statements Statement (or statement block) at least once, you can use do... The basic syntax structure of the while statement is as follows:
Do {
Statements;
} While (condition );
[Example 3-6] use the do-while () loop to calculate the value of 1 + 2 + 3... + 98 + 99 + 100:
The Code is as follows: |
Copy code |
<Html> <Head> <Title> calculate the value of 1 + 2 + 3... + 98 + 99 + 100 </title> </Head> <Body> <Script language = "JavaScript" type = "text/javascript"> Var total = 0; Var I = 1; Do { Total + = I; I ++; } While (I <= 100 ); Alert (total ); </Script> </Body>
|
</Html>
For, while, do... The while statements have basically the same functions. In the actual programming process, you should select the loop statements based on actual needs and the simple and easy-to-understand principle.
Do while instance
The Code is as follows: |
Copy code |
<Html> <Head> <Title> A Javascript example using do... while loop </title> </Head> <Body> <P> <Script type = "text/javascript"> I = 0 Do { Document. write (I + "<br> ") I ++ } While (I <= 5) </Script> |
The number is 0.
The number is 1.
The number is 2.
The number is 3.
The number is 4.
The number is 5.
Explanation:
I is equal to 0.
The loop runs first.
I will accumulate 1 for each loop.
When I is less than or equal to 5, the loop continues to run.
Javascrpt while
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> I = 0 While (I <= 5) { Document. write ("the number is" + I) Document. write ("<br> ") I ++ } </Script>
|
The number is 0.
The number is 1.
The number is 2.
The number is 3.
The number is 4.
The number is 5.
Explanation:
I is equal to 0.
When I is less than or equal to 5, the loop continues to run.
Once a loop is run, I will accumulate 1.
Javascript sample code explanation: This Javascript example uses the do... while loop statement.
Loop statements allow repeated execution of one or several lines of code. do is followed by the code that is repeated, while is followed by the condition that the loop is terminated. In this Javascript example, set a variable to I, and the initial I value to 0. I ++ indicates that the value of I is added to 1 after each repeated execution, the condition for terminating the loop is while (I <= 5). That is, once the I value is greater than 5, the loop is terminated. In this example, the statement for repeating the loop is the document. write statement in the while loop.
From the above examples, we can see the differences between js while and do while.