当前位置:网站首页>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 .
边栏推荐
- Prevent cookies from modifying ID to cheat login
- Alibaba cloud international receipt message introduction and configuration process
- 【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发1/100
- Why do major domestic manufacturers regard cloud computing as a pastry? Do you really understand this trillion market
- Introduction to depth first search (DFS)
- CommonTitleBar hide left right
- Attack and defense World Lottery
- Use of elastic box / expansion box (Flex)
- Netdata 性能监测工具介绍、安装、使用
- redis配置文件下载
猜你喜欢
随机推荐
Graph node deployment and testing
Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
C commissioned use cases
Data extraction 2
You may need an additional loader to handle the result of these loaders.
[MRCTF2020]PYWebsite 1
关于数据库的接口响应非常慢
情人节,我用字符画出了一个对象!
JS advanced knowledge - function
[ten thousand words long article] thoroughly understand load balancing, and have a technical interview with Alibaba Daniel
Eval and assert execute one sentence Trojan horse
Use of "PHP Basics" Boolean
How does kettle handle text data transfer as' 'instead of null
Why do major domestic manufacturers regard cloud computing as a pastry? Do you really understand this trillion market
Lua stateful iterator
借生态力量,openGauss突破性能瓶颈
Development of three database general SQL code based on PG Oracle and MySQL
All in one 1319 - queue for water
百人参与,openGauss开源社区这群人都在讨论什么?


![[BJDCTF2020]EasySearch 1](/img/ea/90ac6eab32c28e09bb1fab62b6b140.png)
![[MRCTF2020]Ezpop 1](/img/c0/41873fe5864490ade403eeddc26976.png)





