JavaScript02-Operator (operator)? Process control?

JavaScript basics day 02

1-operator (operator)

1.1 Classification of operators Operators, also known as operators, are symbols used to implement functions such as assignment, comparison, and performing arithmetic operations.

Arithmetic operators, increment and decrement operators, comparison operators, logical operators, assignment operators

1.2 Overview of arithmetic operators

Concept: Symbols used in arithmetic operations to perform arithmetic operations on two variables or values.

1

Commonly used operators in JavaScript are:

Arithmetic operators, increment and decrement operators, comparison operators, logical operators, assignment operators

The accuracy of
floating-point numbers The highest accuracy of floating-point numbers is 17 decimal places, but its accuracy is far inferior to integers when performing arithmetic calculations.

So: don't directly judge whether two floating-point numbers are equal!
Expressions and return value expressions: are combinations of numbers, operators, variables, etc. that can be calculated in a meaningful arrangement of values. Simple understanding: numbers, operations The formula expression composed of characters, variables, etc. will eventually have a result, which is returned to the developer, called the return value

1 2

var result = 0.1 + 0.2; // The result is not 0.3, but: 0.30000000000000004 console.log(0.07 * 100); // The result is not 7, but: 7.000000000000001

1.3 Increment and decrement operators Overview of increment and decrement operators

If you need to repeatedly add or subtract 1 to a numeric variable, you can use the increment (++) and decrement (--) operators to complete.

Increment operator

Pre-increment operator
++num Pre-increment is to add 1 to itself, similar to num = num + 1, but ++num is easier to write. Use formula: add first, then return value

The post-increment operator
num++ is post-increment, which is self-increment, similar to num = num + 1, but num++ is simpler to write. Use formula: first return to the original value, then add

1.4 Comparison Operators Overview of Comparison Operators

Concept: A comparison operator (relational operator) is an operator used when comparing two data. After the comparison operation, a Boolean value (true / false) is returned as the result of the comparison operation.

1

2 3

In JavaScript, increment (++) and decrement (--) can be placed before or after the variable. When placed in front of a variable, we can call it a pre-increment (decrement) operator, and when placed behind a variable, we can be called a post-increment (decrement) operator.

Note: The increment and decrement operators must be used with variables.

1 2

var num = 10; alert (++ num + 10); // 21

1 2

var num = 10;
alert (10 + num ++); // 20

Equal sign comparison

 

1 2

console.log(18 == '18'); console.log(18 === '18');

1.5 Overview of logical operators

Concept: Logical operators are operators used to perform Boolean operations, and their return values ​​are also Booleans. It is often used to judge multiple conditions in later development

Logical AND &&
both sides are true to return true, otherwise it returns false

Logical OR ||
Both sides are true before returning true, otherwise it returns false

 

Logical negation!
Logical negation (!) is also called the negation sign, used to take the opposite value of a Boolean value, such as true and false

Short-circuit operation (logical interruption) The principle of short-circuit operation: when there are multiple expressions (values), when the value of the expression on the left can determine the result, the operation of the expression on the right is not continued.

Value;
logical and
grammar: expression 1 && expression 2

console.log( 123 && 456 ); // 456 console.log( 0 && 456 ); // 0 console.log( 123 && 456&& 789 ); // 789

逻辑或
语法: 表达式1 || 表达式2

1 2

var isOk = !true; console.log(isOk); // false

1 2 3

- 如果第一个表达式的值为真,则返回表达式2 - 如果第一个表达式的值为假,则返回表达式1

1 2 3

1 2 3

- 如果第一个表达式的值为真,则返回表达式1 - 如果第一个表达式的值为假,则返回表达式2

 

1 2 3

console.log( 123 || 456 ); // 123 console.log( 0 || 456 ); // 456 console.log( 123 || 456 || 789 ); // 123

1.6 赋值运算符

1

概念:用来把数据赋值给变量的运算符。

1 2 3 4

