Loop statement (while && do while && for & & loop control statement)

1, while loop statement

1. Loop: execute a piece of code repeatedly

+ the loop in the code is counting

+ determine the number of cycles by changing the number

2. Three elements of circulation

2.1 start: count from what

2.2 end: count to the end

2.3 step length: interval of counting

3.while statement format

3.1 while {code snippet}

3.2 when the conditions are met, execute the code segment in {}

+ after execution, judge the condition again

+ if the conditions are met, execute the code snippet in {} again

+ one analogy until the condition is not met

+ end of cycle

3.3 cycle precautions: the initial value must be changed

3.4 what does the loop provide

+ ability to execute code repeatedly

+ a set of regular numbers

4. Practice

4.1 multiple of 3 between console output 1 ~ 100

    // Scheme 1: basic cycle
    //   1. Get all the numbers between 1 and 100
    //      Cycle start: 1
    //      End of cycle: < = 100
    //      Cycle step: + 1
    var n = 1
    while (n <= 100) {
      // Judge if the current n% 3 = = = 0 is output
      if (n % 3 === 0) console.log(n)
      n++
    }
    // Scheme 2: directly lock the number
    //  The multiple of the first 3 is 3
    var n = 3
    while (n <= 100) {
      console.log(n)

      // Step set to + 3
      n += 3
    }

2, do while loop statement

1.do while

1.1 format: do {code segment} while {condition}

1.2 execution process:

Execute the code once before the condition

+ if the conditions are met, continue to execute the code

+ if the conditions are not met, end the cycle

2. The difference between and while

2.1 When the initial content is within the condition, there is no difference between while and do while loops

2.2 when out of condition

+ while loop will not execute once

+ do while executes the code once

3. Practice

3.1 slap the password at home on the page (enter the correct content and close the prompt pop-up layer, otherwise it will be turned on all the time)

    /*
      Logic to implement business
        1. When opening the page, an input box will pop up to let the user enter the password
        2. Start judging whether your input is correct
          => If not, execute the code that pops up the input box again
          => Judge again
          => If yes, the code that pops up the input box is no longer executed
    */   
do {
      // Pop up input box
      var res = prompt('Do you love me ?')
    } while (res !== 'yes')

    alert('Fuck off')	

3, for loop statement

1. Syntax:

1.1 for (initial variable; condition judgment; modify initial value) {code segment}

2. Practice

2.1 finding cubic self idempotent (daffodil number)

+ guaranteed to be a three digit number

The sum of the three powers of each digit of the + number is equal to this number

for(var i = 100; i <= 999; i++){
    var a = parseInt(i / 100)
    var b = parseInt(i % 100 / 10)
    var c = i % 10
    
    if(a ** 3 + b ** 3 + c ** 3 === i)    console.log( i + "Is a cubic self idempotent")
}

4, Loop control statement

1. break: is a keyword

When this break occurs in the cycle of gear 1.1, the cycle will be ended directly

2. continue: is a keyword

2.1 When you encounter this continue in the loop, you will directly end this time of the loop and go to the next time

3. Loop control tag syntax

3.1 marking can be established at the beginning of the cycle

    for (var i = 1; i <= 5; i++) {
      console.log('I ate the second ' + i + ' A steamed stuffed bun')

      if (i === 3) {
        console.log('i am full')
        // Write a break keyword when needed
        break
      }
    }

3.2 in the loop, the break and continue keywords are marked when they are written

    for (var i = 1; i <= 5; i++) {
      console.log('I picked up the second ' + i + ' A steamed stuffed bun')

      if (i === 3) {
        console.log('The steamed stuffed bun fell to the ground')

        continue
      }

      console.log('I ate the second ' + i + ' A steamed stuffed bun')
      console.log('-------------------------')
    }

3.3 return directly to the marked position

   //Execute break a when I = 3 & & J = 3  
   //End a marked for loop
	a:
    for (var i = 1; i <= 5; i++) {
      console.log('I picked up the second ' + i + ' A steamed stuffed bun')

      // loop nesting 
      b:
      for (var j = 1; j <= 3; j++) {
        console.log('Eat first ' + i + ' The second steamed stuffed bun ' + j + ' mouth')

        if (i === 3 && j === 1) {
          console.log('Half a worm was found')

          // End the marked cycle directly
          break a
        }
      }

      console.log('The first ' + i + ' A steamed stuffed bun is finished')
      console.log('---------------------')
    }

Keywords: Javascript

Added by The Stewart on Thu, 24 Feb 2022 14:03:15 +0200