Blogger Information
Blog 94
fans 0
comment 0
visits 92726
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【JS】 JS 流程控制之:分支
可乐随笔
Original
554 people have browsed it

JS流程控制之分支

  1. 顺序:默认:与源码书写顺序一致
  2. 分支:有条件的执行某一段代码
  3. 循环:有条件的重复执行某一段代码

1. 顺序是默认的

2. 分支

  1. 单分支
  2. 双分支(简化:三元)
  3. 多分支(简化:switch)

(1).单分支

  1. //代码块
  2. {
  3. // 代码...
  4. }

(2).双分支:true / default

  1. // * 条件一般写 'true'和'!true'
  2. if(!true){
  3. console.log('success')
  4. }else{
  5. console.log('fail')
  6. }
  7. console.log('----------------------')
  8. //三元进行简化
  9. let status1 = true
  10. let result = status1 ? '成功' : '失败'
  11. console.log(result)

(3).多分支

  1. console.log('----------------------')
  2. let grade = 'A'
  3. if (grade === 'A'){
  4. console.log('优秀')
  5. }else if(grade === 'B'){
  6. console.log('良好')
  7. }else if(grade === 'C'){
  8. console.log('合格')
  9. }else if(grade === 'D'){
  10. console.log('补考')
  11. }else{
  12. console.log('非法输入')
  13. }
  14. //switch简化
  15. console.log('----------------------')
  16. grade = 'D'
  17. switch (grade) {
  18. case 'A':
  19. console.log('优秀')
  20. break
  21. case 'B':
  22. console.log('良好')
  23. break
  24. case 'C':
  25. console.log('合格')
  26. break
  27. case 'D':
  28. console.log('补考')
  29. break
  30. default:
  31. console.log('非法输入')
  32. break
  33. }
  34. //上面是单值,如果是区间判断
  35. console.log('----------------------')
  36. score = 0
  37. //switch多值时,参数必须传一个 ture
  38. switch (true) {
  39. case score >= 80 && score <=100:
  40. console.log('优秀')
  41. break
  42. case score >= 70 && score < 80:
  43. console.log('良好')
  44. break
  45. case score >= 60 && score < 70:
  46. console.log('合格')
  47. break
  48. case 'D':
  49. case score >=0 && score < 60:
  50. console.log('补考')
  51. break
  52. default:
  53. console.log('非法输入')
  54. break
  55. }
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!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post