JS02 process control

Keywords: Javascript

6. Operator

Expressions and return values
Expression: it is an expression composed of numbers, operators, variables, etc
Expressions have a result that is returned to us, that is, the return value

1. Arithmetic operator: + - * /%

1. There is a precision problem in the operation of floating-point numbers. Do not directly judge whether two floating-point numbers are equal
2. A number can be divided by whole, using% (remainder) as 0

2. Pre auto increment and post auto increment (to be used with variables)

1. Pre auto increment, self increment first, and then operation + + a
2. Post auto increment: return the original value first, and then add a++
3. When the pre and post are used separately, the execution result is the same, that is, a=a+1
However, there are differences when participating in the operation:
var a=10;
var b=a++ +10 //a++ =10 b=20 a=11
var b=++a +10 //++a =11 b=21 a=11

3. Comparison operator (relational operator) returns a Boolean value

< > >= <= == != === !===

1. ==During comparison, judge whether the values on both sides are equal
You can convert from string type to number type
For example: 10 = = '10' returns true
2. ===During comparison, judge whether the values and data types on both sides are equal and cannot be transformed
For example: 10 = = '10' returns false

4. Logical operators

1. Logic and && total truth are true
Logic or all false is false
Logical non!
2. Short circuit operation (logic interrupt)
When there are multiple expressions, the expression value on the left can be determined to determine the result, and the operation on the right will not be continued
1. Expression 1 & & expression 2
If the value of expression 1 is true, expression 2 is returned
If the value of expression 1 is false, expression 1 is returned
2. Expression 1 | expression 2
If the value of expression 1 is true, expression 1 is returned
If the value of expression 1 is false, expression 2 is returned
example:
At this time, the expressions on both sides of the logical operator are pure numbers, which must be true
If the expression is null or false when negative, 0 "" is null undefined Nan
The rest is true

``` <script>
console.log(123&&456);//456
console.log(123||456); //123
console.log(''&&11); // ''
console.log(0&&11); // 0
console.log(NaN&&11); //NaN
console.log(null&&11); //null
console.log(0 ||11); //11
</script>
```

5. Assignment operator:

=Direct assignment from right to left
+=- = (also multiplication and division) a = a + 2 A + = 2

6. Operation priority: (from high to low)

parentheses
Unary operator: + + --!
Arithmetic operator: multiply and divide% before +-
Relational operator: > > = <<=
Equality operator: = =! = = =======
Logical operators: & & before||
Assignment operator:=

7. Process control

1. Branch structure

1. if statement

Examples of judging leap years
Requirement: receive the year entered by the user. If it is a leap year, the leap year will pop up, otherwise the normal year will pop up
analysis:
Leap years are those that can be divided by 4 and cannot be divided by 100, or run years are those that can be divided by 400
1. The prompt input box pops up, allowing the user to enter the year and save the value to the variable
2. Use if statement to judge

```<script>
var year=prompt('Please enter the year:');
if(year%4==0 && year%100!=0||year%400==0){
alert('The year entered is a leap year');
}
else{
alert('The year entered is a normal year');
}
</script>
```

2. If else if statement, which is used to select one more

For example, judge the level of achievement:
It is required to receive the score entered by the user and output the corresponding grade letters a, B, C, D and e according to the score
Of which:
More than 1.90 minutes (inclusive), output A
2. 80 min (included) ~ 90 min (excluded), output B
3.70 (included) ~ 80 (excluded), output C
4.60 (included) ~ 70 (not included), output D
Below 5.60 minutes (excluding), output E

```<script>
if(fen>=90){
}Here it is judged from large to small
else if(fen>=80){
}
else if(fen>=70){
}
else if(fen>=60){
}
else{
}
</script>
```

3. The formula composed of ternary operators is called ternary expression:

Conditional expression? Expression 1: expression 2
If the conditional expression is true, the expression 1 is returned
If the conditional expression is false, the expression 2 is returned
Expressions have return values
var n=10;
var result=n>3 ? "Yes": "no";

