4. Logic Control Statement
4.1-Piece structure
The conditional structure is divided into the if structure and the switch structure.
Often when writing code, you always need to perform different actions for different decisions. You can use conditional statements in your code to accomplish this task.
In JavaScript, we can use the following conditional statements:
-
- If statement-use this statement to execute code only if the specified condition is true.
- If...else Statement-executes code when the condition is true, and executes additional code when the condition is false.
- If...else If....else Statement-Use this statement to select one of several code blocks to execute.
- Switch statement-Use this statement to select one of several code blocks to execute.
4.1.1 If structure
The statement executes code only if the specified condition is true.
Grammar:
if (condition)
{
Execute code when condition is True
}
Please use the lowercase if. Using uppercase letters (IF) generates JavaScript errors!
If...else statements
Use the If....else statement to execute code when the condition is true, and to execute additional code when the condition is false.
Grammar:
if (condition)
{
Code to execute when the condition is true
}
Else
{
Code to execute when the condition is not true
}
Code Style recommendations:
if (condition)
{
DoSomething ();
}
Else
{
Dosomethingelse ();
}
This style of JavaScript code is inherited from Java, which is clearly defined in the programming specification of the Java programming language. In Crockford programming specifications, the jquery Core style guide, the Sqroutcore Programming style guide,
Both Google's JavaScript style guides and the Dojo programming style guides appear.
Related examples:
The wrong wording
if (condition) {
DoSomething ();
}
Dosomethingelse ();
The wrong wording
if (condition) {
DoSomething ();
Dosomethingelse ();
}
//Bad wording, though This is the code for the legitimate JavaScript
if (condition)
DoSomething ();
//Bad wording, though This is the code for the legitimate JavaScript
if (condition) dosomething ();
//Bad wording, though This is the code for the legitimate JavaScript
if (condition) { dosomething (); }
Good wording.
if (condition) {
DoSomething ();
}
4.1.2 Switch Structure
Use the switch statement to select one of several code blocks to execute.
Grammar:
Switch (n) {
Case 1:
Executing code block 1
Break
Case 2:
Executing code block 2
Break
Default
N code that executes differently than case 1 and case 2
}
Working principle:
First, set the expression n (usually a variable). The value of the subsequent expression is compared to the value of each case in the structure. The code block associated with the case is executed, using break to prevent the code from automatically running down a case.
The key word for default uses:
Use the default keyword to specify what happens when a match does not exist, depending on your business. You can also add a break or a last case after a case to end directly.
Related examples:
Style One:
Switch (condition) {
Clearly executed in turn
Case "First":
Code
Break
Case "Second":
Code
Break
Case "Third":
Code
/* Fall through */
Default
Code
}
I am more accustomed to using this style, and Java syntax is the same.
Style Two:
Switch (condition) {
Case "First":
Code
Break
Case "Second":
Code
Break
Case "Third":
Code
Break
Default
Code
}
This style is advocated by the Crockford programming specification and the Dojo Programming style guide.
4.2 Cycle Structure
If you want to run the same code over and over again, and the values are different each time, it is convenient to use loops.
Different types of loops:
JavaScript supports different types of loops:
-
- For-loop code block for a certain number of times
- For/in,for/each-Looping through the properties of an object
- While-loops the specified block of code when the specified condition is true
- Do/while-also loops the specified block of code when the specified condition is true
4.2.1 For Loop
A For loop is a tool that you will often use when you want to create loops.
Here is the syntax for the FOR loop:
for (statement 1; Statement 2; Statement 3)
{
code block to be executed
}
Execute starts before statement 1 (code block) starts
Statement 2 defining conditions for running loops (code blocks)
Statement 3 executes after a loop (code block) has been executed
Example 1:
for (Var i=0;i<5;i++) {
X=x+ "The figure is" +i+ "<br>";
}
From the example above, you can see:
Statement 1 Sets the variable initialization (Var i=0) before the loop begins.
Statement 2 defines the conditions for the loop to run (I must be less than 5).
Statement 3 adds a value (i++) after each code block has been executed.
Example 2:
var i=0,len=cars.length;
for (; i<len;) {
document.write (cars[i]+ "<br>");
i++;
}
Statement 1 is optional and can be defined and initialized outside of the loop.
Statement 2 is a condition that evaluates an initial variable and is also optional.
Statement 3 is also optional, with multiple usages such as: increments can be negative (i--), or larger (i=i+15). The value of the initial variable is usually increased.
4.2.2.1 For/in Cycle
The for/in statement loops through the properties of the object:
Grammar:
For (variable in object) {
}
Instance:
var person= (fname: "JSON", lname: "Doe", age:25);
for (x in person) {
TXT=TXT+PERSON[X];
}
Note: The code blocks in the for...in loop are executed once for each property.
4.2.2.2 For/each loops are iterative loops (not recommended for reasons that affect performance)
The For/each statement loops through the value of the object type variable:
Grammar:
for (element type T element variable x: Traverse object obj) {
A JavaScript statement that references X;
}
Related examples:
var arr[] = {2, 3, 1},var x=null;
document.write ("----1----one-dimensional array before sorting");
for (X:arr) {
document.write (x); Output the value of an array element individually
} 4.2.3 While loop as long as the condition is specified
4.3 Cyclic interrupts
5, note 6, grammar rules
......
JavaScript basic Syntax (bottom)