S: The For loop here refers to the language of JS oh ...
The composition structure of the For loop;
The code is as follows |
Copy Code |
Why should var i;//be defined in this? See jquery source For (i=0 i<10; i++) { This is the loop block. }
|
This is a very ordinary "woman", but do you really know her? The following fields are divided:
Red: Defining blocks
Green: Judgment block
Blue: Calculated block
Pink: Execution Block
PS: The name is I up, ah, not the official website
So what is the order of their execution?
Order of execution for loops
Modify the code as:
The code is as follows |
Copy Code |
Why should var i;//be defined in this? See jquery source For (I=0,console.log ("Red block:" + i); Console.log ("Green block:" + i),i<3; Console.log ("Blue block:" + i), i++) { Console.log ("Pink block:" + i) This is the loop block. }
|
The results of the console run are:
However, if the conditions are not tenable:
If the execution block has a break,continue keyword:
See results get the following rules:
Order of execution:
Red-> green-> pink-> Blue
Definition block-> judgment block-> execution Block-> compute block
rule:
Red definition block: Used to define a variable (refers to the identity of a loop, as : i), execute only once, after the last step to the next
Green judgment block: Used to determine whether the condition is set up, if it is set to proceed to the next, if not set up then exit the entire cycle
Pink execution block: Used to execute code, if there is a break do not execute the following code, and do not Inue do not execute the following code and proceed to the next
Blue Compute BLOCK: Used to compute variables such as + +,--
PS: Don't get tripped by ++i, i++ 's pit.
Wonderful for loop
code is as follows |
copy code |
//definition block content var i = 0, len = 10;//data.length for (; I <len; i++) { console.log (i) } //definition, calculated block no content var i = 0, len = ten F or (; i<len;) { console.log (i++); know the For loop execution order is this a vegetable dish? //Even so ... *_* var i = 0, len = ten for (;;) { console.log (i); if (++i >=) { break; } } //feel less than while var i = 0; while (I <) { console.log (i++); } |
Do you have any sense of a new understanding for a?