```  <script>
// Digit complement 0: if the number entered by the user is less than 10, complement 0 in front of it; otherwise, it is not necessary to complement 0
var num = prompt('Please enter a number from 0 to 30:');
var result = num <10 ? '0'+num :num;
</script>
```

4. switch statement, multi branch statement, realizing one out of many

1. Syntax:
Switch (expression){
case value1:
Statement 1;
break;
case value2:
Statement 2;
break;
default:
Execute the last statement;
}
Match the value of the expression with the option value after the case (the two values are equal). If they match, execute; if they don't match, execute
default:
Execute the last statement;
2. Usually, the expression after switch is written as a variable;
3. The expression is written as a variable. The value of the variable matches the value in the case. It is required to be congruent, that is, the value and data type should be consistent
4. If there is no break after the current case, you will not exit the switch and continue to execute the next case

2. Circulation structure

1. For loop

1. The for loop can repeat some of the same code
2. The for loop can repeatedly execute a little different code, using counters
```<script>
for(var i=1;i<=10;i++){
if(i==1){
console.log('You are one year old');
}
else{
console.log('You have'+ i +'Years old');
}

}
</script>
```
1. The for loop can repeat certain operations, such as doing some arithmetic operations
Summation:
```<script>
var s=0;
for(var i=1;i<=10;i++){
s+=i;
}
console.log(s);
</script>
```

requirement:
The user inputs the number of classes, then successively inputs the scores of each student, and finally prints out the total score and average score of the class
analysis:

1. Pop up the input box and enter the total number of people num
2. Enter the scores of each student in turn. The number of times the input box pops up is equal to the total number of people. Use the for loop, I < = num
3. Processing data
4. Pop up results
```<script>
var num=prompt('Please enter the total class size:');
var sum=0;
var average=0;
for(var i=1;i<=num;i++){
var score=prompt('Please enter page'+ i +'Student grades');
sum=sum+parseFloat(score);
// Because the data returned by prompt() is a string,
// To convert to numeric type, it can be used with the plus sign for addition
}
average = sum/num;
</script>
```
4. Double for loop
```<script>
// When you want to print a planet number: use string splicing
var num=prompt('Please enter the number of asterisks:');
var str='';
for(var i=1;i<=num;i++){
str+='*';
}
console.log(str);
</script>
```

When rows and columns are involved, a double for loop is used, which is loop nesting
The outer layer circulates once, and the inner layer circulates all

1. Requirements: the user inputs the number of rows and columns, and prints the asterisk
```<script>
// When you want to print n rows and N columns of asterisks: String splicing is used
var num1=prompt('Please enter the number of rows:');
var num2=prompt('Please enter the number of columns:');
var str='';
for(var i=1;i<=num1;i++){
for(var j=1;j<=num2;j++){
str+='*';
}
str+='\n';
}
console.log(str);
</script>
```
1. Requirements: print out 10 lines of inverted triangular asterisks
```<script>
var str='';
for(var i=1;i<=10;i++){//Outer control rows
for(var j=i;j<=10;j++){//The number of prints on the inner layer is different. The key is that the initial number of prints is j = I
str+='*';
}
str+='\n';
}
console.log(str);
</script>
```
1. Print 99 multiplication table
``` <script>
//  analysis:
//  1. There are 9 lines in total, and the number of each line is different. Double for loop is used
//  2. Number of outer control lines i, 9 cycles
//  3. The inner layer controls the formula of each line
//  4. Key: the number of formulas in each row is consistent with the number of rows, that is, J < = I
//  5. There should be a newline escape character '\ n', and it is found that each formula is listed in the front, and there is air attack between adjacent formulas, so use '\ t'
var str='';
for(var i=1;i<=9;i++){
for(var j=1;j<=i;j++){
str+=j + 'x' + i +'='+ i*j +'\t';
}
str+='\n';
}
console.log(str);
</script>
```

Posted by phencesgirl on Sat, 30 Oct 2021 10:29:02 -0700