Usage of JavaScript For While LOOP

Source: Internet
Author: User

In js, loops include For loops and while loops. They are not much different, but they will be used more in some places. Next I will introduce you to the usage of JavaScript For While 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.

The Code is as follows: Copy code

<Html>
<Body>

<Script type = "text/javascript">
Var I = 0for (I = 0; I <= 10; I ++)
{
Document. write ("The number is" + I)
Document. write ("<br/> ")
} </Script>

</Body>
</Html> result:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10


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.

The Code is as follows: Copy code

<Html>
<Body>
<Script type = "text/javascript">
Var I = 0 while (I <= 10)
{
Document. write ("The number is" + I)
Document. write ("<br/> ")
I = I + 1
} </Script>
</Body>
</Html>

Result:

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The 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:

The Code is as follows: Copy code

<Html>
<Body>
<Script type = "text/javascript">
Var I = 0do
{
Document. write ("The number is" + I)
Document. write ("<br/> ")
I = I + 1
}
While (I <0) </script>
</Body>
</Html>

Result:
The number is 0

How can we exit the loop? In js, we can use break and continue to exit the loop.


There are two special statements available in the loop: break and continue.

Break
The break command can terminate the running of the loop and then continue executing the code after the loop (if there is code after the loop ).

Instance:

The Code is as follows: Copy code

<Html>
<Body>
<Script type = "text/javascript">
Var I = 0
For (I = 0; I <= 10; I ++)
{
If (I = 3) {break} document. write ("The number is" + I)
Document. write ("<br/> ")
}
</Script>
</Body>
</Html>

Result:
The number is 0
The number is 1
The number is 2

Continue
The continue command terminates the current loop and continues running from the next value.

Instance:

The Code is as follows: Copy code

<Html>
<Body>
<Script type = "text/javascript">
Var I = 0
For (I = 0; I <= 10; I ++)
{
If (I = 3) {continue} document. write ("The number is" + I)
Document. write ("<br/> ")
}
</Script>
</Body>
</Html>

Result:
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10

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.