Web Design
Mobile Internet
Brand Design
Innovative
News
Encyclopedias

Statement structure of Javascript

Date:2016-07-26 Source: Shangpin China Type: website encyclopedia
Word Size: small   medium   big

The flow of JavaScript is the order in which program statements run. By default, JavaScript runs in the order of statements, which is called sequential run. In addition to running in order, JavaScript also allows some special running order structures, through which you can write some complex structure Website production script.

1、 Select Structure

The selection structure is usually used to indicate multiple running sequences or directions of program code, and create an intersection for these sequences or directions. The procedures for selecting structure can be divided into the following 4 types.

1. Single choice structure

The single choice structure refers to the use of JavaScript statements to test a condition. When the condition meets the test requirements, some commands are executed. A single selection structure requires an if statement, as shown below.

 If statement

2. Two way selection structure

The two-way selection structure refers to testing a condition with JavaScript, and executing a command when the condition meets the test requirements. When the condition does not meet the test requirements, execute another command. Since the program will have two options after testing, it is called two-way selection structure. If.. Is required for two-way selection structure Else statement, as shown below.

 if .. Else statement

3. Inline ternary operator

JavaScript also supports implicit conditional formats, which use a question mark (?) after the conditions. If two options need to be specified for such conditions, they can be separated by a colon (:), as shown below.

 Inline ternary operator

4. Multiple selection structure

The selection structures previously described are single or dual selection structures. JavaScript also supports multiple selection structures. If you need to test multiple conditions, you can add switches to the program Case statement, as shown below.

 Multiplexer structure

2、 Cyclic structure

In JavaScript, you can also use loop structures. The cycle structure is characterized by multiple operations under certain conditions until certain conditions are met. For example, the program that prints out the multiplication table needs to use this structure.

1. Cycle controlled by counter

This loop needs to use the for statement to specify a counter variable, a test condition, and the operation to update the counter. This condition is tested before each cycle is repeated. If the test is successful, the code in the loop will be run; If the test is unsuccessful, the code in the loop will not be run, and the program will continue to run the first line of code immediately after the loop, as shown below.

 Cycle controlled by counter

After the loop is executed, the computer variables will be updated before the next loop. If the loop condition is met, the loop will stop execution. If the test conditions are not met, it will lead to an infinite loop, that is, an endless loop. When designing the program, we should try our best to avoid the occurrence of dead cycle.

1. Operate each attribute of the object

JavaScript also provides a special loop way to traverse all user-defined attributes of an object or all elements of an array. Fo... in The loop counter in the loop is a string, not a number. It contains the name of the current attribute or the subscript of the current array element, as shown below.

 Operate on each attribute of the object

2. Test the expression at the beginning of the cycle

If you want to control the circular execution of a statement or statement block, you need not only "run the code n times", but more complex rules, you need to use the while loop. The while loop is similar to the for loop, but the difference is that the while loop does not have a built-in counter or update expression, as shown below.

 Test the expression at the beginning of the loop

3. Test the expression at the end of the loop

In JavaScript, there is also a do The while statement loop is similar to the while loop. The difference is that it always runs at least once, because it checks the condition at the end of the loop, not at the beginning. For example, the code of the above example can also be written in the following way.

 Test the expression at the end of the loop


Please contact our consultant

+86 10-60259772

Please provide your contact number. The project manager of shangpin China will contact you as soon as possible.