当前位置:网站首页>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.边栏推荐
- quick-3.5 无法使用模拟器修改
- 【JVM加载】---类加载机制
- Fragmented NFT (Fractional NFT)
- On the side of Ali, tell me what are the application scenarios of message middleware you know?
- MySQL高级学习笔记
- 一文速学-玩转MySQL获取时间、格式转换各类操作方法详解
- 5 methods of MySQL paging query
- First acquaintance with Flask
- NFTs: The Heart of Digital Ownership
- 对js的数组的理解
猜你喜欢

MySQL高级学习笔记

Year-end summary - the years are quiet~

【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
![[swagger close] The production environment closes the swagger method](/img/43/17be22626ba152b33beaf03f92fbec.png)
[swagger close] The production environment closes the swagger method

npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案

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

MySQL压缩包方式安装,傻瓜式教学

The MySQL database in Alibaba Cloud was attacked, and the data was finally recovered

flutter arr 依赖
![[windows]--- SQL Server 2008 super detailed installation tutorial](/img/b7/dc802c63b07edc4298b6e6b90d865c.png)
[windows]--- SQL Server 2008 super detailed installation tutorial
随机推荐
MySQL压缩包方式安装,傻瓜式教学
C语言 | 获取字符串里逗号间隔的内容
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
Digital twins will be an important way to enter the "metaverse"
2021 Mianjing - Embrace Change
sqlmap injection tutorial common commands
[swagger close] The production environment closes the swagger method
Why is the redis single-threaded also so fast?
场效应管 | N-mos内部结构详解
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
一文速学-玩转MySQL获取时间、格式转换各类操作方法详解
为什么bash中的read要配合while才能读取/dev/stdin的内容
flutter 混合开发 module 依赖
动态规划(一)| 斐波那契数列和归递
小米手机短信定位服务激活失败
Understanding SSRF, this article is enough
File operations in C language (1)
[Elastic-Job source code analysis] - job listener
【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
this指向问题