“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

First, loop statements

Let’s start with a little game where a loop in place executes a block of code a specified number of times. Loops are handy if we need to run the same code over and over again, with different values each time.

Different types of loops

JavaScript supports different types of loops:

  • For – Loop a code block a certain number of times
  • While – A block of code that loops when the specified condition is true
  • Do /while – Also loops the specified code block when the specified condition is true
  • For/In – Loops over the properties of the object
/* 1. Understand the loop structure 2. Use the loop structure to develop simple cases 3
        /* Loop structure: loop: repeat the common JS loop, for loop, while loop, do-while loop */
        /* execute the first loop by checking the condition. 1. For loop: output 1~10 digits to the web page loop step: start loop var I = 1; // Initialize a variable, I =1 ------ the first loop begins --------- I <= 10; // execute the code in the loop braces i++; // I increment by 1 ------- end of first loop, start next loop -------- I <= 10; // Determine the code in the conditional execution loop. ------- the second loop ends and the next loop -------- */ begins
        for(var i = 1; i <= 10; i++) {
            // Output content directly to the web page
            document.write("<h1>for:" + i + "</h1>");// Use write(data) to write the data in parentheses to the page for display
        }
        /* execute the first loop by checking the condition. 2. While loop */

        var w = 1;
        while(w <= 10) {
            document.write("<h2>while:" + w + "</h2>");

            w++;
        }

        /* 3. Do -while loop: a special loop executes the code in the loop directly for the first time, and then determines the condition to execute the loop; Does not satisfy the loop */
        var wh = 1;
        do{
            document.write("<h3>do-while:" + wh + "</h3>");
            wh ++;
        }while(wh <= 10);
Copy the code

Second, for loop

Our class classmate XX was late, punish him in place to turn 10 laps, no turn of the time to remember as 0, turn the first time of heart meditation 1 circle, turn the second circle of heart meditation 2 laps,… Stop on the tenth lap

Grammar:

for(statement1; statements2; statements3) {block of code executed}// index = 0 index <= 100; index++
  /** statement 1 executes before the loop (code block) begins. 2 Defines the conditional statement that runs the loop (code block). 3 Executes after the loop (code block) has been executed
Copy the code

Example:

for (var i=0; i<5; i++){
  console.log('number is' + i);
 }
Copy the code

While loop

The while is a pre-test loop statement that executes a block of code as long as the condition is true. Grammar:

while(Condition){code to execute}Copy the code

Example:

while (i<5){
    text = text + "The number is " + i + "\n";
    i++;
}
console.log(text);

Copy the code

Do /while loop

The while loop, on the other hand, is a pre-test loop where the exit condition is evaluated before the code in the loop is executed, so if the condition is not met, the code inside the loop may never be executed. Grammar:

do{code to execute}while(conditions);Copy the code

Example:

do {
  text = text + "The number is " + i + "<br>";
  i++;
}
while (i<5);
console.log(text);

Copy the code

For /in loop

For /in loops are generally used for traversal, traversing the properties of an object, which is described briefly here, and will be used more later on in objects and arrays.

var person={fname:"John".lname:"Doe".age:25};

for (key in person)  // key is the attribute name
{
    text = text + person[key];
}

console.log(text);
Copy the code

Break and continue

The break statement is used to break out of the loop, and the continue statement is used to skip an iteration of the loop. To put it simply, a break is a direct break out of the loop, and subsequent loops are not executed if the conditions are met. Continue breaks out of the loop and continues with subsequent loops that meet the criteria. Example:

for (i=0; i<10; i++){if (i==3) {break;
  }
  text = text + "The number is " + i + "\n";
}
console.log(text);

Copy the code

The continue statement interrupts the iteration in the loop, if the specified condition occurs, and continues to the next iteration in the loop.

for (i=0; i<=10; i++) {if (i==3) continue;
  text = text + "The number is " + i + "\n";
 }
console.log(text);

Copy the code

conclusion

JavaScript branch statements will continue to be updated... Original is not easy, looking forward to your likes attention and forwarding comments 😜😜😜Copy the code