当前位置:网站首页>JS day 4 process control (if statement and switch statement)
JS day 4 process control (if statement and switch statement)
2022-07-29 07:21:00 【Pillow said he didn't want to wake up】
One 、 What is process control :
During the execution of a program , The execution order of each code has a direct impact on the result of the program . A lot of times
We all need to control the execution order of the code to achieve the functions we want to complete .
brief introduction : Process control is to control our code according to what structural order our code executes
Two 、 There are three main structures of process control , Namely Sequential structure 、 Branching structure and Loop structure
1、 Sequential structure
The sequential structure is the simplest in the program 、 The most basic process control , He has no specific grammatical structure , The sequence of the program is as follows , Execute sequentially , Most of the code in the program is executed in this way
2、 Branch process control
In the process of executing code from top to bottom , According to different conditions , Execute different path codes ( Execute the process of selecting one more code ), So we can get different results
js Language provides two branches of structural statements
if Grammatical structure and switch Grammatical structure
(1)if The grammatical structure of if( If )
1) Single branch statements
Grammatical structure
if( Conditional expression )
{ Execute statement }Execution ideas If if The condition of the price adjustment expression inside is true true Execute the execution statement in braces
If if The conditional expression is false The statement in braces is not executed execute if Code after statement
Code display
if(3<5){
alert(' Pillow can't wake up '); // If 3<5 A warning box pops up ‘ Pillow can't wake up ’
} Pictures show :
2)if else sentence ( Two branch statements )
Grammatical structure : if If else otherwise
if( Conditional expression ){
Execute statement 1
} else{
Execute statement 2
}
Execution ideas : If the expression turns out to be true , Then execute the statement 1 otherwise , Execute statement 2
Code display :
<script>
var age = prompt(' Please enter your age ')
if (age>=18) {
alert(' Pillow can't wake up ')
}
else{
alert(' The pillow can't sleep ')
};
// Enter the age , If greater than or equal to 18 A warning box pops up “ Pillow can't wake up ”
If less than 18 A warning box pops up “ Can't sleep on the pillow ”Pictures show :


3)if else if ( Multi branch statement )
Multi branch statement It is to use multiple conditions to execute different statements Get different results multi-select 1 The process of
if else if A statement is a multi branch statement
Grammatical norms
if( Conditional expression 1){
sentence 1
} else if( Conditional expression 2){
sentence 2
} else {
sentence 3
}Execution ideas : If the conditional expression 1, Fulfill it , After execution Quit the whole if sentence
If the conditional expression 1 dissatisfaction Then judge the conditional expression 2 If you are satisfied , Execute statement 2 And so on
If all the above conditions are not true , execute else The words in it
Be careful :
(1) Multi branch statement or multi choice 1 Finally, only one statement is executed
(2)else if In theory, there can be any number of conditions
(3)else if There's a space in the middle
The case shows :
requirement : User entered score , According to the score (score) Output the corresponding grade letter A,B,C,D,E
1、90 above , Output A
2、80( contain )~90( Not included ), Output B
3、70( contain )~80( Not included ), Output C
4、60( contain )~70( Not included ), Output D
5、60( Not included ) following , Output E
Code display ;
<script>
var score = prompt(' Please enter your score :');
if (score>=90){
alert('A');
} else if(score >= 80 ){
alert('B');
} else if (score>=70) {
alert('C');
} else if (score >= 60) {
alert('D');
} else {
alert('E');
}
</script>Result display :


2、switch sentence
(1)Sitch Statement is also a multi branch statement , Multiple choices can also be realized 1, It is used to execute different code based on different conditions , When you want to set a series of specific values for a variable, the option is , You can use switch transformation 、 switch case The meaning of small examples or options
(2) Grammatical structure
switch( expression ) {
case value:
switch( expression ) {
case value:
Execute statement 1:
break;
case value2:
Execute statement 2;
break;
....
default
Execute the last statement ;
}(3) Execution ideas : Using the value of our expression and case The following option values match If match up Is executed case
The words in it If there is no match , Then perform default The words in it
(4) Code display :
switch(2){
case 1:
console.log(' This is a 1');
break;
case 2:
console.log(' This is a 2');
break;
case 3:
console.log(' This is a 3');
break;
default:
console.log(' No match ');
}Result display :

Be careful
1、 We develop inside Expressions are often written as variables
2、 We num Value and case When the values inside match is Congruence The value must be consistent with the data type num===1
for example
Code :
var num = '3'; // there 3 It's character type
switch (num) {
case 1:
console.log(1);
break;
case 3:
console.log(3);
break;
}result :

3、 If the current case There's no break Will not quit switch Is to move on to the next case
Code display :
var num = 3;
switch (num) {
case 1:
console.log(1);
break;
case 3:
console.log(3);
// Nothing here break
case 5:
console.log(5);
break;
}Result display :

This is because case3 No, break The end of the statement , So it leads to the next cycle , Know how to meet break To end the cycle .
therefore break Be sure to write
边栏推荐
- 【OpenGL】着色器(Shader)的使用
- Docker最新超详细教程——Docker创建运行MySQL并挂载
- Round avatar of user list and follow small blocks
- gin 模版
- 0 8 动态规划(Dynamic Programming)
- My personal website doesn't allow access to wechat, so I did this
- MySQL 使用客户端以及SELECT 方式查看 BLOB 类型字段内容总结
- Record - step on the pit - real-time data warehouse development - doris/pg/flink
- 2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func main() { f
- Spingboot integrates the quartz framework to realize dynamic scheduled tasks (support real-time addition, deletion, modification and query tasks)
猜你喜欢

Cvpr2021 | multi view stereo matching based on self supervised learning (cvpr2021)

Vmware16 create virtual machine: win11 cannot be installed

最新百亿量化私募名单

Summary of OCR optical character recognition methods

我,28岁,测试员,10月无情被辞:想给还在学测试 的人提个醒......

H3C_ Using setting default static routing priority to realize the active and standby function of export dual lines

2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f

MySQL advanced (Advanced) SQL statement (I)

如何与斯堪尼亚SCANIA建立EDI连接?

Docker最新超详细教程——Docker创建运行MySQL并挂载
随机推荐
MySQL如何把行转换为列?
Kubernetes (V) -- deploy kubernetes dashboard
fillder使用
leetcode力扣经典问题——4.寻找两个正序数组的中位数
Paper reading (62):pointer networks
Excel文件读写(创建与解析)
2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f
CMOS芯片制造全工艺流程
Operator3 - design an operator
tp6 使用 ProtoBuf
VMware16安装虚拟机遇到的问题
用户列表 圆形头像并跟随小板块
After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is
Win11 system error: code execution cannot continue because ierutil.dll cannot be found. Reinstalling the program may fix this problem
After 4 years of development and 13K, if you want to change to automated testing, can your salary still rise···
JS break and continue and return keywords
MySQL 使用客户端以及SELECT 方式查看 BLOB 类型字段内容总结
Win11vmware turns on the virtual machine and restarts on the blue screen and the solution that cannot be started
Latest 10 billion quantitative private placement list
gin 中间件