JS流程控制之分支
- 顺序:默认:与源码书写顺序一致
- 分支:有条件的执行某一段代码
- 循环:有条件的重复执行某一段代码
1. 顺序是默认的
2. 分支
- 单分支
- 双分支(简化:三元)
- 多分支(简化:switch)
(1).单分支
//代码块
{
// 代码...
}
(2).双分支:true / default
// * 条件一般写 'true'和'!true'
if(!true){
console.log('success')
}else{
console.log('fail')
}
console.log('----------------------')
//三元进行简化
let status1 = true
let result = status1 ? '成功' : '失败'
console.log(result)
(3).多分支
console.log('----------------------')
let grade = 'A'
if (grade === 'A'){
console.log('优秀')
}else if(grade === 'B'){
console.log('良好')
}else if(grade === 'C'){
console.log('合格')
}else if(grade === 'D'){
console.log('补考')
}else{
console.log('非法输入')
}
//switch简化
console.log('----------------------')
grade = 'D'
switch (grade) {
case 'A':
console.log('优秀')
break
case 'B':
console.log('良好')
break
case 'C':
console.log('合格')
break
case 'D':
console.log('补考')
break
default:
console.log('非法输入')
break
}
//上面是单值,如果是区间判断
console.log('----------------------')
score = 0
//switch多值时,参数必须传一个 ture
switch (true) {
case score >= 80 && score <=100:
console.log('优秀')
break
case score >= 70 && score < 80:
console.log('良好')
break
case score >= 60 && score < 70:
console.log('合格')
break
case 'D':
case score >=0 && score < 60:
console.log('补考')
break
default:
console.log('非法输入')
break
}
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!