当前位置:网站首页>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 ).
边栏推荐
- 6092. Replace elements in the array
- /Bin/ld: cannot find -lcrypto
- Jsp+mysql006 community management system
- Demo of converting point cloud coordinates to world coordinates
- Multi data source configuration code
- GraphX 图计算实践之模式匹配抽取特定子图
- QVariant与Json的各种纠葛——Qt
- Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
- AWS云主机扩容
- lseek 出错
猜你喜欢

Practice of constructing ten billion relationship knowledge map based on Nebula graph

Huawei ECS installs mysqlb for mysqld service failed because the control process exited with error code. See “sys

微信v3native支付设置的结束时间处理办法

图数据库|Nebula Graph v3.1.0 性能报告

愛可可AI前沿推介(7.2)

【5G NR】RRC连接释放

Idea jar package conflict troubleshooting

Recommended practice sharing of Zhilian recruitment based on Nebula graph

Pattern matching extraction of specific subgraphs in graphx graph Computing Practice

Construction and business practice of Zhongke brain knowledge map platform
随机推荐
Invalid bound statement (not found)解决方法总结
Various entanglements between qvariant and Jason -- QT
The sea of stars hidden behind the nebula graph
locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db‘: 没有那个文件或目录
End time processing method of wechat v3native payment settings
Lseek error
华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys
How to use percona tool to add fields to MySQL table after interruption
Boot connection to impala database
Flink real-time data warehouse (7): Flink realizes the full pull module to extract data in MySQL
AWS云主机扩容
Teach you how to build virtual machines locally and deploy microservices
Golang MD5 encryption and MD5 salt value encryption
Data Lake (11): Iceberg table data organization and query
数仓中的维度表与事实表
Idea jar package conflict troubleshooting
Strings and arrays
How to import a billion level offline CSV into Nepal graph
The task cannot be submitted after the installation of flick is completed
可视化技术在 Nebula Graph 中的应用