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 structure of JavaScript programs?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what the structure of JavaScript programs, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

There are three kinds of program structures in JavaScript, which are: 1, sequential structure; the program is executed line by line from top to bottom by default. 2. Branch structure; selective execution is different according to the result of conditional judgment. 3. Loop structure; the program can execute the same code segment repeatedly and exit when it reaches the critical point.

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

Program structure in JavaScript

It is divided into three main categories:

Order: programs are executed line by line from top to bottom by default

Branch: according to the result of conditional judgment, the selective execution is different.

Loop: the program can execute the same code segment repeatedly and exit when it reaches the critical point

*

Example:

Requirements Analysis:

Var price=prompt ("please input the goods price:"); var accout=prompt ("please input the goods account:"); var money=prompt ("please input the goods money:"); var sum=parseFloat (price) * parseInt (accout); (sum > 500) & & (sum*=0.8); var change=money-sum;alert ("receivable" + sum+ ", change" + change); branch structure

1.if structure:

Syntax:

If (condition) {snippet of code executed only if the condition is met}

2.if else structure:

Syntax:

If (condition) {Code snippet that meets the condition} else {condition not satisfied}

3.else if structure

Syntax:

If (condition 1) {Code snippet that meets condition 1} else if (condition 2) {condition 2 satisfies} else if (condition 3) {condition 3 satisfies} else {all previous conditions are not satisfied}

4. Branched vs trinocular / short circuit

If it's just a return value-- > three eyes / short circuit

If the operation is complex-> branch structure

Short circuit logic:

Condition & operation: one thing, do as soon as you meet it, otherwise don't do it, only if the operation is simple.

Value 1 | | value 2: if the value 1 is valid, the value 1 is returned, otherwise the value 2 is returned.

Trinocular operation:

Trinocular operation: multiple values, judge according to conditions, choose more than one

Conditional expression? Expression 1: expression 2

If the result of the conditional expression is true (true), the code in expression 1 is executed, otherwise the code in expression 2 is executed.

5.switch structure

Syntax:

Switch (expression) {case value 1: code 1: case value 2: code 2: case value 3: code 3; "`default: default code snippet;}

Break: stop the execution of the current structure and jump out of the current structure

Continue: ending the current cycle and continuing the next cycle / / control can generally be replaced by negative conditions.

Switch: switch case is preferred when congruent comparison is the condition

Else if: in addition to congruent comparison, when you want to define conditions flexibly

Cyclic structure

Loop structure: let the program execute a piece of code repeatedly and stop the loop only when the critical condition is reached

3 elements:

1. Cycle condition: the condition to continue the cycle

two。 Loop variable: in a loop condition, a variable used as a comparison

/ / from what number to start, how much to increase or decrease at a time, to what end

3. Loop body: code snippets that are executed repeatedly

While cycle

Conditions of use: when the change law of cyclic variables is uncertain

Grammar

While (conditional) {cyclic body; iterative cyclic variable;}

Example:

Guessing game: the computer randomly generates a number of 0-100, the player guesses the size of the number, and gives the hint of guessing big and small, until the final guess is right!

Var n=parseInt (Math.random () * (100,0,1) + 0); / generate a random number var input= ""; while (inputcounting random numbers = "exit") {input=prompt ("you guess"); alert (input > n? "bigger": input

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