[JavaScript] JavaScript learning thirteen JavaScript For/while Loops

Source: Internet
Author: User
Document directory
  • Javascript has two different types of loops:
  • Syntax:
  • Instance:
  • Result:
  • Syntax:
  • Instance:
  • Result:
  • Syntax:
  • Instance:
  • Result:

The loop in Javascript is used to specify the number of times the same code is executed (or when the specified condition is true ).

Instance
For Loop
How to Write a loop to execute the same code according to the specified number of times.
Generate HTML titles cyclically
How to Use loop loops to generate different HTML headers.
Javascript loop

When writing code, you often want to execute the same piece of code repeatedly. We can use loops to complete this function, so that we do not need to repeatedly write several lines of the same Code.

Javascript has two different types of loops:
For
Number of times a piece of code is cyclically executed
While
When the specified condition is true, the code is executed cyclically.
For Loop

Use the for loop when the number of scripts is determined.

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

Explanation: The following example defines a loop program. The starting value of I in this program is 0. Every time a loop is executed, the value of I will accumulate 1, and the loop will continue to run until I is equal to 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++){document.write("The number is " + i)document.write("<br />")}</script></body>Result:
The number is 0The number is 1The number is 2The number is 3The number is 4The number is 5The number is 6The number is 7The number is 8The number is 9The number is 10
While Loop

 

The loop in Javascript is used to specify the number of times the same code is executed (or when the specified condition is true ).

Instance
While Loop
The while loop is used to execute code cyclically when the specified condition is true.
Do WHILE LOOP
Use the do... while loop to execute code cyclically when the specified condition is true. This loop will be executed at least once even if the condition is false. This is because the statement is executed before the condition is verified.
While Loop

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

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

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

Instance:

The following example defines a loop program. The starting value of parameter I of this loop program is 0. 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>Result:
The number is 0The number is 1The number is 2The number is 3The number is 4The number is 5The number is 6The number is 7The number is 8The number is 9The number is 10
Do... while loop

Do... while loop is a variant of while loop. During the first running of the loop program, the code is first executed, and then it continues the loop when the specified condition is true. So it can be said that the do... while loop is to execute the code at least once, even if the condition is false, because the code in the loop is executed only after the condition is verified.

Syntax:
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>Result:
The number is 0

 

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.