JavaScript loop/Select structure

Source: Internet
Author: User

Like other languages, JavaScript has a looping structure and a selection structure.

Select structure:

    1. if (condition) {
         //EXECUTE statement that satisfies the condition
    2. if (condition) {
         //EXECUTE statement that satisfies the condition
      } else {
          ; Execute statement without condition
      }
    3. if (condition 1) {
         //EXECUTE statement that satisfies condition 1
      } else if (condition 2) {
         //Meet Condition 2: N.. The EXECUTE statement
      } else{
         //EXECUTE statement that does not meet the above conditions
      }
    4. switch (condition) {     Case matching result 1:
             //Match successful execution statement
            & nbsp break;   //exit switch
          Case matching result 2:
             //Match Successful execution statement
      & nbsp;       break;
          ....
          default:   //The above conditions are not successful
             //above conditions match unsuccessful execution statement
      }
Loop structure:

  1. For (initial value; number of cycles; self-increment (self-decrement)) {
    Loop body
    }
    <script type= "Text/javascript" >for (var i=0;i<10;i++) {   document.write (i + "&nbsp;");  Output results: 0 1 2 3 4 5 6 7 8 9}</script>
  2. for-in loops, primarily for loop arrays, but not commonly used, because this outputs hidden properties
    Specific Use method:
    <script type= "Text/javascript" >var array = [1,2,3,4,5,6];var i = 0;for (i in array) {  document.write (Array[i] + ' &nbsp; ');  i++;} Output is 1 2 3 4 5 6 </script>



    The traversal of an array mainly uses a for loop instead of using the for
  3. While loop
    <script type= "Text/javascript" >var array = [1,2,3,4,5,6];var i = 0;while (i<array.length) {  document.write (Array[i] + ' &nbsp; ');  i++;} Output is 1 2 3 4 5 6 </script>
  4. Do...while Cycle
    <script type= "Text/javascript" >var array = [1,2,3,4,5,6];var i = 0;do{  document.write (array[i] + ' &nbsp; ');  i++;} while (i<0);//output result is 1</script>
The difference between the for traversal array and the for-in traversal array
  1. An array's traversal can only be an indexed array
  2. the For loop will only iterate through the elements in the array, and for in will traverse the other properties
    Such as:
    <script type= "Text/javascript" >var array = [1,2,3,4,5,6];array.name = ' Zhang San '; for (var i = 0;i<array.length;i++) { C4/>document.write (array[i]+ ' &nbsp; ');} 1 2 3 4 5 6document.write (' <br/> '); var i=0;for (i in array) {  document.write (array[i]+ ' &nbsp; ');  i++;} 1 2 3 4 5 6 Zhang San </script>



The difference between a while loop and a do...while loop
  1. The while loop is not looped if the condition is not satisfied
  2. The Do...while loop is that if the condition is not met, it will still be executed once, as shown in the example above, the initial value of I is 0, does not satisfy the i<0 condition, but still outputs a value

JavaScript loop/Select structure

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.