js's core ECMAScript the Process Control statements and other programming languages are quite similar. Let's take a few practical examples to see
these statements. The order structure we are not talking about here, directly say conditions and loops and other statements.
One, conditional selection structure
Conditional selection statements are used to perform different actions based on different conditions, usually when writing code You always need to perform different
actions for different decisions, and you can use conditional statements in your code to complete the task.
in JavaScript, we can use the following conditional statement:
If statement: Use this statement to execute code
only if the specified condition is true
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The results of the run are:
If...else statement: executes code when the condition is true, and executes other code when the condition is false
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The results of the run are:
if...else if....else statement: Use this statement to select one of several code blocks to execute
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The results of the run are:
switch statement: Use this statement to select one of several code blocks to execute. Switch statements are used to perform different actions based on different conditions
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The results of the run:
Use of the default keyword
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The results of the run are:
Second, the circulation structure
Loops 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 results of the run are:
(2) for...in statement: Looping through the properties of an object
var box={
Name: "John",
age:24,
sex: "Male"
};
For (x in box)
{
document.write (box[x]+ "<br/>");
}
The results of the run are:
(3) While statement: loops The specified code block when the specified condition is true. Judge first, then execute the statement, this is more practical.
var box=1;
while (box<=5)
{
document.write ("box=" +box+ "<br/>");
box++;
}
The results of the run are:
(4) Do...while- also loops the specified code block when the specified condition is true. Do it first, then judge
var box=1;
do{
document.write ("box=" +box+ "<br/>");
box++;
} while (box<=10)
The results of the run are:
Third, other statements
(1) Break statement: Used to jump out of the loop.
for (Var box=1;box<=10;box++)
{
if (box==5)
{
break;//force exits the entire loop
}
document.write ( "box=" +box+ "<br/>");
The results of the run are:
Execution to the fourth cycle no longer executes, out of the whole cycle, the output of less box=5 after the cycle.
(2) Continue statement: used to skip an iteration in a loop.
for (Var box=1;box<=10;box++)
{
if (box==5)
{
continue;//exits the current loop and continues to perform the following loop
}
document.write ("box=" +box+ "<br/>");
The results of the run are:
Execution to the fourth cycle, jump out of the fifth cycle, continue to the following, the output of less box=5.
(3) With statement: Setting the scope of the code to a specific object
Let's see how we normally output the value of an object's properties:
var box={
Name: "John",
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 results of the run are:
To write with the WITH statement instead :
var box={
Name: "John",
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 results of the run are:
From the three major aspects of the JavaScript process Control statements, I hope that we read carefully, the number of JavaScript flow control statements to master the use of the method.