当前位置:网站首页>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.边栏推荐
- Gradle sync failed: Uninitialized object exists on backward branch 142
- 【Elastic-Job】分布式调度任务概览篇
- 阿里云中mysql数据库被攻击了,最终数据找回来了
- Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?
- SQLite 查询表中每天插入的数量
- mysql常用命令
- Access database query
- cocos2d-x-3.2创建项目方法
- C language tutorial (1) - preparation
- [uiautomation] Get WeChat friend list (stored in txt)
猜你喜欢

Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'
![[swagger close] The production environment closes the swagger method](/img/43/17be22626ba152b33beaf03f92fbec.png)
[swagger close] The production environment closes the swagger method

feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

【windows】--- SQL Server 2008 超详细安装教程

自定dialog 布局没有居中解决方案

阿里云中mysql数据库被攻击了,最终数据找回来了

Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?

The server time zone value ‘й‘ is unrecognized or represents more than one time zone

Linux modify MySQL database password

js中的this指向与原型对象
随机推荐
MySQL-如何分库分表?一看就懂
360 加固 file path not exists.
Filter out egrep itself when using ps | egrep
Detailed explanation of pointers in C language
数据库 | SQL增删改查基础语法
最新MySql安装教学,非常详细
[Elastic-Job] Overview of Distributed Scheduling Tasks
cocos2d-x 实现跨平台的目录遍历
Tencent Cloud Lightweight Server deletes all firewall rules
The MySQL database in Alibaba Cloud was attacked, and the data was finally recovered
For penetration testing methods where the output point is a timestamp (take Oracle database as an example)
js中的对象与函数的理解
cocos2d-x-3.x 修改和纪录
How MySQL - depots table?A look at will understand
MySQL高级语句(一)
sqlmap injection tutorial common commands
Markdown 帮助文档
阿里一面,说说你知道消息中间件的应用场景有哪些?
通信原理——纠错编码 | 汉明码(海明码)手算详解
【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了