06-流程控制语句
程序员文章站
2023-01-20 22:06:37
1.if、if-else、if-else if-else 2.逻辑与&&、逻辑或|| 3.switch 4.while循环 循环三步走: 初始化循环变量; 判断循环条件; 更新循环条件; 5.do_while 不管有没有满足while里面的条件,do里面的代码都会走一次 解析:先执行do里面的代码, ......
1.if、if-else、if-else if-else
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var i = 20; 12 if (i >= 20){ 13 console.log('大吉大利'); 14 } 15 alert('继续下一把'); 16 </script> 17 18 </body> 19 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var i = 20; 12 if (i >= 20){ 13 console.log('大吉大利'); 14 }else { 15 console.log('很遗憾,下次继续努力'); 16 } 17 </script> 18 19 </body> 20 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var s = 88; 12 if (s == 100){ 13 console.log('太棒了'); 14 }else if (80 <= s < 100) { 15 console.log('刚刚好'); 16 }else{ 17 console.log('还需努力'); 18 } 19 </script> 20 21 </body> 22 </html>
2.逻辑与&&、逻辑或||
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var sum = 469; 12 var math = 93; 13 if (sum > 400 && math > 90){ 14 console.log('清华大学录入成功'); 15 }else{ 16 console.log('高考失利'); 17 } 18 </script> 19 20 </body> 21 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var sum = 469; 12 var english = 90; 13 if (sum > 400 || english > 87){ 14 console.log('复旦大学录入成功'); 15 }else{ 16 console.log('高考失利'); 17 } 18 </script> 19 20 </body> 21 </html>
3.switch
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var gamescore = 'better'; 12 switch (gamescore) { 13 // case表示一个条件,如果成立,就会走进来,遇到break跳出。如果条件中不写break,那么直到该程序遇到下一个break 14 case 'good': 15 console.log('玩的很好'); 16 break; 17 case 'better': 18 console.log('玩的老牛逼了'); 19 break; 20 case 'best': 21 console.log('恭喜你,吃鸡成功'); 22 break; 23 default: 24 console.log('很遗憾'); 25 break; 26 } 27 </script> 28 29 </body> 30 </html>
4.while循环
循环三步走:
初始化循环变量;
判断循环条件;
更新循环条件;
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var i = 1; //初始化循环变量 12 while (i <= 9) { //判断循环条件 13 console.log(i); 14 i++; //更新循环条件 15 } 16 </script> 17 18 </body> 19 </html>
5.do_while
不管有没有满足while里面的条件,do里面的代码都会走一次
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var i = 3; 12 do{ 13 console.log(i); 14 i++; 15 }while (i < 10){ 16 console.log('no' + i); 17 } 18 </script> 19 20 </body> 21 </html>
解析:先执行do里面的代码,打印3-9,此时的i值为10,接着执行while循环,当i为9 的时候do执行完毕,所以满足条件,打印while循环中的内容。
6.for循环
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 for (var i = 1; i <= 10; i++) { 12 console.log(i); 13 } 14 </script> 15 16 </body> 17 </html>
双重for循环
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 for (var i = 1; i <= 3; i++) { 12 for (var j = 0; j < 6; j++) { 13 document.write('*'); 14 } 15 document.write('<br />') 16 } 17 </script> 18 19 </body> 20 </html>
1.浏览器中输出直角三角形
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 for (var i = 1; i <= 6; i++) { 12 for (var j = 1; j <= i; j++) { 13 document.write('*'); 14 } 15 document.write('<br />') 16 } 17 </script> 18 19 </body> 20 </html>
2输出如下内容
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>流浪者</title> </head> <body> <script> for(var i=1;i<=6;i++){ //行数 //控制我们的空格数 for(var s=i;s<6;s++){ document.write(' ') } for(var j=1;j<=2*i-1;j++){ document.write('*') } document.write('<br>') } </script> </body> </html>
作者:流浪者
日期:2019-09-03