var age = 10;
age+=5; //相当于age=age+5; age-=5; //相当于age=age-5; age *= 10; // 相当于 age = age * 10;

1.7 运算符优先级

一元运算符里面的逻辑非优先级很高 逻辑与比逻辑或优先级高

2 - 流程控制

2.1 流程控制概念

1

2 3 4 5

在一个程序执行的过程中,各条代码的执行顺序对程序的结果是有直接影响的。很多时候我们要通过控制代码的执行 顺序来实现我们要完成的功能。

简单理解:**流程控制就是来控制代码按照一定结构顺序来执行** 流程控制主要有三种结构,分别是**顺序结构**、**分支结构**和**循环结构**,代表三种代码执行的顺序。

2.2 顺序流程控制 顺序结构是程序中最简单、最基本的流程控制,它没有特定的语法结构,程序会按照代码的先后顺序,依次执行,

程序中大多数的代码都是这样执行的。

2.3 分支流程控制 分支结构

由上到下执行代码的过程中,根据不同的条件,执行不同的路径代码(执行代码多选一的过程),从而得到 不同的结果

1

JS 语言提供了两种分支结构语句:if 语句、switch 语句

if 语句 语法结构

1 语句可以理解为一个行为,循环语句和分支语句就是典型的语句。一个程序由很多个语句组成,一般情况下, 会分割成一个一个的语句。

执行流程

if else语句(双分支语句) 语法结构

1 2 3 4

// 条件成立执行代码,否则什么也不做 if (条件表达式) {

// 条件成立执行的代码语句 }

 

1 2 3 4 5 6 7

// 条件成立 执行 if 里面代码,否则执行else 里面的代码 if (条件表达式) {

// [如果] 条件成立执行的代码 } else {

// [否则] 执行的代码 }

执行流程

if else if 语句(多分支语句) 语法结构

1 2 3 4 5 6 7 8 9

10 11 12

// 适合于检查多重条件。 if (条件表达式1) {

语句1;
} else if (条件表达式2) {

语句2;
} else if (条件表达式3) {

语句3; ....

} else {
// 上述条件都不成立执行此处代码

}

执行逻辑

 
 

2.4 三元表达式 语法结构

执行思路

如果表达式1为 true ,则返回表达式2的值,如果表达式1为 false,则返回表达式3的值 简单理解: 就类似于 if else (双分支) 的简写

2.5 switch分支流程控制 语法结构

switch 语句也是多分支语句,它用于基于不同的条件来执行不同的代码。当要针对变量设置一系列的特定值 的选项时,就可以使用 switch。

1

表达式1 ? 表达式2 : 表达式3;

 

switch( 表达式 ){ case value1:

// 表达式 等于 value1 时要执行的代码

break; case value2:

// 表达式 等于 value2 时要执行的代码

break; default:

// 表达式 不等于任何一个 value 时要执行的代码 }

switch :开关 转换 , case :小例子 选项
关键字 switch 后面括号内可以是表达式或值, 通常是一个变量 关键字 case , 后跟一个选项的表达式或值,后面跟一个冒号 switch 表达式的值会与结构中的 case 的值做比较

如果存在匹配全等(===) ,则与该 case 关联的代码块会被执行,并在遇到 break 时停止,整个 switch 语句代 码执行结束

如果所有的 case 的值都和表达式的值不匹配,则执行 default 里的代码
注意: 执行case 里面的语句时,如果没有break,则继续执行下一个case里面的语句。 switch 语句和 if else if 语句的区别

一般情况下,它们两个语句可以相互替换
switch...case 语句通常处理 case为比较确定值的情况, 而 if...else...语句更加灵活,常用于范围判断(大 于、等于某个范围)
switch 语句进行条件判断后直接执行到程序的条件语句,效率更高。而if...else 语句有几种条件,就得 判断多少次。
当分支比较少时,if... else语句的执行效率比 switch语句高。 当分支比较多时,switch语句的执行效率比较高,而且结构更清晰。

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108203172