JavaScript while and do-while cyclic usage

Source: Internet
Author: User


While loop



A while statement, similar to an if statement, has conditions to control the execution of a statement (or statement block), and its language structure is essentially the same:
while (conditions) {
statements;
}



A while statement differs from an if statement in that if the logical conditional expression is true, the statements statement (or statement block) is run and only once, while the loop statement is true in the case of a logical conditional expression Repeatedly executes the statement (or statement block) contained within the loop.



Note: The assignment statement of the loop variable of the while statement is before the loop body, and the loop variable update is placed in the loop body; the loop variable assignment and UPDATE statement for the FOR Loop statement are in parentheses behind for, and the difference in programming should be noted.



"Example 3-5" uses a while () loop to compute the value of 1+2+3 ... +98+99+100:


The code is as follows
<title> calculate the value of 1+2+3 ... +98+99+100 </title>
<body>
<script language= "JavaScript" type= "Text/javascript" >
var total=0;
var I=1;
while (i<=100) {
Total+=i;
i++;
}
Alert (total);
</script>
</body>


In some cases, a statements statement (or statement block) within a while loop brace may not be executed once, because the operation of the logical conditional expression precedes the execution of the statements statement (or statement block). If the logical conditional expression evaluates to False, the program skips the loop directly and does not execute the statements statement (or statement block) at once.



Do...while Cycle



If you want to execute at least one statements statement (or statement block), you can use the Do...while statement with the following basic syntax structure:



do {
statements;
}while (condition);



"Example 3-6" uses the Do-while () loop to compute the value of the 1+2+3 ... +98+99+100:


The code is as follows

<title> calculate the value of 1+2+3 ... +98+99+100 </title>
<body>
<script language= "JavaScript" type= "Text/javascript" >
var total=0;
var I=1;
do{
Total+=i;
i++;
}while (i<=100);
Alert (total);
</script>
</body>





For, while, do...while three kinds of circular statements have essentially the same function, in the actual programming process, should be based on the actual needs and in accordance with the principle of simple to understand the choice of circular statements.



Do While instance





The code is as follows
<title> A JavaScript example that uses the Do...while loop </title>
<body>
<p>
<script type= "Text/javascript" >
i = 0
Todo
{
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.
Explain:
I equals 0.
The loop will run first.
Once per cycle, I will accumulate 1.
When I is less than or equal to 5 o'clock, the loop continues to run.
Javascrpt while


The code is as follows

<script type= "Text/javascript" >
i = 0
while (I <= 5)
{
document.write ("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.
Explain:
I equals 0.



When I is less than or equal to 5 o'clock, the loop continues to run.
Every time the loop runs, I will accumulate 1.
JavaScript Sample Code Explanation: This JavaScript example uses the Do...while loop statement.
A loop statement allows you to repeat a line or lines of code, followed by a repeating code, while followed by a condition that terminates the loop. In this JavaScript example, set a variable to the I,i initial value of 0,i++ to add 1 to the value of I after each repetition, terminating the loop condition as while (I <= 5), that is, to terminate the loop once I has a value greater than 5. In this example, a repeating loop statement is a document.write statement inside a while loop.



From the above example, we can see the difference between JS while and do.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.