当前位置:网站首页>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 -lxml2
- 纪念成为首个 DAYU200 三方 demo 贡献者
- Boot transaction usage
- Data Lake (11): Iceberg table data organization and query
- [2. Basics of Delphi grammar] 3 Object Pascal constants and variables
- Boot connection to impala database
- 由ASP.NET Core根据路径下载文件异常引发的探究
- /Bin/ld: cannot find -lgssapi_ krb5
- Jsp+mysql006 community management system
- Pyobject to char* (string)
猜你喜欢

Compress words (kmp/ string hash, double hash)

Write your own CPU Chapter 11 - learning notes

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

又是一年毕业季

PHP static members
![[development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)](/img/26/3f19d36c048e669c736e27384e0fa7.jpg)
[development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)

智联招聘的基于 Nebula Graph 的推荐实践分享

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

Crawl the information of national colleges and universities in 1 minute and make it into a large screen for visualization!

After the win10 system is upgraded for a period of time, the memory occupation is too high
随机推荐
win10系统升级一段时间后,内存占用过高
Wavedec2 in MATLAB, talk about the wavedec2 function [easy to understand]
数字藏品系统开发(程序开发)丨数字藏品3D建模经济模式系统开发源码
Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
Xpt2046 four wire resistive touch screen
原神2.6服务端下载以及搭建安装教程
(5) Flink's table API and SQL update mode and Kafka connector case
爱可可AI前沿推介(7.2)
Introduction to database system Chapter 1 short answer questions - how was the final exam?
Tree binary search tree
fastjson List转JSONArray以及JSONArray转List「建议收藏」
/Bin/ld: cannot find -lpam
Group by的用法
又是一年毕业季
华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys
PHP static members
Song of cactus - throwing stones to ask the way (2)
Aiko ai Frontier promotion (7.2)
Crawl the information of national colleges and universities in 1 minute and make it into a large screen for visualization!
Traversal before, during and after binary tree