Javascript流程控制

Javascript流程控制

1.条件语句

  (1)if(exp)执行一句代码

  (2)if(exp){执行代码段;}

  (3)if(exp){exp为true执行代码段}else{exp为false执行的代码段}

  (4)if...else if...

  (5)if嵌套

2.循环语句

  (1)for 循环

  (2)while 循环

  (3)do/while循环

3.特殊循环控制

  (1)break 终止循环

  (2)continue 跳过循环

下面就是具体详情:

  for循环

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <h3>for循环</h3>
 9     <p>for(exp1;exp2;exp3;){循环体;}</p>
10     <p>exp1:无条件执行第一个语句</p>
11     <p>exp2:判断是欧服可以执行循环体的条件</p>
12     <p>exp3:做增量的操作</p>
13     <script>
14         for(var i=0;i<4;i++){
15             document.write(i+'hello world <br />')
16         }
17         // 循环i++,i循环三次,每次输出第几次+hello world
18     </script>
19     <p>通过break结束循环</p>
20     <script>
21         for(var i=0;i<=6;i++){
22             if(i>5){
23                 break;
24             }
25             document.write(i+'<br/>');
26         }//循环for语句每次输出1个i值,当i>5时停止输出
27     </script>
28     <p>通过continue跳过档次循环</p>
29     <script>
30         for(var i=1;i<=6;i++){
31             if(i==5){
32                 continue;
33             }
34             document.write(i+'<br />')
35         }//循环for语句,每次输出1个i值,的那个i=5时跳过本次循环进入下次循环。
36     </script>
37 </body>
38 </html>

   for循环嵌套

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <h3>for循环嵌套</h3>
 9     <script>
10         for(var i=1;i<=3;i++){
11             for (var k=1;k<=2;k++){
12                 document.write(k);
13             }
14             document.write(i+'<br>');
15         }
16     </script>
17 </body>
18 </html>

  条件语句

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <h3>条件语句</h3>
 9     <p>假如3>=11 输出a;否则输出b</p>
10     <script>
11         if(3>=11){
12             document.write('a');
13         }else{
14             document.write('b');
15         }
16     </script>
17     <p>定义a=1,判断a是否全等于2,若全等则输出2;若不全等则判断a是否全等于3,若全等则输出3;若不全等则判断a是否全等于1,若全等则输出1</p>
18     <script>
19         var a=1;
20         if(a==2){
21             document.write(2);
22         }else if(a==3){
23             document.write(3);
24         }else if(a==1){
25             document.write(1);
26         }
27     </script>
28 </body>
29 </html>

  switch循环

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <h3>switch循环</h3>
 9     <p>switch循环若是没有找到指定值则一直比较,当找到指定值是执行后边所有代码,所以在swith循环中每个值的代码段最后要写break</p>
10     <p>没加break前</p>
11     <script>
12         var i=3;
13         switch(i){
14             case 1:document.write('a<br />');
15             case 2:document.write('b<br />');
16             case 3:document.write('c<br />');
17             case 4:document.write('d<br />');
18         }//因为我们在这里没有加入break所以说当找到i=3时后执行后边所有代码
19     </script>
20     <p>加break后</p>
21     <script>
22         switch(i){
23             case 1:document.write('a<br />');break;
24             case 2:document.write('b<br />');break;
25             case 3:document.write('c<br />');break;
26             case 4:document.write('d<br />');break;
27         }
28     </script>
29 </body>
30 </html>

  while循环

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <h3>while循环</h3>
 9     <script>
10         var i=0;
11         while(i<=5){
12             i++;
13             document.write(i+'<br />');
14         }//若i<5则执行i++并输出i值
15     </script>
16 </body>
17 </html>

  do/while循环

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <h3>do/while循环</h3>
 9     <script>
10         var x=0;
11         do{
12             x++;
13             document.write(''+x+'次X的值为:'+x+'<br>')
14         }
15         while (x<4);
16         document.write('最终X的值为:'+x+'<br />')
17         //当x<4时执行每次输出第几次的x的值,
18     </script>
19 </body>
20 </html>
原文地址:https://www.cnblogs.com/CcPz/p/8228986.html