当前位置:网站首页>JS learning notes - process control
JS learning notes - process control
2022-07-02 16:01:00 【Dumb monkey】
Process control is to control the structure and order of our code
There are three main structures of process control , Namely Sequential structure 、 Branch and loop structures , These three structures represent the order in which the three codes are executed .
Sequential structure
The sequential structure is in the program The most simple 、 The most basic Process control , It has no specific grammatical structure , Program meeting In the order of the code , Execute sequentially , Most of the code in the program is executed in this way .
Branching structure
In the process of executing code from top to bottom , According to different conditions , Execute different path codes ( Execute code multiple selection - The process of ) , So we can get different results
Branch process control if sentence
if sentence ( Single branch )
// If the condition holds, execute the code , Otherwise, do nothing
if ( Conditional expression ) {
// The code statement executed when the condition is true
}
if else sentence ( Two branch statements )
// Implementation of conditions if Code inside , Otherwise execution else Code inside
if ( Conditional expression ) {
// [ If ] Conditional execution code
} else {
// [ otherwise ] Executed code
}
if else if sentence ( Multi branch statement )
// Suitable for checking multiple conditions .
if ( Conditional expression 1) {
sentence 1 ;
} else if ( Conditional expression 2) {
sentence 2 ;
} else if ( Conditional expression 3) {
sentence 3 ;
} else {
// Above conditions It is not established to execute the code here
}
Ternary expression
Ternary expressions can also make some simple conditional choices . A formula consisting of ternary operators is called a ternary expression
Conditional expression ? expression 1 : expression 2
If the conditional expression turns out to be true , Then return the expression 1 Value . If the conditional expression turns out to be false , Then return the expression 2 Value .
Branch process control switch sentence
switch Statement is also a multi branch statement , It is used to execute different code based on different conditions . When you want to set - Series of specific values when , You can use switch.
switch( expression ) {
case value1 : Execute statement 1; break ;
case value2 : Execute statement 2; break ;
default : Execute the last statement ;
}
Using the value of our 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 , Then perform default The words in it .
The value of the expression and case When the values inside match, they are congruent .
switch Statement and if else if The difference between sentences
① In general , They can be replaced by each other .
②swith...case Statements usually deal with case In order to compare the determined values , and f..else.. More flexible sentences , It is often used to judge the range ( Greater than 、 Equal to a certain range )
③switch A conditional statement that executes directly into a program after conditional judgment , More efficient . and ..else There are several conditions for a statement , You have to judge how many times .
Loop structure
stay Js in , There are three main types of circular queries : for loop while loop do..while loop .
In the program , A set of repeatedly executed statements is called The loop body , Can you continue to repeat , Depending on the termination condition of the loop . A statement consisting of the body of a loop and its termination conditions , It's called a loop statement .
for loop
for ( Initialize variable ; Conditional expression ; Operation expression ) {
/ / The loop body
}
for Repeat some code , It's usually related to counting .
Breakpoint debugging
Breakpoint debugging refers to setting a breakpoint on a certain line of the program , During debugging , The program will stop at this line , Then you can debug it step by step , During debugging, you can see the current value of each variable , Are wrong , Debugging to the wrong line of code shows the error , stop .
Breakpoint debugging can help us observe the running process of the program
step :
Press in browser F12--> sources --> Find the file you need to debug --> Set breakpoints on one line of the program
Watch: monitor , adopt watch You can monitor the value of variables , Very often .
F11: Step by step , Let the program execute line by line , This is the time , Observe watch The change in the value of a variable in .
A nested loop
Loop nesting means Define the syntax structure of a circular statement in a circular statement , For example, in for In loop statement , You can nest another for loop , In this way for Circular statements we call double for loop .
for ( The initialization variable of the outer layer ; The conditional expression of the outer layer ; The operation expression of the outer layer ) {
for ( in Initialization variables of layer ; The conditional expression of the inner layer ; Inner layer operation expression ) {
// Execute statement ;
}
}
while loop
while Statement can be used if the conditional expression is true , Loop executes a specified piece of code , Until the expression is not true .
while ( Conditional expression ) {
// Loop body code
}
Execution steps :
① First execute the conditional expression , If the result is true , The loop body code is executed ; If false , Then exit the loop , Execute the following code
② Execute the loop body code .
③ After the loop body code is executed , The program will continue to judge the execution condition expression , If the condition is still true , The loop body will continue to execute , To cycle condition is false when , The whole cycle will end .
do while loop
do.... while The sentence is actually while A variant of the statement . The loop executes a block of code first , Then judge the conditional expression , If the condition is true , The loop body will be executed repeatedly , Otherwise exit the loop .
do {
// The loop body
} while ( Conditional expression )
continue keyword
continue Keywords are used for Immediately jump out of this cycle , So let's go to the next loop ( In this cycle continue After that, the code will be executed once less ) .
break keyword
break Keywords are used for Jump out of the cycle immediately ( The loop ends ).
边栏推荐
- /Bin/ld: cannot find -llz4
- 通过两级网关设计来路由服务网格流量
- 基于 Nebula Graph 构建百亿关系知识图谱实践
- Boot 事务使用
- Best practices for building multi architecture images
- idea 公共方法抽取快捷键
- Invalid bound statement (not found) solution summary
- 由ASP.NET Core根据路径下载文件异常引发的探究
- /Bin/ld: cannot find -lcrypto
- 解决** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the defau
猜你喜欢
又是一年毕业季
Armv8-a programming guide MMU (4)
PHP static members
Compress words (kmp/ string hash, double hash)
The outline dimension function application of small motherboard
Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
如何實現十億級離線 CSV 導入 Nebula Graph
Traversal before, during and after binary tree
Introduction to dynamic planning I, BFS of queue (70.121.279.200)
GraphX 图计算实践之模式匹配抽取特定子图
随机推荐
Flink real-time data warehouse (IX): incremental synchronization of data in MySQL
Astra: could not open "2bc5/ [email protected] /6“: Failed to set USB interface
lseek 出错
/Bin/ld: cannot find -lpam
Invalid bound statement (not found) solution summary
Pyinstaller打包exe附带图片的方法
将点云坐标转换成世界坐标的demo
Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
Ssh/scp does not prompt all activities are monitored and reported
由ASP.NET Core根据路径下载文件异常引发的探究
idea 公共方法抽取快捷键
End time processing method of wechat v3native payment settings
Nebula Graph & 数仓血缘关系数据的存储与读写
C # get PLC information (kepserver) II
Storage, reading and writing of blood relationship data of Nepal Graph & Data Warehouse
Introduction to database system Chapter 1 short answer questions - how was the final exam?
解决BASE64Encoder报错的问题
Application of visualization technology in Nebula graph
Demo of converting point cloud coordinates to world coordinates
Moveit 避障路径规划 demo