JavaScript basic loop

Source: Internet
Author: User

JavaScript Loops

When writing code, you often want to execute the same piece of code over and over again. We can use loops to do this, so we don't have to write several lines of the same code repeatedly.

JavaScript has two different kinds of loops:
For
loop A piece of code to execute a specified number of times
While
Loop execution code when the specified condition is true
For
Loop

Use a For loop when the number of times the script has been run has been determined.

Grammar:
for(variable = start value; variable <= end value; variable = variable + step value) {    code to be executed}
Instance:

Explanation: The following example defines a cyclic program in which the starting value of I is 0. Each time the loop is executed, the value of I accumulates 1, and the loop runs until I equals 10.

Note: The step value can be negative. If the step value is negative, you need to adjust the comparison operator in the for Declaration.

for (i=0;i<=10;i++){
//javaScript的输出

document.write("<br />")// }




The while loop takes advantage of a while loop to loop through code when the specified condition is true.
The Do While loop uses the Do...while loop to loop through code when the specified condition is true.
This loop is executed at least once, even if the condition is false. This is because the statement executes before the condition is validated.

While loop

The while loop is used to loop code execution when the specified condition is true.

Grammar:
while(Variable <= end value) {    code to be executed}

Note: In addition to <=, you can also use other comparison operators.

Instance:

Explanation: The following example defines a cyclic program with a starting value of 0 for the parameter I of the Loop program. The program runs repeatedly until I is greater than 10. The step value of I is 1.

while (i<=10){document.write("The number is " + i)document.write("<br />")i=i+1} </script></body>

Do...while Cycle

The Do...while loop is a variant of the while loop. The loop program executes the code one time at the first run and then continues the loop when the specified condition is true. So, so to speak, the Do...while loop is to execute at least one of the code, even if the condition is false, because conditional validation occurs after the code in it executes.

Grammar:
do{    code to be executed} while (Variable <= end value)
Instance:
do {document.write("The number is " + i)document.write("<br />")i=i+1}while (i<0) </script></body>

JavaScript basic loop

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.