JavaScript Flow control – loop
Loop profile
- There are three main types of loops in JS:
- The for loop
- The while loop
- The do while loop
- A set of statements that are executed repeatedly is called the body of a loop, and whether they can be executed continuously depends on the termination conditions of the loop
- A statement consisting of a loop body and a loop termination condition is called a loop statement
The for loop
-
Grammatical structure
-
forInitialize a variable; Conditional expression; Operation expression) {// loop statements } // To initialize a variable, we use var to declare a normal variable, which is executed only once before the entire for loop is completed // The conditional expression determines whether or not we execute each time through the loop, terminating the condition with true and continuing execution // Operation expression, the code executed at the end of each loop Copy the code
-
-
Chrome breakpoint test
- Breakpoint test is to set a breakpoint on one line of your program, and the program will stop on this line during debugging.
- Breakpoint testing can help us observe the program as it runs
-
The test method
- In Chrome, call Up Sources
- Find the test file
- Set a breakpoint on a line of the program
- Watch changes to monitor variables through watch
- f11You can make the program run step by step
-
Execute the same code
-
Execute different code
-
A for loop can do something repeated something repeated
-
Double for loop
-
Syntactic structure conditional expressions
-
forInitialize a variable; Conditional expression; Operation expression) {forInitialize a variable; Conditional expression; Operation expression) {// loop statements}}// Each time the outer loop is executed, the inner loop executes until the conditional expression is false Copy the code
-
-
The while loop
-
Grammatical structure:
-
while(conditional expression) {/ / the loop body // Be sure to use a timer, otherwise you will enter an infinite loop } // If the conditional expression is judged to be true, the loop body continues // You can make some complicated judgments, but you can use the for loop instead Copy the code
-
The do while loop
-
Grammatical structure
-
do { // The body of the loop is executed the first time regardless of whether the conditional expression is true } while(Conditional expression)Copy the code
-
continue break
- Continue Exits the loop and executes the rest of the loop
- Break Breaks the entire loop
JavaScript naming conventions and syntax formats
- The logo is clearly standardized
- Variable names are usually nouns
- Function names usually use verbs
- Operator specification
- Write a space to the left and right of the operator