JavaScript language Basics 14 and javascript language 14

Source: Internet
Author: User

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 _


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.