当前位置:网站首页>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 ).
边栏推荐
- Make p12 certificate [easy to understand]
- Add user-defined formula (time sharing t+0) to mobile app access as an example
- (Wanzi essence knowledge summary) basic knowledge of shell script programming
- Nebula Graph & 数仓血缘关系数据的存储与读写
- 源码look me
- /Bin/ld: cannot find -lxslt
- 可视化技术在 Nebula Graph 中的应用
- Invalid bound statement (not found)解决方法总结
- 手机app通达信添加自定义公式(分时T+0)为例子讲解
- (万字精华知识总结)Shell脚本编程基础知识
猜你喜欢

Dimension table and fact table in data warehouse

MySQL calculates the data within the longitude and latitude range

Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau

《大学“电路分析基础”课程实验合集.实验七》丨正弦稳态电路的研究

PHP static members

愛可可AI前沿推介(7.2)

Write your own CPU Chapter 11 - learning notes

解决** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the defau

手机app通达信添加自定义公式(分时T+0)为例子讲解

智联招聘的基于 Nebula Graph 的推荐实践分享
随机推荐
Construction and business practice of Zhongke brain knowledge map platform
Conditions and solutions of deadlock
解决** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the defau
《大学“电路分析基础”课程实验合集.实验七》丨正弦稳态电路的研究
Boot transaction usage
HMS core machine learning service helps zaful users to shop conveniently
【题解】Educational Codeforces Round 82
Song of cactus - throwing stones to ask the way (3)
/Bin/ld: cannot find -llz4
数组和链表的区别浅析
fastjson List转JSONArray以及JSONArray转List「建议收藏」
2020.4.12 byte written test questions B DP D monotone stack
[2. Basics of Delphi grammar] 3 Object Pascal constants and variables
Figure database | Nepal graph v3.1.0 performance report
Analysis of the difference between array and linked list
matlab中wavedec2,说说wavedec2函数[通俗易懂]
注册成为harmonyos开发者并安装DevEco Studio 3.0 Beta2 for HarmonyOS
Ssh/scp does not prompt all activities are monitored and reported
Crawl the information of national colleges and universities in 1 minute and make it into a large screen for visualization!
全是精华的模电专题复习资料:基本放大电路知识点