js - loop structure

Table of contents

1. for loop

    grammar

​ The execution flow of the for loop

2. for in loop

Commonly used for arrays and objects

3. while statement 

grammar

​Everest small practice

4. do-while statement

Execution flow of do-while loop

5. Double for loop


1. for loop

    grammar:

                       for (initialization statement; conditional judgment; conditional control statement) {

                                      loop execution statement

                        }

 The execution flow of the for loop:

①Execute the initialization statement

② Execute the conditional judgment statement to see if the result is true or false

     If false, end the loop

     If true, continue looping

③ Execute the loop body statement

④Execute conditional control statement

⑤ Go back to ② to continue the cycle


2. for in loop

Commonly used for arrays and objects

Syntax:            
                for (variable in object) {                             loop execution statement

                              }


3. while statement 

grammar:

                       initialization statement

                       while (conditional judgment statement) {

                                       loop statement;

                                       conditional control statement;

                                }

Small exercises on Mount Everest:

    <script>
        // 定义一个计数器,初始值为0
        let sum = 0;
        // 定义纸张的厚度
        let paper = 0.1;
        // 定于珠峰的高度
        let zf = 8844430;
        // 循环条件:纸张的高度小于等于珠峰的高度才能继续执行
        while(paper <= zf){
            // 循环执行的过程中,每次纸张的折叠,厚度加倍
            paper *= 2;
            // 对应折叠多少次
            sum++
        }
        document.write('需要折叠: '+ sum +'次')
    </script>

4. do-while statement

 grammar:                 

               initial judgment

             do{

                    loop statement;

                    conditional control statement;

                }while (conditional judgment statement) ;

The execution flow of the do-while loop:

    The program is executed from top to bottom

    ①Execute the initialization statement

    ② Execute the loop body statement

    ③Execute conditional control statement

    ④ Execute the conditional judgment statement to see if the result is true or false

            If false, end the loop

            If true, continue looping

    ⑤ Go back to ② to continue the cycle 

  <script>
        let i = 1;
        do{
            document.write(i+'<br>')
            i++
        }while(i<=5)
  </script>

5. Double for loop

 Each loop can be nested with each other, generally no more than three layers

★The outer loop variable changes once, and the inner loop variable needs to be changed once

The following is a small practice of the ninety-nine multiplication table:

<script>
         for(var i = 1;i <= 9;i++){
            for(var j = 1;j <= i;j++){
                if((i*j)<10){
                    document.write(j + '*' + i + '=' + i * j +'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
                }
                    document.write(j + '*' + i + '=' + i * j +'&nbsp;&nbsp;&nbsp;')
            }       document.write('<br>')
        }
    </script>

Supongo que te gusta

Origin blog.csdn.net/weixin_68485297/article/details/124323108
Recomendado
Clasificación