JavaScript-----if/if-else/if else if, switch, ternary expression

1. If statement
1. If statement is a branch statement, which is mainly used to judge the
grammar:
if (expression) { code block }

Execution process: first judge whether the result of the expression is true or false, if it is true, execute the code block, if it is false, do not execute

        var num1 = 10;
        var num2 = 20;
        // 当num1小于num2为true时,大括号内的代码执行,输出10
        if(num1 < num2){
            console.log(num1);
        }
        // 当num1大于num2为false时,大括号内的代码不执行,什么也不输出
        if(num1 > num2){
            console.log(num1);
        }

2. If-else statement
1. If-else statement has two branches, only one branch is executed.
Syntax:

if(表达式){
            代码1
        }else{
            代码2
        }

Execution process: If the result of the expression is true, execute code 1, if it is false, execute code 2.
Example:
Enter a number at random (parseInt is to make the entered number an integer), and determine whether the number is greater than 18, yes If true, execute the console.log of code 1 ("this number is greater than 18"); in this part, if false, execute another one.

       var num1 = parseInt(prompt("请输入一个整数"));//自己随便输入一个整数
        if (num1 >18){
            console.log("这个数字大于18");
        }else{
            console.log("这个数字小于等于18");
        }

三、if-else if-else

if(条件1){
          条件1为true时执行的代码块
      }else if(条件2){
          条件 1 为 false 而条件 2 为 true 时执行的代码块
      }else{
           条件1和条件2同时为 false 时执行的代码块
      }

Four, switch
switch branch statement
is a multi-branch statement, you can choose one more
execution idea:
According to the value of the conditional expression and the value value match, which value matches the execution statement corresponding to which value is executed, if there is no match, Just execute the statement following default

switch(表达式){
          case 1:代码块
          break;
          case 2:代码块
          break;
          .
          .
          .
          case n:代码块
          break;
          default: 代码块
      }

V. Ternary expressions
Syntax:
var variable = expression 1? Expression 2: expression 3;
execution process:
if expression 1 is true, execute expression 2, and then give the result to the variable;
if expression 1 is false , Execute expression 3, and then give the result to the variable;

The difference between switch statement and if else if statement
1. In general, two statements can be replaced with each other.
2. The switch...case statement is usually used to deal with the case where the case is a relatively definite value, while the if...else if...else statement is more flexible. Commonly used for range judgment (greater than or equal to a certain range)
3. The switch statement executes the conditional statement directly to the program after the conditional judgment, which is efficient, and if...else if...else statement has several conditions, you have to judge how many times
4 When there are fewer branches, it is more efficient to use if else.
5. When there are more branches, it is more efficient to use switch and the structure is clearer.

Guess you like

Origin blog.csdn.net/weixin_44401120/article/details/93376908