JavaScript language Basics 14 and javascript language 14
JavaScript loop statement ---------------- while LOOP statement.
When the loop condition is true, the loop continues until the loop condition is false.
The template is as follows:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value=10;while(value>1){document.write(value+"<br>");value--;}</SCRIPT></BODY></HTML>
The while LOOP statement contains a loop condition and body. The value is printed when the value is greater than 1, exit the loop when the value is less than or equal to 1 (we should avoid endless loops when using the while LOOP statement, that is, the loop condition is always false ).
The preceding while LOOP statement first checks whether the condition is true and then executes the statement. Next let's take a look at the first execution and then judgment, that is, at least one loop body ---------- do... while () loop statement:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value=10;do{document.write(value+"<br>");}while(value>20);</SCRIPT></BODY></HTML>
In the appeal Code, when the value is greater than 20, the actual value of the loop body is 10, but the result shows that even if the condition is false, do... while is executed once.
The break statement and the continue statement in the loop statement.
The break statement is used to terminate the loop statement and exit the loop. The continue statement is used to skip the current loop (the code block under the continue statement) and directly execute the next loop body.
Break statement:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value=10;for(var i=0;i<10;i++){if(i==5){break;}document.write(value+"<br>");value--;}</SCRIPT></BODY></HTML>
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value=10;for(var i=0;i<10;i++){if(i==5){break;}document.write(value+"<br>");value--;}</SCRIPT></BODY></HTML>
Code Description: terminate when I is equal to 5 and jump out of the loop body.
Continue statement:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">for(var value=0;value<10;value++){if(value==5){continue;}document.write(value+"<br>");}</SCRIPT></BODY></HTML>
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value=-1;while(value<9){value++;if(value==5){continue;}document.write(value+"<br>");}</SCRIPT></BODY></HTML>
Code Description: When I is equal to 5, it is terminated and the loop body after the current continue is skipped once before execution.
Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/41385641 sentiment control _