A javascript loop statement is one of the commonly used statement blocks in javascript. It is used to execute a code block cyclically when a condition is met until the condition is no longer true. When we need to execute some code blocks multiple times
A javascript loop statement is one of the commonly used statement blocks in javascript. It is used to execute a code block cyclically when a condition is met until the condition is no longer true. When we need to execute some code blocks repeatedly multiple times, some of the variables or some changes in each execution can be implemented using loop statements.
Next, let's learn how to cyclically execute a code block for a specified number of times.
- For (var I = 0; I <5; I ++ ){
- // Record "test 0", "Test 1 ″,..., "Test 4". console. log ("test" + I );
- }
Note: In this loop, even if var is used to define the variable I, I is not a global scope. More Scope knowledge will be detailed in the scope chapter.
For Loop
A For loop consists of four statements with the following structure:
- For ([initialisation]; [conditional]; [iteration]) {
- [LoopBody]
- }
Initialisation: An initialization statement that is executed only once before the start of the loop and declares any variables.
Conditional: A Condition Statement that is executed before each cycle to determine whether the cycle meets the conditions. If falsy is returned, the loop ends.
Iteration: An iteration statement that changes the variable value when each loop is completed. A counter is usually used to control the number of cycles.
LoopBody: the loop body Statement, which is the code to be run in each loop. It is a series of statements that need to be executed that are enclosed by curly braces.
The following is an example of a typical For loop:
- For (var I = 0, limit = 100; I <limit; I ++ ){
- // This code will be executed 100 times. console. log ("current" + I );
- // Note: the last record is "Currently 99 ".
- }
While Loop
A While LOOP is similar to an If statement. The difference is that While statements always execute code blocks until the specified condition is "falsy.
- While ([conditional]) {
- [LoopBody]
- }
The following is an example of a typical while loop:
- Var I = 0;
- While (I <100 ){
- // This code will be executed 100 times.
- Console. log ("current is" + I );
- I ++; // I increments
- }
Note: This counter is in the loop body.
The following is an example of combining counters and conditions:
- Var I =-1;
- While (++ I <100 ){
- // This code will be executed 100 times. console. log ("current" + I );}
Note: The initial value of this counter is-1, and the prefix increment (++ I) is used ).
Do/while LOOP
The do/while loop is similar to the while loop. The only difference is that the loop executes a code block before checking whether the condition is true.
- Do {
- [LoopBody]
- } While ([conditional])
The following is a do-while loop:
- Do {
- // Even if the condition is false
- // This code will be executed once.
- Alert ("Hello !" );
- } While (false );
Note: This type of loop is not commonly used, because in rare cases, a loop needs to be executed once without any conditional judgment. In any case, we must be clear about this.
Break and continue
Generally, the truthy value can end a loop, but the break statement can also be used to jump out of the loop:
- // Stop the loop
- For (var I = 0; I <10; I ++ ){
- If (something ){
- Break;
- }
- }
Use the continue statement to skip the current loop and execute the next loop.
- // Skip the current loop and execute the next loop
- For (var I = 0; I <10; I ++ ){
- If (something ){
- Continue;
- }
- // The following loop will be executed only when the following conditions are met
- // If something is true to console. log ("match ");
- }