JavaScript branch structure statement

Keywords: less Javascript TypeScript

Branch structure statement in JS

JS code should be executed from top to bottom

Branch structure statement: when a part is executed, select whether to execute a section of code according to the condition

Conditional branch statement in JS - if

There are four ways to write

1, if branch
1. if statement

if (condition) {code} - single branch

Syntax: if (condition) {code}

Execute the code in {} as long as the condition is satisfied, and execute the code in {} as long as the condition is not satisfied

Satisfied: the condition is true

Not satisfied: the condition is false

	var age = 16
    // Because the result of age > = 18 is false
    // So the code in {} will not execute
    if (age >= 18) {
      console.log('Go online in the Internet bar')
    }
2,if … else statement

If (condition) {execute if condition is established} else {execute if condition is not established} - double branch

Syntax: if (condition) {execute if condition is established} else {execute if condition is not established}

If the condition is satisfied, execute the code in {} after if. If the condition is not satisfied, execute the code in {} after else

Two conditions are bound to carry out one

	var num = 203
    if (10 < num && num < 100) {
      console.log(num + ' It's at 10 ~ 100 between')
    } else {
      console.log(num + ' Not at 10 ~ 100 between')
    }
	var age = 16
    if (age >= 18) {
      console.log('Go to the Internet bar')
    } else {
      console.log('Online at home')
    }
	 /*
    	leap year:
        The number of years in the Gregorian calendar is a multiple of 4, not a multiple of 100, which is a common leap year
        That is: year% 4 = = = 0 & & year / 100! = = 0

        The number of years in the Gregorian calendar is a multiple of 100 and 400, which is a leap year of the century
        That is: year% 400 = = = 0
    */
	var year = prompt('Please enter the year')

    if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
      alert(year + ' It's a leap year')
    } else {
      alert(year + ' It's the year of the year')
    }
Extension: ternary operator

For if The abbreviation of else statement, only if Else statement

Syntax:

Condition judgment? Execute when the condition is established: execute when the condition is not established

Strict Grammar: two symbols, divided into three sections

The sign rules of ternary operators are? And:

	var age = 15
    age >= 18 ? console.log('adult') : console.log('under age')	// under age
3,if … else if statement

If (condition 1) {if condition 1 is satisfied, execute} else if (condition 2) {if condition 2 is satisfied, execute} - multi branch

Syntax: if (condition 1) {if condition 1 is satisfied, execute} else if (condition 2) {if condition 2 is satisfied, execute}

From the first condition, if any condition is satisfied, execute the code in {} after the condition

If the previous conditions are met, all the later conditions will be ignored. Skip it directly

	var age = 2
    if (age >= 23) {
      console.log('I can get married')
    } else if (age >= 14) {
      console.log('I can fall in love')
    } else if (age >= 6) {
      console.log('Can sit at the same table with female students')
    }
4,if … else if … Else statement

If (condition) {execute on condition 2} else if (condition 2) {execute on condition 2} Else {} - multi branch

Syntax: if (condition) {execute when established} else if (condition 2) {execute when condition 2 is established} else {}

From the first condition, if any condition is satisfied, execute the code in {} after the condition

When all the conditions are not satisfied, execute the code in {} after else

If the previous conditions are met, all the later conditions will be ignored. Skip directly

	var age = 5
    if (age >= 23) {
      console.log('I can get married')
    } else if (age >= 14) {
      console.log('I can fall in love')
    } else if (age >= 6) {
      console.log('Can sit at the same table with female students')
    } else {
      console.log('I can only play with my aunt at home')
    }
5. Expansion: a small interaction with the page

Syntax: prompt('text content of prompt ')

Function: pop up an input box in the browser, and write a prompt text content in parentheses

Return value: the content entered by the user in the text box

Note: the content obtained from the page is in a string format

If you want to add, remember to change the value type first

	// prompt will pop up an input box for the user to input
    // What result receives is what the user enters
    var result = prompt('Please enter your bank deposit')

    // Judge if your bank deposit is more than 100000, we can talk for a while
    // Judge if your bank deposit is less than 100000, then I'm sorry
    if (result >= 100000) {
      alert('hello nice to meet you!')
    } else {
      alert('I have something else to do, Let's go, sorry')
    }

vscode

1, vscode plug in

1. chinese plug in

2. Color difference in Bracket Pair Colorizer brackets

3. comment translate translation plug-in

4. one dark pro theme plug-in

5. open in browser

6. Vscode Icon Editor Icon plug in

2, Shortcut key

Select the same content ctrl + d

Move up the whole line alt + up arrow

Move down one line as a whole alt + down arrow

3, Code snippet

Lower left gear - > user code snippet - > new global code snippet - > write a name by yourself - > Enter

	"Print to console": {
    	"scope": "javascript,typescript", // Indicates that it takes effect in js file and ts file
        "prefix": "abc", // Your shortcut text
        "body": [ // That's what happens when you press the tab key
        	"console.log('$1');",
            "$2"
        ],
        "description": "Log output to console"
    }

Conditional branch statement in JS - switch

The switch statement is also one of our conditional branch statements

switch Statements
1, Syntax:

switch (variable to judge){

case 1:

Code executed when case 1 is satisfied

​ break

case 2:

Code executed when case 2 is satisfied

​ break

​ default :

Code executed when all conditions are not satisfied

​ }

	// Prepare a variable
    var num = 1
    // switch syntax
    switch (num) {
      case 1:
        console.log('You entered the number 1')
        break
      case 2:
        console.log('You entered the number 2')
        break
      case 3:
        console.log('You entered the number three')
        break
      default:
        console.log('All conditions are not met')
    }
    console.log('Follow up code continues')
2, Explain words

Switch switch

case scenario

Default default

Break break

3, Attention

1. It is OK to write the variables to be judged directly in parentheses, without symbols

2. The case after case can't write the comparison of range, only the full (=====)

3. The break after each case should be written. If it is not written, a penetration will occur

Penetration: no matter whether the next case is satisfied or not, the code of the next case will be executed

4. The effect of downward penetration will start from the first satisfied case and continue until a break is encountered

5. default can be written or not

If default is written, the code will execute if all conditions are not met

If you don't write default, no code will execute if all conditions are not met

4, The difference between if statement and switch statement

1. if condition judgment can judge the scope

if statement is recommended when you need unit judgment

2. switch can only judge accurately

When you need to judge accurately and judge more, it is recommended to use the switch statement

5, Small case
	/*
      According to a number from 1 to 12, give feedback on the number of days in the month
        Determine whether February is 28 days or 29 days by a year's variable
    */

    // 1. Prepare a number
    var month = prompt('Please enter a 1 ~ 12 The number between represents the month')
    var year = prompt('Please enter a four digit number for the year') - 0

    // Mathematical operations without addition can be converted into numbers
    // Minus 0 does not change the original size
    month = month - 0

    // Write with switch penetration
    switch (month) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        console.log(month + ' There are 31 days in the month')
        break
      case 4:
      case 6:
      case 9:
      case 11:
        console.log(month + ' There are 30 days in the month')
        break
      case 2:
       // Judging whether it is a normal year or a leap year by year
        if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
          console.log(month + ' There are 29 days in the month')
        } else {
          console.log(month + ' There are 28 days in the month')
        }
        break
      default:
        console.log('Please enter a 1 ~ 12 Number between')
    }

Posted by alecapone on Wed, 24 Jun 2020 00:55:41 -0700