Loop structure in JS

Keywords: Javascript IE

while loop:
Syntax:
  while (expression) {
  code...
  }
Rules of implementation:
The expression (expression) is judged first, and if it is true, the code is executed in a loop, and then the judgement is made...
If it is false, it will not enter.
Note: If expression is false for the first time, it will not enter the loop
Note: we must modify the contents of expression in the code part, so that one day the expression will be false. Otherwise, it's a dead cycle.

 

 

do while loop:
Syntax:
    do {
    code...
   } while (expression);
Rules of implementation:
Execute the code once before making a decision. Unlike the while loop, do while always executes code once regardless of the condition.

Note that the same as the while loop, it is necessary to modify the contents of expression in the code part, so that one day the expression will be false. Otherwise, it's a dead cycle.

 

 

for cycle:
Syntax:
for (initialization value; determination condition; modification of initialization value){
    code...
  }

Implementation process:

for (initial value 1; cyclic condition 2; change initial value 4){
Code 3 to be executed
  }
The order of the cycles:
  1 2 3 4
  2 3 4
  2 3 4
  2 3 4
  ......
Until the end

 

Understand the loop structure. Let's look at some common examples of branch structure learning.

  1. Output all even numbers within 0 to 100
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <script>
             for (var i = 0; i <= 100; i++) {
                // Determine if the number is even
                if (i % 2 === 0) {
                    // Decision: If you can take the remaining 0 of 2, you can say that the multiple of 2 is even.
                    console.log(i);
                }
            }
        </script>  
    </body>
    </html>

2. Calculate all leap years from 1000 to 2000

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
           // Calculating conditions for leap years: divisible by 4 but not by 100 or by 400
        // Define how many leap years a counter has found accumulatively
        var count = 0;
        for (var i = 1000; i <= 2000; i++) {
            if (i % 4 === 0 && i % 100 != 0 || i % 400 === 0) {
                // It's a leap year and the counter accumulates.
                count++;
                // console.log("Ad" + i + "Year is a leap year.");
                document.write("Ad" + i + "Year is a leap year.");
                if (count % 8 === 0) {
                    document.write("</br>");
                }
            }
        }
        // Idea: From 1000 to 2000, if it's leap year, we need to input it into the page.
        // Every eight lines, we have to find a way to accumulate how many leap years.
        // So we can define a variable specifically for accumulating the number of leap years found. 
        // So definition count Specialized accumulation
        // whenever count Execute once when it is a multiple of 8 or 8 document.write("</br>")
    </script>
</body>
</html>

3. Find out the number of all daffodils in the range of 100-999 and output them.

         

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // Narcissus Number: The cube of each digit of a three-digit number and the number equal to itself are called Narcissus Number
        // ex: 153 = 1 * 1 * 1  + 5 * 5 * 5 + 3 * 3 * 3  125 + 27 + 1

        for (var i = 100; i <= 999; i++) {
            // Decide one by one whether each number is Narcissus number
            // Firstly, get the number of digits pair 10 to find the remainder
            var ge = i % 10;
            // Get another ten digits
            var shi = parseInt(i % 100 / 10);
            // Get another hundred digits
            var bai = parseInt(i / 100);
            // console.log(i + "The hundred digits of this number are" + bai + "The ten digit is" + shi + "Single digit is" + ge);
            if (ge * ge * ge + shi * shi * shi + bai * bai * bai === i) {
                console.log(i + "It's a number of daffodils.");
            } 
        }
    </script>
</body>
</html>    

4. Find all prime numbers within 0-100

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // Prime number: Except for 1 and itself, there is no other number that can divide it entirely into such numbers as prime numbers.
        // Define a number casually 
        var num = +prompt("Please enter a positive integer greater than 2");
        // Define a marker variable. This marker variable is responsible for marking whether there are divisible numbers in the loop.
        var isZ = true;
        for (var i = 2; i < num; i++) {
            if (num % i === 0) {
                // It means that if there is a number between 2 and 10 that divides 11, then it is not a prime number. 
                isZ = false;
            }
        }
        // determine isZ If so true Explain the cycle if If a statement is not entered, it means that there are no numbers divisible by 11. 
        if (isZ) {
            alert(num + "Prime number");
        } else {
            alert(num + "Not prime numbers.");
        }
    </script>
</body>
</html>

Posted by esport on Thu, 03 Oct 2019 00:29:26 -0700