Nesting of JavaScript double loops

Time:2020-12-5

Nested reverse war class series of JavaScript double loop

loop nesting

The nesting of loops is that a loop contains another complete loop structure. As shown in the figure:

 

Its execution order is from outside to inside, the outer loop will execute once, and the inner loop will execute once completely. After the cycle body in the outer cycle has been executed, it will return to the outer cycle to continue the execution until the end of the outer cycle.

Application: use loop nesting to output positive and negative pyramid code diagram

var n= window.prompt (“please enter the number of rows”);

For (I = 1; I < = n; I + +) {/ / control the number of rows / / positive pyramid

For (J = 1; J < = n-i; j + +) {/ / print each line of space

                document.write(” “);

 

            }

For (k = 1; K < = I; K + +) {/ / print asterisks per line

                document.write(“* “);

            }

            document.write(“
“);

 

        } 

For (I = 1; I < = n; I + +) {/ / control the number of rows / / to the pyramid

For (J = 1; J < = I-1; j + +) {/ / print each line of space

                document.write(” “);

 

            }

For (k = 1; K < = n-i + 1; K + +) {/ / print asterisks per line

                document.write(“* “);

            }

            document.write(“
“);

 

        } 

 

design sketch:

 

Special attention:
1. When the outer loop performs one cycle, the inner loop performs all the loops;
2. When the inner loop is executed, the value of the outer loop remains unchanged;
3. In other words, I is often related to J.

summary

This loop nesting is still very important. There are still a lot of places to use in JavaScript. We still need to continue to learn. I hope there is a big guy to teach me and help me, Xiaobai. Let’s learn together!

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]