For statement:
1 <script>2 /*for (EXP1;EXP2;EXP3) {3 loop body; 4 }5 EXP1: Unconditionally executes the first expression 6 EXP2: Determines whether the loop body can be executed 7 EXP3: Do an incremental operation/ 8 </script>
We want to print a word like: document.write ("Welcome to Ziksang Blog")
If we want to print the same 100 sentences, we have to print 100 times in this way
If you use a For loop, consider the following example
1 <script>2for (var i = 1; i<=100; i++) {3 document.write ("Welcome to Ziksang Blog")4 }5 </script>
If we want to print 1~100 or 100~1, take a look at the example below
1 //If we don't use a for loop, we're going to do it 100 times.2document.write ("0")3 //........ Perform 100 operations4 for(vari = 0; i<=100; i++) {//from 0 to5 document.write (i)6 }7 for(vari = 100; i>=0; i--) {//from 100 to 08 document.write (i)9}
Print the sum of 1~100
1 <script>2 var sum = 03for (var i = 1;i<=100; i++) {4 sum + =I5 }6 document.write (sum)7 </script>
We use the For statement to find all the odd and even numbers of a 1~100
1 <script>2for (var i =1;i<=100;i++) {3 if(i %2 = = 1) { //Use the IF statement to determine the odd number, if it is odd, execute the following code block statement 4 document.write (i) 5 } //Returns all odd 6 } 7 </script>
1<script>2 for(varI =1;i<=100;i++){3 if(i%2 = = 1) {//use a judgment statement to determine if it is odd, if it is odd to return True, execute a code block statement, continue jumps out of the loop, and proceeds to the next loop4 Continue5 }6 document.write (i)// return all even 7 }8</script>
We use break to insert a for loop to see the effect
1<script>2 for(vari = 0; I <10;i++){3 if(i==3){4 Break //jump out of loop when i=3, stop looping5 }6document.write (i)//at this point I output is 0,1,27 }8</script>
We use continue to insert a For loop to see the effect
1<script>2 3 for(vari = 0; I <10;i++){4 if(i==3){5 Continue //when i=3 jump out of the loop statement, continue to start the loop, the direct execution of the flase (i<10);6 }7document.write (i)//at this point I output is 0,1,2,4,5,6,7,8,98 }9</script>
Nested loops for a for statement
1<script>2 for(varI =1;i<3;i++){3document.write ("Outer loop" +i+ "times" + "</br>")//execute the outer statement first4 //execute the outer statement again5 for(varJ =1;j<=3;j++){6document.write ("Inner Loop" +j+ "times" + "</br>")//execute the inner statement again7}//execute the inner statement again8document.write ("//the last direct judgment is false .9 }Ten One</script>
We use a For statement to loop through a 3-line 30-case table with the following code:
1<table border= "1" cellpadding= "0" bgcolor= "Aqua" width= "80%" >2<script>3 for(vari = 1; i<=3;i++) {//We first execute the first line, the so-called execution once, then execute the second line, the so-called second4document.write ("<tr>")5 for(varJ=1; j<=30;j++) {//30 cases were circulated and 30 cases were recycled.6document.write ("<td>x</td>")7 }8document.write ("</tr>")9 }Ten</script> One</table>
Once again, we will strengthen the function above, using a for statement to loop a 99 multiplication table, the code is as follows:
1<table border= "1" cellpadding= "0" bgcolor= "Aqua" width= "40%" >2<script>3 for(varI =1;i<=9;i++){4document.write ("<tr>")//First line <tr>5 for(varj = 1; j<=i;j++) {//<td>1*1=1</td>6document.write ("<td>" +i+ "*" +j+ "=" + (i*j) + "</td>")7 }8document.write ("</tr>")//</tr>9}//keep doing this loop until the condition is falseTen</script> One</table>
Process Control-loop (for) statements in JS