Detailed explanation of javascript flow control statement while loop and do...while loop syntax examples

伊谢尔伦
Release: 2017-07-24 09:44:51
Original
2232 people have browsed it

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(判断条件) { 循环语句 }
Copy after login

Use a while loop to complete the action of taking balls from the box, one at a time, a total of 6 balls.


Copy after login

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(判断条件)
Copy after login

Try to output 5 numbers.


Copy after login

Use the do...while statement to output 6 numbers.


Copy after login

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;} 循环代码 }
Copy after login

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.


Copy after login

Continue loop continue
Statement structure:


for(初始条件;判断条件;循环后条件值更新){ if(特殊情况){ continue; } 循环代码 }
Copy after login

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.


Copy after login

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')


Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!