当前位置:网站首页>JS ------ Chapter II JS logic control
JS ------ Chapter II JS logic control
2022-07-24 23:16:00 【Monkey hanging on a tree】
review
ECMA(javascript The official Standardization Organization ) Basic grammar :
notes , Variable , Operator , data type , Process control , object
data type : character string , The number ( Integers , decimal ), Boolean value (true/false),null,undefined
Process control : The order , Branch , loop
The objectives of this chapter
Process control :
The order : Execute in sequence according to the writing order of the code
Branch : The content of this lesson is
- if Branch : Single branch , Double branch , Multiple branches
- switch Branch
1. Process control
1.1 What is process control
Process control : The program is in progress , Control the execution sequence of the program through specific conditions .
1.2 Three program structures
- Sequential structure : From the top down ,( equation ) From right to left ( If there is an expression on the right ( Left to right ))
- Branching structure : When is the control code ( According to the condition ) perform , Whether to carry out .
- Loop structure : Control code execution ( According to the condition ) How many times .

2. Branching structure --if If
2.1 single if conditional : Single branch
grammar :
if( Conditional expression ){
Code block ;
}Execution process : If if The conditions in the brackets are true, Then execute the code block in braces . Otherwise, do not execute .
Be careful : In this code block , Don't add ";"
<script type="text/javascript">
// Enter the score
var score = Number(prompt(" How many points did you get in Xiaoming's exam ?",""));
// if Single branch judgment
// If (if) A score greater than or equal to 90( Conditions ) establish : Execute the code inside
if(score>=90){
alert(" Reward you with an electric car !");
}
</script> 
2.2 if else Judge : Double branch
Double branch : You must choose one of the two
grammar :
if( Conditional expression ){
Code block 1;
}else{
Code block 2;
}Execute the process :
1. If if The following conditional expression holds , Then execute the code block 1
2. If if The following conditional expression does not hold , Then execute the code block 2
Be careful : else No conditions
// if else Double branch
// Enter the score
var score = Number(prompt(" How many points did you get in Xiaoming's exam ?",""));
// if Double branch judgment : A score greater than or equal to 90 Reward electric cars , otherwise , Whips
if(score>=90){
alert(" Reward an electric car ");
}else{
alert(" Whips ");
}
2.3 multiple if Judge
grammar :
if( Conditional expression 1){
Code block 1;
}else if( Conditional expression 2){
Code block 2;
}else if( Conditional expression 3){
Code block 3;
}
.... // Omit some conditions
else{ // else It can be omitted
Code block n;
}Execute the process :
- Code execution from top to bottom , If the conditional expression 1 establish , Then execute the code block 1, Code block 1 After performing , Other branches are no longer executed ( Each branch is mutually exclusive ).
- If the conditional expression 1 Don't set up , Then judge the conditional expression 2 Is it true , If set up , Then execute the code block 2
- If the conditional expression 2 Cannot be established , Then judge the conditional expression 3 Is it true , If set up , Then execute the code block 3
- If the conditional expression 3 Cannot be established , Then judge the subsequent conditions in turn .
- If all the conditions don't hold , execute else Code block in .
Be careful :else It can be omitted , Means there is no default choice
// if.. else if..else Multiple branches
var score = Number(prompt(" How many points did you get in Xiaoming's exam ?",""));
if(score>=90){ // fraction >=90
alert(" good ");
}else if(score>=80){ // fraction >=80
alert(" good ")
}else if(score>=60){ // fraction >=60
alert(" secondary ");
}else{
alert(" Bad ")
}
<script type="text/javascript">
// Input math scores and Chinese scores
// If mathematics is greater than 80, And Chinese is greater than 90
// Or math equals 100 And Chinese is greater than 70
// Reward electric cars
// Other situations : Whips
var chinese = Number(prompt(" Please input Chinese score :",0));
var math = Number(prompt(" Please enter the math score ",0));
// If the score is invalid , To prompt errors
// Mathematics is less than 0 Greater than 100 Chinese less than 0 Greater than 100
// Wrong writing : math<0 && math>100
if(math<0 || math>100 || chinese<0 || chinese>100){
document.write(" Wrong score ")
}else if(math>=80 && chinese>=90 ||
math==100 && chinese>=70){
document.write(" Reward an electric car ");
}else{
document.write(" Whips ");
}
</script>2.4 if nesting
var score = Number(prompt(" Enter the score ",""));
if(score>=90){
}else {
if(score>=80)(){
}else{
}
}Summary :
if The conditions of branching are all judgments aimed at a certain range
3. Branching structure switch - case
switch: For equivalence judgment
if : For range judgment
grammar :
switch( Variable / data ){
case data 1:
Code block 1;
break;
case data 2:
Code block 2;
break;
case data 3:
Code block 3;
break;
default:
Default code block ;
break;
}Execute the process :
- according to switch The data in parentheses , With the following case Compare the corresponding values one by one .
- Satisfy a certain case Branch , The corresponding code block is executed
- If the above branches are not satisfied , execute default The following code block
- among break( End statement execution ) Represents the whole switch End of sentence , If a branch does not break, Then the code will continue to execute downward , Until I met break Or all statements are executed ,switch The statement will end .
- default Sentences can be omitted .
<script type="text/javascript">
// Linghuchong takes part in the martial arts competition :
// If you are the first, you will be the leader of Wulin
// If he is the second, he will be the leader of Wudang
// If he is the third, he will be the leader of Emei
// Other situations , Expel from the school
var mc = Number(prompt(" Please enter linghuchong's martial arts ranking :",""));
// For a variable value , Judge whether it conforms to that situation
switch(mc){
case 1: // If mc yes 1 The situation of
document.write(" Leader of Wulin ");
break;
case 2: // If mc yes 2 The situation of
document.write(" Wudang leader ");
break;
case 3: // If mc yes 3 The situation of
document.write(" Emei leader ");
break;
default: // If mc It's something else
document.write(" Expel from the school ");
break;
}
/*
// For a variable value , Judge whether it conforms to that situation
switch(mc){
case 1: // If mc yes 1 The situation of
document.write(" Leader of Wulin ");
break;
case 2: // If mc yes 2 The situation of
document.write(" Wudang leader ");
break;
case 3: // If mc yes 3 The situation of
document.write(" Emei leader ");
break;
default: // If mc It's something else
document.write(" Expel from the school ");
break;
}*/
/*
if(mc==1){
document.write(" Leader of Wulin ");
}else if(mc==2){
document.write(" Wudang leader ");
}else if(mc==3){
document.write(" Emei leader ");
}else{
document.write(" Expel from the school ");
}*/
</script>
// According to the day of today , Output what to eat
// week 1,2 Eat rice 3,4 Eat noodles 5,6 dumplings 7 To lose weight
// According to the day of today , Output what to eat
// week 1,2 Eat rice 3,4 Eat noodles 5,6 dumplings 7 To lose weight
var weekDay = prompt(" Please enter the day of today ?","");
switch(weekDay){
case " Monday ":
//document.write(" Eat rice ");
//break;
case " Tuesday ":
document.write(" Eat rice ");
break;
case " Wednesday ":
//document.write(" Eat noodles ");
//break;
case " Thursday ":
document.write(" Eat noodles ");
break;
case " Friday ":
//document.write(" Eat dumplings ");
//break;
case " Saturday ":
document.write(" Eat dumplings ");
break;
case " Sunday ":
//document.write(" To lose weight ");
//break;
case " Sunday ":
document.write(" To lose weight ");
break;
default:
document.write(" Please input !!!");
break;
}
</script>
summary :
Process control : Sequential structure , Branching structure (if,switch), Loop structure
边栏推荐
- 《JUC并发编程 - 高级篇》05 -共享模型之无锁 (CAS | 原子整数 | 原子引用 | 原子数组 | 字段更新器 | 原子累加器 | Unsafe类 )
- yolov5
- WPF opens external programs and activates them when needed
- Notes of Teacher Li Hongyi's 2020 in-depth learning series 6
- Burp's thinking from tracing to counteracting
- Notes of Teacher Li Hongyi's 2020 in-depth learning series 9
- Network Security Learning (II) IP address
- CA certificate production practice
- Trinitycore worldofwarcraft server - registration website
- Notes of Teacher Li Hongyi's 2020 in-depth learning series 4
猜你喜欢

