当前位置:网站首页>Process control - Branch
Process control - Branch
2022-07-27 08:18:00 【huijie_ 0716】
Catalog
5、if else if sentence ( Multi branch statement )
6、 Sentence for judging grades
8、 Ternary expression — Number complement 0
11、switch Statement and if else if The difference between sentences
Process control
During the execution of a program , The execution order of each code has a direct impact on the result of the program . Many times we need to control the execution order of the code to realize the functions we want to complete .
Simple understanding ︰ Process control is to control what structural order our code technology executes
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 .

Sequence flow control
The sequential structure is the simplest in the program 、 The most basic process control , It has no specific grammatical structure , The program will follow Sequence of codes , Execute sequentially , Most of the code in the program is executed in this way .
Branch process control
1、 Branching structure
In the process of executing code from top to bottom , According to different path codes ( Execute the process of selecting one more code ), So we can get different results

JS Language provides two branch structure statements
- if sentence
- switch sentence
2、if Branch statement
If if The result of the conditional expression is true true Execute... In braces Execute statement
If if The result of the conditional expression is false The statement in braces is not executed execute if Code after statement
// 2. Execution ideas If if The result of the conditional expression is true true Execute... In braces Execute statement
// If if The result of the conditional expression is false The statement in braces is not executed execute if Code after statement
// 3. Code experience
if (3 < 5) {
alert(' Desert camel ');
}3、if else Branch statement
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

