MENU service case
 Website construction website design Beijing website construction high-end website production company Shangpin China
We create by embracing change
360 ° brand value__
simplified Chinese character
Simplified Chinese English

Shangpin China Joins Hands with Beisheng Internet to Create a New Chapter in Website Construction

Type: Shangpin Dynamic Learn more

Statement structure of Javascript

Source: Shangpin China | Type: website encyclopedia | Time: July 26, 2015
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
Source Statement: This article is original or edited by Shangpin China's editors. If it needs to be reproduced, please indicate that it is from Shangpin China. The above contents (including pictures and words) are from the Internet. If there is any infringement, please contact us in time (010-60259772).
TAG label:

What if your website can increase the number of conversions and improve customer satisfaction?

Make an appointment with a professional consultant to communicate!

* Shangpin professional consultant will contact you as soon as possible

Disclaimer

Thank you very much for visiting our website. Please read all the terms of this statement carefully before you use this website.

1. Part of the content of this site comes from the network, and the copyright of some articles and pictures involved belongs to the original author. The reprint of this site is for everyone to learn and exchange, and should not be used for any commercial activities.

2. This website does not assume any form of loss or injury caused by users to themselves and others due to the use of these resources.

3. For issues not covered in this statement, please refer to relevant national laws and regulations. In case of conflict between this statement and national laws and regulations, the national laws and regulations shall prevail.

4. If it infringes your legitimate rights and interests, please contact us in time, and we will delete the relevant content at the first time!

Contact: 010-60259772
E-mail: [email protected]

Communicate with professional consultants now!

  • National Service Hotline

    400-700-4979

  • Beijing Service Hotline

    010-60259772

Please be assured to fill in the information protection
Online consultation

Disclaimer

Thank you very much for visiting our website. Please read all the terms of this statement carefully before you use this website.

1. Part of the content of this site comes from the network, and the copyright of some articles and pictures involved belongs to the original author. The reprint of this site is for everyone to learn and exchange, and should not be used for any commercial activities.

2. This website does not assume any form of loss or injury caused by users to themselves and others due to the use of these resources.

3. For issues not covered in this statement, please refer to relevant national laws and regulations. In case of conflict between this statement and national laws and regulations, the national laws and regulations shall prevail.

4. If it infringes your legitimate rights and interests, please contact us in time, and we will delete the relevant content at the first time!

Contact: 010-60259772
E-mail: [email protected]