1 - cycle
1. for loop
- Grammatical structure
- for repeatedly executes some code, usually related to counting
// 2. for syntax structure for(initialize variable; Conditional expression; Operation expression ){ //Circulatory body }
- Initialization variable: a common variable declared with var, which is usually used as a counter
- Conditional expression: used to determine whether each cycle is terminated or not
- Operation expression: it is the code executed at the end of each cycle. It is often used to update (increment or decrement) counter variables
Execution process:
for (var i = 1; i <= 100; i++) { console.log('how are you'); }
- First, execute the counter variable var i = 1 But this sentence only executes index once in for
- Go to I < = 100 to judge whether the conditions are met. If the conditions are met, execute the loop body and exit the loop if the conditions are not met
- Finally, I + + is executed. I + + is the code written separately. The first round of increment ends
- Then execute I < = 100. If the conditions are met, execute the loop body. If the conditions are not met, exit the second round of the loop
Breakpoint debugging:
Help observe the running process of the program
technological process:
- Press F12 in the browser – > sources -- > to find the file to be debugged – > set a breakpoint on a line of the program
- Watch: monitoring. You can monitor the change of variable value through watch. It is very common.
- Press F11 to execute the program step by step, and let the program execute line by line. At this time, observe the change of the value of the variable in the watch.
The for loop repeats the same code
// Specify the number of entries var num = prompt('Please enter the number of times:'); for ( var i = 1 ; i <= num; i++) { console.log('Pleasant Sheep'); }
The for loop repeats different code
For example, output 1 to 100 years old:
for (var i = 1; i <= 100; i++) { console.log('This man this year' + i + 'Years old'); }
The for loop repeats different codes and outputs different results
For example, output 1 to 100 years old, and prompt birth and death
// Other statements can be added to for for (var i = 1; i <= 100; i++) { if (i == 1) { console.log('This man is one year old. He was born'); } else if (i == 100) { console.log('This man is 100 years old. He's dead'); } else { console.log('This man this year' + i + 'Years old'); } }
for loop arithmetic operation
// 1. Find the sum of all numbers between 1 and 100 var num = 0; for (var i = 1; i <= 100; i++) { num = num + i; } console.log(num / 100); // 2. Find the sum of all even and odd numbers between 1-100 var even = 0; var odd = 0; var age = 0; for (var i = 1; i <= 100; i++) { if (i % 2 == 0) { even = even + i; } else { odd = odd + i; } } age = odd + even; console.log(even); console.log(odd); console.log(age); // 3. Find the sum of all numbers between 1 and 100 that can be divided by 3 var asd = 0 for (var i = 1; i <= 100; i++) { if (i % 3 == 0) { asd = asd + i; } } console.log(asd);
for loop append string
var sum = ''; for (var i = 1; i <= 5; i++) { sum = sum + '♥'; } console.log(sum);
var num = prompt('Please enter the number of stars'); var srt = ''; for (var i = 1; i <= num; i++) { srt = srt + '♥'; } console.log(srt);
2. Double cycle
summary
Loop nesting refers to defining the syntax structure of a loop statement in a loop statement
grammar
for (Initial of external circulation; Conditions of external circulation; Operation expression of outer loop) { for (Initial of internal circulation; Conditions of internal circulation; Inner loop operation expression) { Code to execute; } }
- The inner loop can be regarded as the loop body statement of the outer loop
- The execution order of the inner loop should also follow the execution order of the for loop
- The outer loop is executed once, and the inner loop is executed all times
Double for loop append string
var age = ''; for (var i = 1; i <= 10; i++) { for (var j = 1; j <= i; j++) { age = age + '★'; } age = age + '\n'; } alert(age); //★ //★★ //★★★ //★★★★ //★★★★★ //★★★★★★
var age = ""; for (var i = 1; i <= 10; i++) { for (var j = i; j <= 10; j++) { age = age + '★'; } age = age + '\n'; } alert(age); //★★★★★★ //★★★★★ //★★★★ //★★★ //★★ //★
var sum = ''; for (var i = 1; i <= 9; i++) { for (var j = 1; j <= i; j++) { //1*2=2 sum += j + '×' + i + '=' + j * i + '\t'; // '\ t' = Space } sum = sum + '\n'; } console.log(sum); /* 1×1 = 1 1×2 = 2 2×2 = 4 1×3 = 3 2×3 = 6 3×3 = 9 1×4 = 4 2×4 = 8 3×4 = 12 4×4 = 16 1×5 = 5 2×5 = 10 3×5 = 15 4×5 = 20 5×5 = 25 1×6 = 6 2×6 = 12 3×6 = 18 4×6 = 24 5×6 = 30 6×6 = 36 1×7 = 7 2×7 = 14 3×7 = 21 4×7 = 28 5×7 = 35 6×7 = 42 7×7 = 49 1×8 = 8 2×8 = 16 3×8 = 24 4×8 = 32 5×8 = 40 6×8 = 48 7×8 = 56 8×8 = 64 1×9 = 9 2×9 = 18 3×9 = 27 4×9 = 36 5×9 = 45 6×9 = 54 7×9 = 63 8×9 = 72 9×9 = 81 */
Summary
- The for loop can repeat some of the same code
- The for loop can repeat a little different code because we have counters
- The for loop can repeat certain operations, such as arithmetic operators and addition operations
- As the demand increases, the double for loop can do more and better results
- Double for loop, the outer loop is executed once, and the inner for loop is executed completely
- The for loop is a loop whose condition is directly related to the number
3. while loop
Structure:
while (Conditional expression) { // Loop body code }
Implementation idea:
- 1. Execute the conditional expression first. If the result is true, execute the loop body code; If false, exit the loop and execute the following code
- 2 execute loop body code
- 3 after the execution of the loop body code, the program will continue to judge the execution condition expression. If the condition is still true, the loop body will continue to be executed until the loop condition is false
be careful:
- When using a while loop, you must be aware that it must have exit conditions, otherwise it will become an endless loop
4. Do while loop
Structure:
do { // Loop body code - repeats the loop body code when the conditional expression is true } while(Conditional expression);
Implementation ideas
- Execute the loop body code once first
- Then execute the conditional expression. If the result is true, continue to execute the loop body code. If it is false, exit the loop and continue to execute the following code
Note: execute the loop body first, and then judge. The do... while loop statement will execute the loop body code at least once
5. Cycle summary
- Three loops: for, while, do while
- In most cases, the three can replace each other
- It can be used to record the number of times or related to numbers. All three can be used. I prefer to use for
- While and do while can make more complex judgments and are more flexible than for
- The execution sequence of while and do while is different. While is judged before execution, and do while is judged after execution.
- The execution times of while and do while are different. Do while will execute the loop body at least once, while while while may not execute at all
6. continue,break
continue
It is used to immediately jump out of this loop and continue the next loop (the code after continue in the body of this loop will be executed less once).
For example:
// 1. Find the sum of integers between 1 and 100, except that they can be divided by 7 var sum = 0; for (var i = 1; i <= 100; i++) { if (i % 7 == 0) { continue; } sum += i; } console.log(sum);
break
Used to immediately jump out of the entire loop (end of loop).
for example
// 1. Find the sum of integers between 1 and 100, except that they can be divided by 7 var sum = 0; for (var i = 1; i <= 100; i++) { if (i % 7 == 0) { break ; } sum += i; } console.log(sum);
2 - code specification
- The naming of variables and functions must be meaningful
- Variable names are generally nouns
- The names of functions are usually verbs
- Leave a space on the left and right sides of the operator