05-02 JavaScript process control - loop statement

target

  • Be able to say the purpose of the cycle
  • Be able to say the execution process of the for loop
  • Be able to use power-off debugging to observe the execution of code
  • Be able to use the for loop to complete cases such as cumulative summation
  • Cases where a double for loop can be used to complete the multiplication table
  • Be able to tell the difference between a while loop and a do while loop
  • Be able to tell the difference between break and continue

Purpose of circulation

Some code can be executed repeatedly

Loop in js

There are three loop statements in js:

  • for loop
  • while loop
  • do... while loop
    A group of repeatedly executed statements is called the loop body. Whether they can be executed repeatedly depends on the termination condition of the loop

for loop

1.for repeatedly executing some code is usually related to counting
2.for syntax structure

for(initialize variable;Conditional expression;Operation expression){
	//Circulatory body
}
//>Initialization variable is a common variable declared with var, which is usually used as a counter
//4. Conditional expression is used to determine whether each cycle continues to be executed or terminated
//5. The code executed at the end of each loop of the operation expression is often used to update (increment or decrement) our counter variables


//6. Code experience: repeatedly print 100 sentences hello

for(i = 1; i<=100;i++){
	console.log("Hello")
}
1.First execute the counter variable inside var i= 1.But this sentence is for It's only executed once index
2.go i<=100 Judge whether the conditions are met. If they are met, execute them. If not, exit the loop body
3.Finally, execute i++ i++Is a separate code increment
4.Then execute i<= 100 If the conditions are met, execute the loop. If the conditions are not met, exit the loop 

breakpoint debugging

Help us observe the running process of the program

The for loop executes different code

Because of the existence of counter variable i, the value of i will change every cycle

Case: Print this person from 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 some of the same actions

Case: seeking 1-100 Sum of integers
let sum = 0;//Defines a variable that receives a sum
for(let num = 1;num <= 100;num++){
    sum = sum+num;
}
console.log(sum);//Print 5050

case

Print a line of five stars
//Define an empty string for the column
let str = "";
for(let i= 1;i<=5;i++){
    str = str+"※"
}
console.log(str);

//If it needs to be determined according to the number entered by the user, it can be adjusted slightly
let str = "";
let num = prompt('Please enter the number of stars:');
for (let i = 0; i < num; i++) {
    str = str + "※"
}
console.log(str);

Double for loop

definition:
Defining the syntax structure of a loop statement in a loop statement is called a double for loop

Grammatical structure
for(Initialization variables of the outer layer; Conditional expression of outer layer; Operation expression of outer layer){
	for(Initialization variables of the inner layer; Conditional expression of inner layer; Inner layer operation expression){
		//Execute statement
	}
}
1.We can think of the inner loop as an outer loop statement
2.The outer loop executes once, and the inner loop executes all
3.If you can, please write a 99 multiplication table case according to the above content.

for loop 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 effects
  • Double for loop, the outer for 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
  • Analysis is more important than writing code
  • Some core algorithms are unexpected, but we should learn to analyze its execution process
  • Draw inferences from one instance. I often summarize and do some similar cases

while Loop

while
When the conditional expression is true, the loop executes the specified piece of code until the expression is not true.

Grammatical structure
while(Conditional expression){  
	//Loop body code
}
Implementation idea:
When the result of the conditional expression is true,Execute the loop body, otherwise exit the loop.
There should be counter initialization variables
 There should also be an operation expression to complete the update of the counter and prevent dead circulation

do while loop

A variant of the while statement, which executes the code block first, and then judges the conditional expression

Syntax structure:
do{
	//Circulatory body
}while(Conditional expression)

Implementation idea:
do while Execute the loop body first and then judge the condition. If the condition expression structure is true, continue to execute the loop body, otherwise exit the loop.
do while The loop statement executes the loop body code at least once

Summary of do while loop

  • In most cases, they can be used instead of each other
  • If it is used to count times, it is related to numbers. The three are basically the same, but for is more commonly used
  • while and do... while can make more complex judgment conditions and be more flexible
  • While is executed after judgment. It may not be executed once. do while is executed before judgment. It is executed at least once.
  • In practice, we often use the for loop.

continue break keyword

continue

The continue keyword is used to immediately jump out of this loop and continue the next loop. The code after this loop continues will be executed less than once.

Take a chestnut
//Find the sum of integers between 1 and 100 except those divided by 7
let sum = 0;
for(i=1;i<=100;i++){
    if (i % 7 == 0) {
        continue;
    }
    sum += i;
}
console.log(sum);

break

The break keyword is used to immediately jump out of the entire loop (end of loop)
For example, if you eat five apples, you will find half an insect in the fourth one, and you won't eat the rest.

Take a chestnut
for (let i = 1; i<=100;i++){
        if (i == 3) {
            break;
        }
        console.log('I'm eating the third'+i+'A steamed stuffed bun');
    }

Keywords: Javascript

Added by gtanzer on Wed, 29 Dec 2021 20:18:33 +0200