Home > Article > Web Front-end > Detailed explanation of javascript flow control statement while loop and do...while loop syntax examples
while loop
The while loop has the same function as the for loop. The while loop repeatedly executes a piece of code until a certain condition is no longer met.
while statement structure:
while(判断条件) { 循环语句 }
Use a while loop to complete the action of taking balls from the box, one at a time, a total of 6 balls.
<script type="text/javascript"> var num=0; //初始化值 while (num<=6){ //条件判断 document.write("取出第"+num+"个球<br />"); num=num+1; //条件值更新 } </script>
Do...while loop
The basic principle of do while structure and the while structure are Basically the same, but it guarantees that the body of the loop is executed at least once. Because it executes the code first, then judges the condition. If the condition is true, the loop continues.
do...while statement structure:
do { 循环语句 } while(判断条件)
Try to output 5 numbers.
<script type="text/javascript"> num= 1; do{ document.write("数值为:" + num+"<br />"); num++; //更新条件 } while (num<=5) </script>
Use the do...while statement to output 6 numbers.
<script type="text/javascript"> var mynum =6;//mynum初值化数值为6 do{ document.write("数字:"+mynum+"<br/>"); mynum=mynum-1; } while(mynum>=1); </script>
Exit the loop break
In while, for, do...while, while loop Use the break statement to exit the current loop and directly execute the following code.
The format is as follows:
for(初始条件;判断条件;循环后条件值更新){ if(特殊情况) {break;} 循环代码 }
The test results are output. If the result is passed, continue to output the next score. If the result is failed, exit and subsequent scores will not be output.
<script type="text/JavaScript"> var mynum =new Array(70,80,66,90,50,100,89);//定义数组mynum并赋值 var i=0; while(i<mynum.length){ if(mynum[i]<60){ document.write("成绩"+mynum[i]+"不及格,不用循环了"+"<br>"); break; } document.write("成绩:"+mynum[i]+"及格,继续循环"+"<br>"); i=i+1; } </script>
Continue loop continue
Statement structure:
for(初始条件;判断条件;循环后条件值更新){ if(特殊情况){ continue; } 循环代码 }
In the above loop, when a special situation occurs, this loop will be skipped, and subsequent loops will not be affected.
Example: Output test scores. If the score is passed, continue to output the next score. If the score is failed, the score will not be output.
<script type="text/JavaScript"> var mynum =new Array(70,80,66,90,50,100,89);//定义数组mynum并赋值 var i; for(i=0;i<mynum.length;i++){ if(mynum[i]<60){ document.write("成绩不及格,不输出!"+"<br>"); continue; } document.write("成绩:"+mynum[i]+"及格,输出!"+"<br>"); } </script>
In a university programming elective class, we got a set of student data participating in the class, which are name, gender, age and grade. , Next, we need to use JavaScript knowledge to pick out the names of all the freshmen girls.
Student information is as follows:
('Little A', 'Female', 21, 'Freshman'), ('Little B', 'Male', 23, 'Journal' ),
('Little C','Male',24,'Senior'), ('Little D','Female',21,'Freshman'),
('Little E','Female',22,'Female'), ('Little F','Male',21,'Freshman'),
('Little G','Female ',22,'sophomore'), ('little H','female',20,'senior year'),
('little I','female',20,'freshman' ), ('小J','male',20,'junior year')
<script type="text/javascript"> //第一步把之前的数据写成一个数组的形式,定义变量为 infos var infos = [ ['小A','女',21,'大一'], ['小B','男',23,'大三'], ['小C','男',24,'大四'], ['小D','女',21,'大一'], ['小E','女',22,'大四'], ['小F','男',21,'大一'], ['小G','女',22,'大二'], ['小H','女',20,'大三'], ['小I','女',20,'大一'], ['小J','男',20,'大三'] ]; //第一次筛选,找出都是大一的信息 var arr1 = []; var n = 0; for(var i=0;i<infos.length;i++){ if( infos[i][3] == "大一" ){ arr1[n] = infos[i]; document.write(arr1[n]+"<br/>"); n=n+1; } } document.write("大一人数: "+arr1.length+"<br/>"); //第二次筛选,找出都是女生的信息 for(var i=0;i<arr1.length;i++){ //这里可以用switch if(arr1[i][1]=='女'){ document.write(arr1[i][0]+"<br/>"); } } </script>
The above is the detailed content of Detailed explanation of javascript flow control statement while loop and do...while loop syntax examples. For more information, please follow other related articles on the PHP Chinese website!