Differences between the while, do-while, for-in, and for statements in javascript

Source: Internet
Author: User

Do-while is more than a while loop once, I will not give an example.

For loop I believe everyone is familiar with it, so let's look at the for-in sentence.

This is actually for arrays, And the array initialization in js is also quite strange, for example, we write in the script node :( also pay attention to the array initialization, using brackets)

The Code is as follows:
<Script type = "text/javascript">
<! --
Document. write ("test <br/> ");
Var a = [3, 4, 5, 7];
For (var test in ){
Document. write (test + ":" + a [test] + "<br/> ");
}
-->
</Script>
 

For in instance 2

The Code is as follows:
<Html>
<Body>
<Script type = "text/javascript">
Var x
Var mycars = new Array ()
Mycars [0] = "http://www.zhutiai.com/6120c"
Mycars [1] = "sina.com"
Mycars [2] = "163.com"

For (x in mycars)
{
Document. write (mycars [x] + "<br/> ")
}
</Script>
</Body>
</Html>
 

Javascrpt

The Code is as follows:
<Script type = "text/javascript">
For (I = 0; I <= 5; I ++)
{
Document. write ("the number is" + I)
Document. write ("<br/> ")
}
</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:
The step value of the for loop starts from I = 0.

As long as I is less than or equal to 5, the loop continues to run.

Once every cycle of a loop, I will accumulate 1.


Javascrpt do while:

The Code is as follows:
<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:
<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.

</P>
<P> Javascript sample code: The do... while loop statement is used in this Javascript example.
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. </P>
</Body>
</Html>

From the above examples, we can see the differences between js for, for in, while, And do while.

 

From php development
 

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.