<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The result of the operation is:
Two-cycle structure
A loop can execute a code block for a specified number of times.
JavaScript supports different types of loops:
(1) For statement: Loop code block a certain number of times
for (Var box=1;box<=10;box++) { document.write ("box=" +box+ "<br/>");}
The result of the operation is:
(2) for...in statement: Looping through the properties of an object
var box={ name: "Zhang San", age:24, sex: "Male" }; For (x in box) { document.write (box[x]+ "<br/>"); }
The result of the operation is:
(3) While statement: loops the specified block of code when the specified condition is true. It is more practical to judge and execute the statements first.
var box=1; while (box<=5) { document.write ("box=" +box+ "<br/>"); box++; }
The result of the operation is:
(4) Do...while-The specified code block is also looped when the specified condition is true. Execute once and then judge
var box=1; do{ document.write ("box=" +box+ "<br/>"); box++; } while (box<=10)
The result of the operation is:
Three other statements
(1) Break statement: Used to jump out of a loop.
for (Var box=1;box<=10;box++) { if (box==5) { break;//forces the entire loop to exit } document.write ( "box=" +box+ "<br/>"); }
The result of the operation is:
Execution to the fourth cycle no longer executes, jumping out of the cycle, the output of less box=5 after the cycle.
(2) Continue statement: Used to skip an iteration in the loop.
for (Var box=1;box<=10;box++) { if (box==5) { continue;//exits the current loop and continues with the following loop } document.write ("box=" +box+ "<br/>"); }
The result of the operation is:
Execute to the fourth cycle, jump out of the fifth cycle, continue to the following, output less box=5.
(3) With statement: Set the scope of the code to a specific object
Let's start by looking at how we're going to output the value of an object's properties:
var box={ name: "Zhang San", age:24, sex: "Male" }; var n=box.name; var a=box.age; var s=box.sex; document.write (n+ "<br/>"); document.write (A + "<br/>"); document.write (s);
The result of the operation is:
Use the WITH statement to write:
var box={ name: "Zhang San", age:24, sex: "Male" }; With (box) { var n=name; var a=age; var s=sex; }; document.write (n+ "<br/>"); document.write (A + "<br/>"); document.write (s);
The result of the operation is:
Easy Learning JavaScript Seven: Process Control statements for JavaScript