当前位置:网站首页>Flow control statement in js
Flow control statement in js
2022-07-31 05:58:00 【messy me】
目录
流程控制语句
js中的程序是从上到下一行一行执行的,通过流程控制语句可以控制程序执行流程,是程序可以根据一定的条件来选择执行.
控制语句分类:
| 控制语句 | |
| 1.条件判断语句 | if语句 |
| 2.条件分支语句 | switch语句 |
| 3.循环语句 |
条件判断语句(if):
使用条件判断语句可以在执行某个语句之前进行判断,如果条件成立才会执行语句,条件不成立则语句不执行.
if语句:
语法一:
if(条件表达式)
语句
if语句在执行时,会先对条件表达式进行求值判断.
如果条件表达式的值为true,则执行if后的语句.
如果条件表达式的值为false,则不会执行if后的语句.
if语句只能控制紧随其后的那个语句,如果需要ifStatements control more than one statement.
Multiple statements can be put into a code block.
“代码块”:A code block refers to encapsulating multiple lines of code into a single statement,使用{}to put multiple lines of code into it.
语法二:
if(条件表达式){
语句...
}else{
语句...
}
if...else...语句
当该语句执行时,会先对if后的条件表达式进行求值判断.
如果该值为true,则执行if后的语句,如果该值为false.则执行else后的语句.
语法三:
if(条件表达式){
语句...
}else if(条件表达式){
语句...
}else if(条件表达式){
语句...
}else{
语句...
}
if...else if...else当该语句执行时,会从上到下依次对条件表达式进行求值判断.
如果值为true,则执行当前语句.如果值为false,则继续向下判断.
如果所有的条件都不满足,则执行最后一个else后的语句.
该语句中,只会有一个代码块被执行,一旦代码块执行了,则直接结束语句.条件分支语句(Switch):
Switch...case...语句在执行时会依次将case后的表达式的值和switch后的条件表达式的值进行全等比较,如果比较结果为true,from the currentcase处开始执行代码,当前case后的所有代码都会执行,我们可以在case后面跟着一个break关键字,This will ensure that only the current one is executedcase后的语句,而不会执行其他的case语句.如果比较为false,Continue to compare downwards.
条件分支语句(Switch):
语法:
switch(条件表达式){
case 表达式:
语句...
break;
case 表达式:
语句...
break;
default:
语句...
break;
}
switch语句和if语句的功能实际上有重复的,使用switch可以实现if的功能.
同时使用if也可以实现switch功能,所以我们使用时,You can use it according to your own preferences.循环语句:
循环语句(while):
A piece of code can be executed multiple times through a loop statement.
循环语句while:
语法:
while(条件表达式){
语句...
}
while语句在执行时,先对条件表达式进行求值判断,
如果值为true,则执行循环体,循环体执行完毕后,继续对表达式进行判断.
如果仍为true,则继续执行循环体,以此类推.直至条件表达式为false时,则终止循环.
do...while循环
语法:
do{
语句...
}while(条件表达式)
实际上这两个语句功能类似,不同的是while是先判断后执行,而do...while会先执行后判断
do...while可以保证循环体至少执行一次,而while不能执行.循环语句(for):
A piece of code can be executed multiple times through a loop statement.
for循环:
在for循环中,为我们提供了专门的位置用来放三个表达式:
1.初始化表达式,2.条件表达式,3.更新表达式.
语法:
for(1:初始化表达式;2:条件表达式;4:更新表达式){
3:语句...
}
for循环执行流程:
1:执行初始化表达式,初始化变量.
2:执行条件表达式,判断是否执行循环.
如果为true,则执行循环3,
如果为false,则终止循环.
4:执行更新表达式,Continue to repeat the steps after the update expression is executed2.边栏推荐
- mysql启动报错The server quit without updating PID file几种解决办法
- (Crypto必备干货)详细分析目前NFT的几大交易市场
- Build DVWA with phpstudy
- Common JVM interview questions and answers
- cocos2d-x 实现跨平台的目录遍历
- What is the difference between NFT and digital collection?
- Build vulhub vulnerability shooting range on kali
- [uiautomation] Get WeChat friend list (stored in txt)
- 2021面经-拥抱变化
- 微信小程序启动优化
猜你喜欢

Build DVWA with phpstudy

MySQL高级语句(一)
![[Elastic-Job source code analysis] - job listener](/img/99/5e047b1aa83aad7d7f17b4eec606e6.png)
[Elastic-Job source code analysis] - job listener

阿里一面,说说你知道消息中间件的应用场景有哪些?

NFT与数字藏品到底有何区别?

MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案

GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?

安装Multisim出现 No software will be installed or removed解决方法

On the side of Ali, tell me what are the application scenarios of message middleware you know?

著名网站msdn.itellyou.cn原理分析
随机推荐
Chinese garbled solution in UTF-8 environment in Powershell
C language tutorial (2) - printf and data types that come with c
【windows】--- SQL Server 2008 超详细安装教程
flutter 混合开发 module 依赖
2021美赛C题M奖思路
Why is the redis single-threaded also so fast?
5 methods of MySQL paging query
Understanding SSRF, this article is enough
小米手机短信定位服务激活失败
通信原理——纠错编码 | 汉明码(海明码)手算详解
NFT:数字所有权的核心
NFT与数字藏品到底有何区别?
The latest MySql installation teaching, very detailed
js中流程控制语句
场效应管 | N-mos内部结构详解
Why does read in bash need to cooperate with while to read the contents of /dev/stdin
MySql to create data tables
File operations in C language (1)
MySQL分页查询的5种方法
sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named