var age = prompt(' Please enter your score :');
if (age >= 80) {
alert(' Reward you for playing mobile ');
} else {
alert(' roll , Go home and do your homework ');
}4、 Judgement of leap year
The year entered by the user , If it's a leap year, it pops up a leap year , Otherwise, the normal year will pop up
// Algorithm : Can be 4 Divisible and not divisible 100 Leap year ( Such as 2004 Year is leap year ,1901 Year is not a leap year ) Or can be 400 Divisible is leap year
// eject prompt Input box , Let the user enter the year , Take this value and save it in a variable
// Use if Statement to determine whether it is a leap year , If it's a leap year , Is executed if The output statement in braces , Otherwise, it will be executed else The output statement inside
// Be sure to pay attention to the inside and && And or || Writing , At the same time, note that the way to judge the division is to take the remainder as 0
var year = prompt(' Please enter the year :');
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
alert(year + ' It's a leap year ');
} else {
alert(year + ' It's the year of the year ');
}5、if else if sentence ( Multi branch statement )
1. Multi branch statement Is to use multiple conditions to select different statements to execute Get different results multi-select 1 The process of
2. if else if A statement is a multi branch statement
3. Grammatical norms
if ( Conditional expression 1) {
// sentence 1;
} else if ( Conditional expression 2) {
// sentence 2;
} else if ( Conditional expression 3) {
// sentence 3;
} else {
// Last statement ;
}
4. Execution ideas
If the conditional expression 1 Fulfill it sentence 1 After execution , Quit the whole if Branch statement
If the conditional expression 1 dissatisfaction , Then judge the conditional expression 2 If you are satisfied , Execute statement 2 And so on
If none of the above conditional expressions holds , execute else The words in it

6、 Sentence for judging grades

// Pseudo code According to the idea of judging from big to small
// eject prompt Input box , Let the user enter a score (score), Take this value and save it in a variable
// Use multiple branches if else if Statement to judge the output of different values
var score = prompt(' Please enter the score :');
if (score >= 90) {
alert('A');
} else if (score<90 && score >= 80) {
alert('B');
} else if (score < 80 && score >= 70) {
alert('C');
} else if (score < 70 && score >= 60) {
alert('D');
} else {
alert('E');
}7、 Ternary expression
Grammatical structure : Conditional expression ? expression 1 : expression 2
Execution ideas
If the conditional expression turns out to be true be return expression 1 Value If the conditional expression turns out to be false Then return to expression 2 Value
var num = 10;
var result = num > 5 ? ' Yes ' : ' No, it isn't '; // We know that an expression has a return value
console.log(result);
// if (num > 5) {
// result = ' Yes ';
// } else {
// result = ' No, it isn't ';
// }8、 Ternary expression — Number complement 0

// User input 0~59 A number between
// If the number is less than 10, Then add... Before this number 0,( Add 0 Splicing ) otherwise No operation
// Accept the return value with a variable , Output
var time = prompt(' Please enter a 0 ~ 59 A number between ');
// Ternary expression expression ? expression 1 : expression 2
var result = time < 10 ? '0' + time : time; // Assign the return value to a variable
alert(result);9、switch sentence
1、switch Statements use
1. switch Statement is also a multi branch statement Multiple choices can also be realized 1
2. Grammatical structure switch transformation 、 switch case The meaning of small examples or options
switch ( expression ) {
case value1:
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 , It's time to do it case The words in it If none of them match , Then perform default The words in it
switch (8) {
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 ');
}2、switch matters needing attention
var num = 1;
switch (num) {
case 1:
console.log(1);
case 2:
console.log(2);
case 3:
console.log(3);
break;
}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
3. break If the current case There's no break Will not quit switch Is to move on to the next case
10、 Query fruit cases
// eject prompt Input box , Let the user enter the name of the fruit , Take this value and save it in a variable .
// Take this variable as switch The expression in parentheses .
// case The following values write several different fruit names , Be sure to put quotation marks , Because it must be a congruent match .
// Pop up different prices . Also pay attention to each case Then add break , In order to exit switch sentence .
// take default Set to no such fruit .
var fruit = prompt(' Please enter the fruit you want to query :');
switch (fruit) {
case ' Apple ':
alert(' The price of apple is 3.5/ Jin ');
break;
case ' durian ':
alert(' The price of durian is 35/ Jin ');
break;
default:
alert(' Without this fruit ');
}11、switch Statement and if else if The difference between sentences
- In general , They can be replaced by each other
- switch..case Statements usually deal with case In order to compare the determined values , and i...els... More flexible sentences , It is often used to judge the range ( Greater than 、 Equal to a certain country )
- switch A conditional statement that executes directly into a program after conditional judgment , More efficient . and if...else There are several conditions for a statement , You have to judge how many times .
- When there are fewer branches ,if...else Statement execution efficiency ratio switch Sentence height .
- When there are more branches ,switch The execution efficiency of the statement is relatively high , And the structure is clearer .
边栏推荐
- [target detection] yolov6 theoretical interpretation + practical test visdrone data set
- Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
- [BJDCTF2020]EasySearch 1
- Data extraction 2
- Binglog backup data
- Promise details
- 关于数据库的接口响应非常慢
- Introduction, installation and use of netdata performance monitoring tool
- 代码接口自动化的有点
- [ten thousand words long article] thoroughly understand load balancing, and have a technical interview with Alibaba Daniel
猜你喜欢

All in one 1251 - Fairy Island for medicine (breadth first search)

Record a PG master-slave setup and data synchronization performance test process

Notes in "PHP Basics" PHP

一段平平无奇的秋招经历

An ordinary autumn recruitment experience

数据提取1

2020 International Machine Translation Competition: Volcano translation won five championships
Why do major domestic manufacturers regard cloud computing as a pastry? Do you really understand this trillion market

Lua iterator

Plato farm is expected to further expand its ecosystem through elephant swap
随机推荐
Debug: generic related "unresolved external symbols"
Why do major domestic manufacturers regard cloud computing as a pastry? Do you really understand this trillion market
Installation and use of beef XSS
The third letter to the little sister of the test | Oracle stored procedure knowledge sharing and test instructions
2020 International Machine Translation Competition: Volcano translation won five championships
"Intermediate and advanced test questions": what is the implementation principle of mvcc?
百人参与,openGauss开源社区这群人都在讨论什么?
mqtt指令收发请求订阅
QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform
Is redis really slowing down?
信息化项目风险控制与应用
Qt Creator代码风格插件Beautifier
[target detection] yolov6 theoretical interpretation + practical test visdrone data set
Introduction, installation and use of netdata performance monitoring tool
Notes in "PHP Basics" PHP
1176 questions of Olympiad in informatics -- who ranked K in the exam
A quick overview of transformer quantitative papers in emnlp 2020
关于数据库的接口响应非常慢
阿里云国际版回执消息简介与配置流程
Redis configuration file download