1. Conditional branch statement : if
Basic format:
if (<expression 1>){
<Sentence Group 1>
}else if (<expression 2>){
<Sentence Group 2>
}else{
<Sentence Group 3>
}
Execution process:
2. Circular statements
2.1 Pre-test loop statement: Evaluate the exit condition before the code in the loop body is executed.
2.1.1 while statement
Basic format:
do {
<Sentence Group>
} while (<expression>)
Execution process:
2.1.2 for statement
Basic format:
for (<initial expression>; <conditional expression>; <variable expression>){
<Sentence Group>
}
Execution process:
2.2 Test loop statement: The exit conditions will be tested only after the code in the loop body is executed.
2.2.1. do-while statement
Basic format:
do {
<Sentence Group>
} while (<expression>);
Execution process:
3. Accurate iterative statement : for-in
Basic format:
for (property in object){
<Sentence Group>
}
Function: Repeat all properties of the specified object, which can be used to enumerate the properties of the object.
example:
The code copy is as follows:
<html>
<body>
<p>Click the button below to loop through the properties of the object "person". </p>
<button onclick="myFunction()">Click here</button>
<p id="demo"></p>
<script>
function myFunction()
{
var x;
var txt="";
var person={fname:"Bill",lname:"Gates",age:56};
for (x in person)
{
txt=txt + person[x];
}
document.getElementById("demo").innerHTML=txt;
}
</script>
</body>
</html>
If the object to be represented is null or undefined, the loop body will no longer be executed, or an error will be thrown. Therefore, when executing the for-in loop, you should first check whether the property value of the changed object is null or undefined.
4. Lable statement
Basic syntax:
Label: <Sentence Group>
For example: begin: for (var i = 0; i < 10; i++ ){
alert(i);
}
Give an example to illustrate the function of a lable statement:
Unplugged:
The code copy is as follows:
var num = 0;
for (var i = 0 ; i < 10 ; i++){
for (var j = 0 ; j < 10 ; j++){
if( i == 5 && j == 5 ){
break;
}
num++;
}
}
alert(num);//95
Join lable:
The code copy is as follows:
var num = 0;
outPoint:
for (var i = 0 ; i < 10 ; i++){
for (var j = 0 ; j < 10 ; j++){
if( i == 5 && j == 5 ){
break outPoint;
}
num++;
}
}
alert(num); //55
The first example output 95 is not difficult to understand. Why output 55 in the second example is because when executing the break outpoint, it jumps out to the putpoint layer and executes the alert statement.
If you change the second example to the following:
The code copy is as follows:
var num = 0;
for (var i = 0 ; i < 10 ; i++){
outPoint:
for (var j = 0 ; j < 10 ; j++){
if( i == 5 && j == 5 ){
break outPoint;
}
num++;
}
};
alert(num);//95
This result is consistent with the results of the first example.
5. Break and continue statements
5.1break statement:
The code copy is as follows:
var num = 0;
for (var i = 1 ; i < 10 ; i++){
if(i%5==0){
break;
}
num++;
};
alert(num);//4
After the break statement is executed, jump to the alert statement
5.2 continue statement:
The code copy is as follows:
var num = 0;
for (var i = 1 ; i < 10 ; i++){
if(i%5==0){
continue;
}
num++;
};
alert(num);//8
After the continue statement is executed, jump to the for() loop and continue to execute the loop until the loop condition is not true.
6. With statement
Basic syntax:
with (object) {
statements
}
Give an example:
Writing without using:
The code copy is as follows:
var qs = location.search.substring(1);
var hostName = location.hostname;
var url = location.href;
alert(qs);
alert(hostName);
alert(url);
Use with writing method:
The code copy is as follows:
with(location){
var qs = search.substring(1);
var hostName = hostname;
var url = href;
}
alert(qs);
alert(hostName);
alert(url);
From the above example, we can see that the purpose of the with statement is to set the scope of the code into a specific object and reduce repeated input.
However, the js interpreter needs to check whether the variables in the with block belong to the object contained in the with block, which will greatly reduce the execution speed of the with statement and make it difficult for js statements to be optimized.
Therefore, it is not recommended to use with statements on a large scale.
Swe statement
Basic syntax:
switch (<expression>) {
case <value 1>:<statement group 1>
break;
case <value 2>:<statement group 2>
break;
...
default <Statement Group>
}
Execution process:
The switch statement can be any data type, and the value of each case may not be a constant, but may also be a variable, an expression, etc., for example:
The code copy is as follows:
switch ("hello world") {
case "hello" + " world":
alert("Greeting was found.");
break;
case "goodbye":
alert("Closing was found.");
break;
default:
alert("Unexpected message was found.");
}
The code copy is as follows:
var num = 25;
switch (true) {
case num < 0:
alert("Less than 0.");
break;
case num >= 0 && num <= 10:
alert("Between 0 and 10.");
break;
case num > 10 && num <= 20:
alert("Between 10 and 20.");
break;
default:
alert("More than 20.");
}
The switch statement uses congruent operator comparison when comparing, so no type conversion occurs.
practise:
The code copy is as follows:
<script type="text/javascript">
var count = 10;
for (var i=0; i < count; i++){
alert(i);
}
alert(i); //What outputs?
</script>
for(;;){
alert("2");//How many times does 2 output?
}
The above is the entire content of this article, and I hope it will be helpful to my friends.