For loop:
Copy Code code as follows:
For (I=start i<end; i++) {
}
While loop: (Note that if the condition is always true, it goes into the dead loop and the browser hang off)
Copy Code code as follows:
while (condition) {
Do something;
Change condition;
}
recursion:
Use for loop to do substring
Copy Code code as follows:
function substring (all, start, end) {
For (I=start i<=end; i++) {
Console.log (All[i]);
}
SUBSTRING ("Eclipse", 1, 4); Clip
using recursion to implement substring
Copy Code code as follows:
function substring (all, start, end) {
if (start >= end) {
return All[start];
}
else {
return All[start] + substring (all, start+1, end);
}
SUBSTRING ("Eclipse", 1, 4); Clip
To Access object properties using a For loop:
For arrays, strings, we use index [] to access a particular value; For objects, it's the same as using [], but we'll use a special variable: propertyname
Copy Code code as follows:
var person = {
Name: "Morgan Jones",
Telephone: "(650) 777-7777",
Email: "Morgan.jones@example.com"
};
For (Var propertyname in person) {
Console.log (PropertyName + ":" + person[propertyname]);
}
use a For loop to find the data in the array:
Copy Code code as follows:
var table = [
[Person ', ' age ', ' city '],
["Sue", "San Francisco"],
["Joe", "Halifax"]
];
var i;
var rows=table.length;
for (r=0;r<rows;r++) {
var C;
var cells = table[r].length;
var rowtext = "";
for (c=0;c<cells;c++) {
Rowtext + = Table[r][c];
if (c < cells-1) {
Rowtext + = "";
}
}
Console.log (Rowtext);
}
results:
Person Age City
Sue San Francisco
Joe Halifax
--------------------------------------------------------------------------------
Break
Use the break to exit the loop immediately, which applies to the for and while loops.