Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of control statement in javascript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "what is the concept of control statement in javascript", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the concept of control statement in javascript" can help you solve your doubts.

In javascript, the control statement is a statement structure used to control the execution order of each statement in the program, which can control the selection, loop, turn and return of the program flow. Control statements can be divided into three categories: sequential structure, selection structure and loop structure.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

What is a control statement?

The control statement is a statement structure used to control the execution order of each statement in the program, which can control the selection, loop, turn and return of the program flow.

Function: used to control the flow of the program, in order to achieve the various structural ways of the program.

Control statements can be divided into three categories:

The first category is sequential structure: from top to bottom, from left to right

The second category is selection structure: branch selection.

The third category is the loop structure: repeated execution.

1. If control statement

1. If... Else:

If (expression) {statement 1 * *} else {statement 2 * *} / / function description: / / execute statement 1 if the value of the expression is true / otherwise execute statement 2

2. If can be used alone:

Var x = (new Date ()). GetDay (); / / get the value of the week today, 0 is Sunday var ytterif ((x weekend 6) | | (x weekend 0)) {y = "weekend";} else {y = "working day";} alert (y); / / equivalent to: y = "working day"; if ((x weekend 6) | | (x weekend 0)) {y = "weekend"};

3. If nesting:

If (expression 1) {statement 1;} else if (expression 2) {statement 2;} else if (expression 3) {statement 3;} else {statement 4;} / functional description: / / execute statement 1 if the value of expression 1 is true / execute statement 2 if the value of expression 2 is true. } else if (x = Tuesday) {y = "Tuesday";...} else if (x = Saturday) {y = "Saturday";} else if (x = Sunday) {y = "Sunday";} else {y = "undefined";}

2. Switch selection control statement

1. Basic format:

Switch (expression) {case value 1: statement 1: statement 1; case value 2: statement 2; case value 3: statement 3; default: statement 4; if nothing else holds, execute} / / function description: / / if the value of the expression is 1 and 2. Then execute the statement 1 move 2. Then exit / / if it is not any of the values listed, the statement after the execution of default (statement 4) / / without adding break will continue to execute the statement of the next condition / / default at the end, or add break, otherwise execute the next statement var x=3switch (x) {case 1moy = "Monday"; break;case 2moy = "Tuesday"; break;case 3moy = "Wednesday"; break;case 4moy = "Thursday"; break;case 5moy = "Friday" Break;case 6 alert y = "Saturday"; break;case 7 break;default y = "Sunday"; break;default: y = "undefined";} alert (y) / / case 3 will continue with yearly 'Thursday' without adding break.

2. Switch has a more concise and clear structure than if, which makes the program more readable and efficient.

/ * if statement has a wide range of application. As long as boolean expressions can be judged by if, switch can only compare basic types numerically. The comparability between the two is limited to the comparison of two basic types. / * when it comes to numerical comparison of basic types, of course there are two numbers, and then the point comes: * / each sentence of the if statement is independent. Look at the following statement: if (a = = 1)... else if (a = = 2). / * so that an is read into the register twice: 1 and 2 are read into the register twice, actually, it is a bit redundant to read into the register only once before all the comparisons are done, and the rest is extra overhead, but the if statement must read the two numbers out of memory into the register each time. It doesn't know that it is actually comparing the same switch case switch case / so it comes out. Change the above to the switch case version: switch (a) {case 0: break Case 1:} / Summary: 1.switch is used to multibranch according to an integer value, and the compiler can optimize the multiplex branch 2.switch-case evaluates the expression only once, then compares the value of the expression with the value of each case, and then chooses which case statement block 3.if to execute has a wide range of conditions, and each statement is basically independent. Conditional loading is required for each judgment, so it is more efficient to use switch structure than if structure in multi-branch.

Third, while loop control statement

While (condition) {statement 1 * *} / / function description: / / the function is similar to for. When the condition is set up, execute the statement in the loop {}, otherwise jump out of the loop var iTunes 1 * while (I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report