The order of execution of 3 parts separated by semicolons in the For loop explore _javascript techniques

Source: Internet
Author: User
This problem is caused by the running results of a JS program:
Copy Code code as follows:

var i = 0;
function A () {
for (i=0;i<20;i++) {
}
}
Function B () {
for (i=0;i<3;i++) {
A ();
}
return i;
}
var result = B ();

The operation result of this procedure is results = 21;

As we can see from this program, I have a value of 20 when the A function returns, which is no problem.
When the B function returns, the value of I is 20 or 21 is worth discussing.
The essence of the problem is: the first to Judge I<3, or first i++, and then judge whether the i<3.

According to the result of the execution, it is shown that the i++ is executed first.
Copy Code code as follows:

function A () {
for (i=0;i<20;i++) {
No var i
Here's I is the global variable everyone can access
}
}
Function B () {
for (i=0;i<3;i++) {
alert (i)//empathy Here I am also a global variable, returns 0 and returns only once
A ();//This function returns is I=20
When i=20 after i++ i=21 then does not conform to the i<3 condition, exits directly. So return i=21 this is normal!
}
return i;
}
var result = B ();

Here we complete the order in which the For loop is executed:
Take the following procedure as an example
Copy Code code as follows:

for (int i=0;i<10;i++)
{
}

First Execute i=0;i<10; Then execute the first round of the loop body
Then execute the:i++,i<10; Then execute the second round of the loop body
Until the last i++ I >=10, at this time the loop ends.

That

Statement 1 executes before the Loop (code block) starts

Statement 2 defines conditions for running loops (code blocks)

Statement 3 executes after the Loop (code block) has been executed

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.