For
Loops a section of code to a specified number of times
While
Loops execute code when the specified condition is true
For loop
Use a For loop when the number of times the script is run is determined.
Grammar:
for (variable = start value; variable <= end value; variable = variable + step value)
{
Code that needs to be executed
}
Instance:
Explanation: The following example defines a loop program in which the starting value of I is 0. Each time the loop is executed, the value of I is incremented by 1, and the loop runs until I equals 10.
Note: The stepping value can be negative. If the step value is negative, you need to adjust the comparison operator in the for Declaration.
The code is as follows |
Copy Code |
<body> <script type= "Text/javascript" > var i=0for (i=0;i<=10;i++) { document.write ("The number is" + i) document.write ("<br/>") } </script> </body> The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9 The number is 10 |
The while loop is used to loop through code when the specified condition is true.
Grammar:
while (variable <= end value)
{
Code that needs to be executed
}
Note: In addition to <=, you can also use other comparison operators.
Instance:
Explanation: The following example defines a loop program, and the starting value of the parameter I of this loop is 0. The program runs repeatedly until I is greater than 10. The step value of I is 1.
The code is as follows |
Copy Code |
<body> <script type= "Text/javascript" > var i=0while (i<=10) { document.write ("The number is" + i) document.write ("<br/>") I=i+1 } </script> </body>
Results: The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9 The number is 10 |
Do...while Cycle
The Do...while loop is a variant of the while loop. The Loop program first executes the code once, and then it continues the loop when the specified condition is true. So it can be said that the Do...while loop executes the code at least once, even if the condition is false, because the code is executed before the condition is validated.
Grammar:
do {
Code that needs to be executed
}while
(Variable <= end value) instance:
The code is as follows |
Copy Code |
<body> <script type= "Text/javascript" > var i=0do { document.write ("The number is" + i) document.write ("<br/>") I=i+1 } while (i<0) </script> </body>
Results: The number is 0 |
How to exit the loop, in JS we can use break and continue to exit the loop
There are two special types of statements that can be used within loops: break and continue.
Break
The break command terminates the loop's operation, and then continues to execute the code after the loop (if there is code after the loop).
Instance:
The code is as follows |
Copy Code |
<body> <script type= "Text/javascript" > var i=0 for (i=0;i<=10;i++) { if (i==3) {break} document.write (' The number is ' + i) document.write ("<br/>") } </script> </body>
Results: The number is 0 The number is 1 The number is 2 |
Continue
The continue command terminates the current loop and then continues running from the next value.
Instance:
The code is as follows |
Copy Code |
<body> <script type= "Text/javascript" > var i=0 for (i=0;i<=10;i++) { if (i==3) {continue} document.write ("The number is" + i) document.write ("<br/>") } </script> </body>
Results: The number is 0 The number is 1 The number is 2 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9 The number is 10 |