Blogger Information
Blog 25
fans 1
comment 0
visits 12701
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据类型与流程控制(判断与循环)
xueblog9的进阶之旅
Original
344 people have browsed it

数据类型与流程控制(判断与循环)

  1. 数据类型
    1.1 原始类型
    1.1.1 number:数字,例:1,2,3;
    1.1.2 string:字符串,例:”我是谁”,”abdc”,”112233”;
    1.1.3 boolean:布尔值,只有true,false两种;
    1.1.4 undefined:未定义,let声明变量没有给值,该变量类型则为未定义;
    1.1.5 null:空值,就是什么也没有;
    1.2 引用类型
    1.2.1 array:数组,由不同的原始类型或者引用类型组成的集合叫数组;
    1.2.2 object:对象,由成对的属性和值组成的集合,值可以是任何一种数据类型,也可以是函数;
    1.2.3 函数:即使数据类型,也是函数,可以回调或者闭包,一般循环嵌套使用居多;

  2. 流程控制
    2.1 顺序执行:逐行执行;
    2.2 分支:有条件的执行某段代码,if的判断语句;
    2.2.1 单分支:if判断,true则执行代码,不对false结果执行,一般使用较少;
    2.2.2 双分支:if,else判断,true则执行代码A,false则执行代码2;
    2.2.3 多分支:if,elseif,else判断,根据不同的条件执行不同的代码;
    2.2.4 条件判断连接符:并列符号”&&”,或者符号”||”
    2.2.5 简化方案:格式:switch(true){case 条件: 操作;break;…..default:默认操作};
    2.2.6 双分支的三元操作格式:三元操作格式:变量名 = 条件 ? true : false;

  3. 循环控制
    3.1 循环三要素:索引初始化,循环条件,更新循环条件(不更新条件会陷入死循环);
    3.2 while循环:入口判断,条件不满足则不执行,格式:变量初始化,while(条件){执行代码;循环条件更新};
    3.3 while循环:出口判断,条件不满足也会执行一次,格式:变量初始化,do{执行代码;循环条件跟新}while(条件);
    3.4 for循环:格式:for(变量初始化;循环条件;条件更新){执行代码};
    3.5 for-of循环:for(let变量名of数组){执行代码};
    3.6 for-in循环:for(let变量名 in 对象名);{执行代码};
    3.7 数组可以用while,for,for-of,for-in循环遍历,对象只能用for-in遍历;

案例源码

  1. <script>
  2. console.log('----------------数据类型--------')
  3. let a = 1; // 数字变量
  4. let b = 'string'; // 字符串
  5. let c = false; // 布尔值
  6. let d; // 未定义
  7. let e = [1, 'sdjasdj', true, null] // 数组
  8. let f = {a:'PHP', // 对象
  9. b: '中文网',
  10. c: true,
  11. d: function () {return this.a + this.b;}}
  12. console.log(a)
  13. console.log(typeof(a)) //类型:number
  14. console.log(b)
  15. console.log(typeof(b)) //类型:string
  16. console.log(c)
  17. console.log(typeof(c)) //类型:boolean
  18. console.log(d)
  19. console.log(typeof(d)) //类型:undefined
  20. console.log(e[3])
  21. console.log(Array.isArray(e)) //类型:array
  22. console.log(f.d())
  23. console.log(f.a)
  24. console.log(typeof(f)) //类型:object
  25. console.log(typeof(f.d)) //类型:函数,函数也可以对象属性的值
  26. // 双分支
  27. // 三元操作格式:变量名 = 条件 ? true : false
  28. console.log('------------------双分支----')
  29. let point = 59
  30. if (point >= 60) {
  31. console.log('及格');
  32. }
  33. else {
  34. console.log('不及格');
  35. }
  36. console.log('----------------双分支三元操作----')
  37. // 三元操作格式:变量名 = 条件 ? true : false
  38. let res = point >= 60 ? '及格':'不及格';
  39. console.log(res)
  40. console.log('------------------多分支1----')
  41. // 多分支1
  42. // 并列条件:&&连接,或者条件:|| 连接
  43. let point2 = 60
  44. if (point2 >= 60 && point2 < 70) {
  45. console.log('及格');
  46. }
  47. else if (point2 >=70 && ponit2 < 80) {
  48. console.log('良好');
  49. }
  50. else if (point2 >= 80) {
  51. console.log('优秀');
  52. }
  53. else {
  54. console.log('不及格');
  55. }
  56. // 多分支2:
  57. //
  58. // 格式:switch (true) {
  59. // case 条件:
  60. // 操作;
  61. // break;
  62. // ......
  63. // default:
  64. // 默认操作;
  65. // }
  66. // switch判断入口必须以ture为条件
  67. console.log('---------------------多分支2----')
  68. switch (true) {
  69. case point2 >= 60 && point2 < 70:
  70. console.log('及格');
  71. break;
  72. case point2 >=70 && ponit2 < 80:
  73. console.log('良好');
  74. break;
  75. case point2 >= 80:
  76. console.log('优秀');
  77. break;
  78. default:
  79. console.log('不及格');
  80. }
  81. // 循环:while循环
  82. // 入口判断:判断不满足条件则不执行;
  83. console.log('---------------while循环-----------------')
  84. let i = 0;
  85. let x = 0;
  86. while (i <= 100) {
  87. x = x + i;
  88. i++;
  89. }
  90. console.log(x);
  91. // 出口判断:至少执行一次
  92. let n = 101;
  93. let m = 0;
  94. do {
  95. m = m + n;
  96. n++;
  97. }while (n <= 100)
  98. console.log(m);
  99. // while可用在数组遍历中
  100. console.log('---------------while数组遍历-----------------')
  101. e = [1, 'sdjasdj', true, null]
  102. let length = e.length // 取出数组的长度,用长度去遍历
  103. let q = 0;
  104. while (q <= length){
  105. console.log(e[q]);
  106. q++;
  107. }
  108. // 循环:for循环
  109. // 格式:
  110. console.log('---------------for数组遍历-----------------')
  111. for (let item of e) { // for of用于数组遍历,定义变量访问数组中的每一个值
  112. console.log(item);
  113. }
  114. console.log('---------------对象遍历-----------------')
  115. let y = 0;
  116. for (let i = 0; i <= 100; i++) {
  117. y = y + i;
  118. }
  119. console.log(y)
  120. f = {a:'PHP',
  121. b: '中文网',
  122. c: true,
  123. d: function () {return this.a + this.b;}}
  124. // 对象只能用for-in遍历
  125. for (let k in f) { //for in用于对象遍历,定义变量访问数组中的每一个值
  126. console.log(k)
  127. console.log(f[k]);
  128. }
  129. </script>

实际结果

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!