The third day of learning JS

1, Logical branch (Continued)

1. Ternary operator: conditional operator

  a>b? c:d; Expression 1? Expression 2: expression 3;   

The execution result of expression 1 determines whether expression 2 or expression 3 is executed

The result of expression 1 is true. Execute expression 2 and finally return the result of expression 2

The result of expression 1 is false. Execute expression 3 and finally return the result of expression 3

 

  2,Switch/case

Syntax:

Switch (expression){

case expression 1: branch statement 1;

                        break;

case expression 2: branch statement 2; break;

case expression 3: branch statement 3; break;

                ...

case expression n: branch statement n;break;

Default: default branch statement; break;

        }

Specification:

  1. All parentheses are entered in pairs

  2. All double quotes are entered in pairs

  3. One sentence on one line (no more sentences on one line)

  4. Curly braces stand alone on one line

  5. Indent alignment:

a) peer alignment

b) the sub level is indented by 4 spaces compared with the previous level

 

Note:

  1. The () after if cannot be omitted, and do not add a semicolon after ()

  2. Even one sentence in the if branch should write {} (3 yuan for one line of code)

  3.if nesting should not exceed five layers (preferably no more than three layers)

  4. The default branch must be written in the switch statement to prevent ambiguity in future maintenance (default: equivalent to the last else of if else)

Put the higher probability of execution (normal situation) first (if, case)

  5. Who do if else and switch use?

Use switch to judge the equivalent range;

A simple grade division

 1  var score =100;
 2 
 3   switch(parseInt(score/10)){
 4 
 5    case 10:
 6 
 7    case 9:
 8 
 9     console.log("A");
10 
11     break;
12 
13    case 8:
14 
15     console.log("B");
16 
17     break;
18 
19    case 7:
20 
21     console.log("C");
22 
23     break;
24 
25    case 6:
26 
27     console.log("D");
28     break;
29 
30    default :console.log("E");
31     }

II. Circulation

1,while /do while

 

/*
   while Conditional expression ()
          Circulatory body;
*/
    1,A conditional expression is a boolean Value, is true Then execute the loop body, which is false Jump out and modify only one statement;
    2,Execution order: first condition, then statement;
    3,A finite cycle must have a condition that makes the cycle close to the end,
    4,Is the dead cycle good or bad?? Depending on the situation (when the password and account number are entered incorrectly)

while exercise:

//Sum of numbers less than 100

1    var sum = 0;
2     var i = 1;
3 
4     while (i <= 100) {
5         sum += i;
6         i++; 
7     }
8     
9     console.log(i,sum);

//The entry salary is 10K, with an annual increase of 5%. What's the salary after 50 years?

1 var salary = 10000;
2     var i = 1;
3     
4     while(i<=50){
5         salary *= 1+0.05;
6         i++;
7     }
8     
9     console.log(salary);

//Print multiples of 7 within 100

1 var i = 1;
2     
3     while(i<=100){
4         if(i%7==0){
5             console.log(i);
6         }
7         i++;
8     }
9     

//Print odd numbers within 100

1 var i = 1;
2     
3     while(i <= 100){
4         if(i%2){ //1 == true
5             console.log(i);
6         }
7         i++;
8     }

2,do while

* * * structure * * *

  do{
Circulatory body;
} while (conditional expression);

Differences between while and do while

The only difference between while and do while
Execution order is different
  do...while statement before condition
while precondition statement

  do... Similarities and differences between while and while
When the first condition is established, there is no difference between the two
When the first condition does not hold, do While executes the loop body once, and while jumps out directly

3,for

 

For (expression 1; expression 2; expression 3) {1243243243
Expression 4
  }

  for(;;) {} must not be omitted. In addition, expression 1234 can be omitted
Expression 1: assign initial value to cyclic variable
Expression 2: condition
Expression 3: condition that makes the loop close to the end
Expression 4: loop body statement

1 var sum = 0;
2     
3     for(var i=1; i <= 100;i++){
4         sum += i;
5     }
6     
7     console.log(i, sum);

 

Posted by golfromeo on Sun, 15 May 2022 05:46:40 +0300