For loop structure

Order of simple execution

  • The for loop

    for (var i = 0 ; i < 10 ; I++){document.write("a")} //10 line aCopy the code
  • Execution order

    1. var i = 0 ; // declare and assign I = 0 2. If (I <10) {document.write("a")}; 3. Execute i++; If (I <10) {document.write("a")}; 5. Execute i++; I becomes 2 sum up once it doesn't work just jump out of the loop for (;;;) 1. Var I = 0; 2. for (; i<10;) { document.write("a") i++; }Copy the code

Order of simple execution

  • For loop summation

        var count = 0 ;
        for (var i = 0 ; i < 10 ; i++  ){
         count+=i;
         document.write(count)
        }
    Copy the code
  • A number divisible by 3 or 5

         var count = 0 ;
        for (var i = 0 ; i < 10 ; i++  ){
          if(i % 3 == 0 || i % 5 == 0){
                document.write(i +" " )
          }
        }
        //0 3 5 9
    Copy the code

while for

Loop simplified for loop, nothing written before and after
  • The while loop

      var i = 0 ;
        while (i < 10 ){
         document.write(count)
         i++;
        }
    Copy the code
  • While jumps out at 7 and a multiple of 7

    var i = 0 ; While (I < 100) {if (I % 7 = = 0 | | I % 10 = = 7) {document. Write (I + "< br >")} i++} / / in addition to more than 10 7Copy the code
  • The do while loop executes once regardless of whether storage exists or not

       var i = 0 ;
        do {
             document.write(i + "")
        }while (i < 100 )
    Copy the code

The switch case loop executes once whether it exists or not

  • The basic grammar

        <script type="text/javascript"> 
      	var n=3
      	switch(n){
      		case 1:
      		console.log('a');
      		case 2:
      		console.log('b');
      		case 3:
      		console.log('c');
      	}
      </script>
    Copy the code
  • Matching with the switch

    <script type="text/javascript"> // math.sqrt () is used to reduce the number of loops but 1 is neither prime nor composite var n = true switch(n){case true: console.log('a'); case 2: console.log('b'); case 3: console.log('c'); } </script>Copy the code
    Extremely not responsible for printing results A, B, C
Solution adds break
<script type="text/javascript"> // math.sqrt () is used to reduce the number of loops but 1 is neither prime nor composite var n = true switch(n){case true: console.log('a'); break; case 2: console.log('b'); break; case 3: console.log('c'); break; } </script>Copy the code
  • Break Breaks the cycle

    1.1 Reduce coupling degree in switch

    <script type="text/javascript"> var n = true switch(n){ case true: case 2: console.log('a'); break; Case 3:} </script> // value aCopy the code

    1.2 Terminate the loop and jump out directly

  • Continue Breaks out of the current loop and continues with the next loop

        <script type="text/javascript">
      	for(var i = 0 ; i< 100; i++){
      		if(i%7==0||i%10==7){
      			continue;
      		}
      		console.log(i)
      	}
      </script>
    Copy the code

Problem 1.1: Calculate 2 to the NTH power, n can be entered, n is a natural number

Var n = parseInt(window.prompt('input')) var count= 1; for(var i = 0 ; i < n ; i++){ count * = 2; } document.write(count)Copy the code

Problem 1.2: Compute n factorial, n can be entered

        var n = parseInt(window.prompt('input'))
		var count= 1 ;
		for(var i = 1 ; i<=n; i++){
			count*=i;
		}
		document.write(count)
Copy the code

Problem 1.3: Compare the size of a, B, and C and return the maximum value

      <script type="text/javascript">
		var a = parseInt(window.prompt('input'))
		var b = parseInt(prompt('input'))
		var c = parseInt(prompt('input'))
		if(a>b){
			if(a>c){
				document.write(a)
			}else{
				document.write(c)
			}
		}else{
			if(b>c){
				document.write(b)
			}else{
				document.write(c)
			}
		}
		document.write(count)
	 </script>
Copy the code

Problem 1.4: Input three positive integers and output values in reverse

Var n = Number (prompt("input")) Var a = n%10 var b = n%100 - n%10 var c = n - b - a var newa = c - 100 var newb = Var sum = newa + newb + newc document.write(sum)Copy the code

Problem 1.5: Fibonacci sequence first+second=third

      <script type="text/javascript">
		var n = parseInt(window.prompt('请输入数字')) 
		 var first =1;
		     second =1 ;
		     third=0;
		 // 减去两圈
		 // i是循环次数
		 if(n>2){
		 alert(111)
		 for(var i = 0 ; i <n-2;i++){					 				 
		 		 third = first+second;
		  	 	 first = second;
			  	 second = third;
		 	 }
				 document.write(third)
			 }else{
				 document.write(1)
			 }	
	 </script>
Copy the code

Problem 1.5: Print primes up to 100

<script type="text/javascript"> var count = 0; // count for (; i < 100; Var j = 1; var j = 1; j <= i; If (I % j == 0) {count++; If (count == 2) {document.write(I + "<br>")} count = 0} </script>Copy the code

Updated version

<script type="text/javascript"> // math.sqrt () is used to reduce the number of loops but 1 is neither prime nor composite var count = 0; for (var i = 2; i < 100; i++) { for (var j = 1; j <= Math.sqrt(i); j++) { if (i % j == 0) { count++; } } if (count == 1) { document.write(i) } count = 0 } </script>Copy the code