当前位置:网站首页>JS process control
JS process control
2022-07-25 11:47:00 【c_ perfectworld】
Process control
During program execution , various Code execution order Yes The result of the procedure It's influential , Many times we have to Control the execution order of the code Come on Realization What we want function
Simple understanding : Process control controls the structural order in which the code is executed
Process control has three structures , Sequential structure , Branching structure , Loop structure , The three structures represent three code execution sequences
Sequential structure
According to the code Order , Execute sequentially
Branching structure
During execution from top to bottom , according to Different conditions , perform Different path codes , obtain Different results
- if sentence
- switch sentence
if ( Conditional expression ) {
// Execute statement
}If if The result of the conditional expression is true , Execute... In braces , If if The conditional expression is false , Do not execute the , perform if Code after statement
if else sentence

if The words in it 1 and else The words in it 2 Finally, only one statement can execute
else Followed directly by braces
if else if ( Multi branch statement )
if ( Conditional expression 1) {
Execute statement 1
} else if ( Conditional expression 2) {
Execute statement 2
} else if ( Conditional expression 3) {
Execute statement 3
} else {
Last statement
}
Ternary expression
Yes Ternary operator The formula is a ternary expression
Conditional expression ? expression 1 : expression 2Execution ideas : If the conditional expression is true , Then return the expression 1 Value , If the conditional expression is false , Then return the expression 2 Value
switch sentence
It is also a multi branch statement , Used to execute different code based on different conditions , When you want to set a series of specific value options for variables , Use switch
switch ( expression ) {
case value1:
Execute statement 1;
break;
case value2:
Execute statement 2;
break;
...
default:
Last statement ;
}Execution ideas : Use the value of the expression and case The following option values match , If match up , It's time to do it case The words in it , If none of them match , execute default The words in it
Be careful :
- In development , Expressions are often written as variables
- num The value of and case When matching inside, it is congruent , The value must be consistent with the data type
- If at present case There's no break, Will not quit switch, Is to continue executing the code , Whether it can match or not
switch Statement and if else if The difference between
- General situation , Can replace each other
- switch case Usually dealing with case Compare the situation of certain value ,if else if More flexible , It is often used to judge the range
- switch Statement to directly execute the conditional statement of the program after judgment , More efficient ,if else There are several conditions , You have to judge how many times
- When there are fewer branches if else More efficient
- When there are many branches ,switch More efficient
边栏推荐
猜你喜欢

The first C language program (starting from Hello World)

JDBC summary

Chapter 4 linear equations

如何判断静态代码质量分析工具的性能?这五大因素必须考虑

矩阵的特征值和特征向量

Make a reliable delay queue with redis

Learn NLP with Transformer (Chapter 1)

W5500上传温湿度到oneNET平台

Leetcode sword finger offer 27. image of binary tree

Linked list related (design linked list and ring linked list)
随机推荐
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
Shell - Chapter 8 exercise
Reptile foundation I
谣言检测文献阅读十一—Preventing rumor spread with deep learning
varest蓝图设置json
Leetcode sword finger offer 28. symmetric binary tree
教你如何通过MCU将S2E配置为UDP的工作模式
Maskgae: masked graph modeling meets graph autoencoders
JS中的函数
Signal and slot mechanism ==pyqt5
Hardware peripherals =maixpy3
绘图==PYQT5
W5500 upload temperature and humidity to onenet platform
基于MATLAB的常见线性调制方法
SQL注入 Less23(过滤注释符)
JDBC summary
Various controls ==pyqt5
The first C language program (starting from Hello World)
基于W5500实现的考勤系统
如何判断静态代码质量分析工具的性能?这五大因素必须考虑