Basic Application of Javascript loop statements [review]

Source: Internet
Author: User

Review the knowledge about Javascript Loop Statements.

Generally, program statements are executed in the writing order. The previous code is executed first, and the subsequent code is executed later. However, this simple top-down one-way process is only applicable to some very simple programs. In most cases, the priority of program code execution needs to be determined based on logic judgment. To change the execution sequence of program code, any programming language requires conditional statements and cyclic statements, and Javascript is no exception.

This section describes Javascript loop statements.

The following types of Javascript loop statements are available:

  • Test the expression at the beginning of the loop (while LOOP Statement)
  • Test the expression at the end of the loop (do... while loop Statement)
  • Perform operations on each attribute of an object (for... in loop Statement)
  • Loop controlled by counter (for loop Statement)
Use for loop statements

The for loop statement specifies a counter variable, a test condition, and updates the counter behavior.

You must test the conditions before repeating each loop. If the test succeeds, the code in the loop is executed. If the test fails, the code in the loop is not executed, but the first line of code followed by the loop is executed. When this loop is executed, the counter variable is updated before the next repeating loop.

If the cycle condition persists, the loop is never executed. If the conditions are always met, an infinite loop will occur. The previous one is required in some cases, but the latter is basically not applicable. Therefore, you must pay attention to it when writing loop conditions.

Example code of for loop statement:

1 for (I = 0; I <= 5; I ++)
2 {
3 document. write (I );
4 document. write ("<br> ");
5}

Use the for... in loop statement

Javascript provides a special way to traverse all user-defined attributes of an object or all elements of an array. In the for... in loop, the cyclic counter is a string, not a number. It contains the name of the current attribute or the subscript of the current array element.

For... in loop statement sample code:

1 <script type = "text/javascript">
2 // create an object myObject and three attributes: name, sex, age
3 var myObject = new Object ();
4
5 myObject. name = "laosan ";
6 myObject. sex = "Male ";
7 myObject. age = "23 ";
8
9 // traverse all attributes of an object
10 for (prop in myObject)
11 {
12 document. write ("attribute" + prop + "'is" + myObject [prop]);
13 document. write ("<br> ");
14}
15 </script>

Use the while and do... while loop statements

The while loop is similar to the for loop. The difference is that the while LOOP does not have a built-in counter or update expression. If you want to control the loop execution of statements or statement blocks, instead of simply running the code n times, you should use a while loop to implement more complex rules.

Note: The while LOOP does not have an explicit built-in counter variable, so it is more likely to generate an infinite loop than other types of loops. In addition, since it is difficult to find out when and where the loop condition is updated, it is easy to write a while loop that actually never updates the condition. Therefore, be especially careful when writing a while loop.

Sample Code of the while LOOP statement:

1 I = 0
2 while (I <= 5)
3 {
4 document. write (I + "<br> ");
5 I ++;
6}

In JScript, The do... while loop is similar to the while loop. The difference is that it always runs at least once because it checks the condition at the end of the loop rather than at the beginning.

Do... while loop statement example:

1 I = 0;
2 do {
3 document. write (I + "<br> ");
4 I ++;
5} while (I <= 5)

Use the break and continue statements

In Javascript, when certain conditions are met, the break statement is used to interrupt a loop. (Please note that you also use the break statement to exit a switch block. See Javascript condition statements ). If it is a for or for... in loop, when updating the counter variable, use the continue statement to skip the remaining code block and directly jump to the next repeating loop.

Break sample code:

1 2 3 <title> A Javascript sample code with a break interrupt loop </title>
4 <script type = "text/javascript">
5 function BreakTest (breakpoint ){
6 var I = 0;
7 var m = 0;
8 while (I <100 ){
9 // interrupt loop when I is equal to breakpoint
10 if (I = breakpoint)
11 break;
12 m = m + I;
13 I ++;
14}
15 return (m );
16}
17 </script>
18 19 <body>
20 <script type = "text/javascript">
21 // set the BreakTest parameter to 23 to obtain the total value from 1 to 22.
22 document. write (BreakTest (23 ))
23 </script>
24 </body>
25

Sample Code of continue:

1 // The script code is used to output an odd number between 1 and 10.
2 var x;
3 for (x = 1; x <10; x ++ ){
4 // If x is divisible by 2, skip the code and start the next repetition;
5 // If x cannot be divisible by 2, execute the following code and output x.
6 if (x % 2 = 0)
7. continue;
8 document. write (x + "<br> ");
9}
Related Article

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.