JS basic knowledge (operator, process control, loop)

Operator

Classification of operators

Operator (operator), also known as operator, is a symbol used to implement assignment, comparison, and perform arithmetic operations

Arithmetic Operator

Concept : a symbol used in arithmetic operations, used to perform arithmetic operations on two variables or values.
Expression: a combination
expression obtained by a meaningful arrangement method of numbers, operators, variables, etc. that can obtain values, eventually there will be one The result, returned to the developer, is called the return value

Increment and decrement operators

If you need to add or subtract 1 to a numeric variable repeatedly, you can use the increment (++) and decrement (-) operators to complete the
pre-increment operator
++num Pre-increment, which is self-increment by 1, similar to num = num + 1, but ++num is easier to write

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

The post-increment operator
num++ post-increment, that is, self-increase by 1, similar to num = num + 1, but num++ is easier to write

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

Comparison operator

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

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 && returns true if both sides are 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 symbol, which is used to take the opposite value of a Boolean value, such as true and false

Assignment operator

Concept : operators used to assign data to variables

  • = Direct assignment
  • +=, -= add, subtract a number and assign
  • *=, /=, %= multiply, divide, take modulo and assign

Process control

Insert picture description here

Sequence flow control

Sequence structure is the simplest and most basic flow control in the program. It has no specific grammatical structure. The program will be executed in sequence according to the sequence of the code. Most of the code in the program is executed like this

Branch flow control

if statement

grammar structure

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

if else statement (double branch statement)

grammar structure

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

if else if statement (multi-branch statement)

grammar structure

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

cycle

for loop

grammar structure

for(初始化变量; 条件表达式; 操作表达式 ){
  //循环体
}

Double for loop

SUMMARY
loop nest means further define a loop in a loop of the syntax structure
grammar

for (外循环的初始; 外循环的条件; 外循环的操作表达式) {
  for (内循环的初始; 内循环的条件; 内循环的操作表达式) { 
   需执行的代码;
 }
}

while loop

grammar

while (条件表达式) {
  // 循环体代码
}

When using a while loop, we must pay attention to it, it must have an exit condition, otherwise it will become an endless loop

do-while loop

grammar

do {
  // 循环体代码 - 条件表达式为 true 时重复执行循环体代码
} while(条件表达式);

Guess you like

Origin blog.csdn.net/chuenst/article/details/108699515