Convert a string to an integer and don't double it

Use kettle to read the data in Excel file and store it in MySQL

QT6 with vs Code: compiling source code and basic configuration

Error connecting MySQL database with kettle

What is a video content recommendation engine?

Time series data in industrial Internet of things

Okaleido tiger NFT is about to log in to binance NFT platform, and the future market continues to be optimistic

Li Kou 1184. Distance between bus stops

Paper notes: accurate causal influence on discrete data

高阶产品如何提出有效解决方案?(1方法论+2案例+1清单)
随机推荐
Power consumption of chip
GB、MB、KB分别是什么意思。大小分别是多少
谢振东:公共交通行业数字化转型升级的探索与实践
What is a video content recommendation engine?
Notes of Teacher Li Hongyi's 2020 in-depth learning series lecture 1
About constant modifier const
国信证券手机开户安全吗
The specified data is grouped and the number of repetitions is obtained in Oracle
新手哪个证券开户最好 开户最安全
Time series data in industrial Internet of things
WPF uses pathgeometry to draw the hour hand and minute hand
Old Du servlet JSP
JUC concurrent programming - Advanced 05 - lock free of shared model (CAS | atomic integer | atomic reference | atomic array | field updater | atomic accumulator | unsafe class)
The rule created by outlook mail is invalid. Possible reasons
The kettle job implementation runs a kettle conversion task every 6S
Shardingsphere database sub database sub table introduction
Oracle中实现对指定数据分组且获取重复次数
Browser cache
基于FPGA的VGA显示
用VS Code搞Qt6:编译源代码与基本配置