First, let’s take a look at the basic syntax of a for loop:
There are three important parts to a “for loop”:
- Initialization: This is where you initialize your counter variable. It’s executed once when the loop starts.
- Condition: This is a Boolean expression that is checked before each iteration of the loop. If it evaluates to true, the loop continues; if false, the loop stops.
- Final expression: This runs after every loop iteration. It’s typically used to increment or decrement your counter variable.
Here’s a simple example of a “for loop”:
In this example:
- Initialization is `let i = 0`, so we’re starting our counter `i` at `0`.
- Condition is `i < 5`, so the loop will continue as long as `i` is less than `5`.
- The final expression is `i++`, which increments `i` by `1` after each loop iteration.
- As a result, this loop will output the numbers `0` through `4` to the console.
See the execution plan in tabular format, Hope this will provide you better understanding:
|Iteration No||Initialization||Condition||Code Inside Loop||Expression||Continue/Exit|
|1||i = 0||0 < 5 (true)||console.log(0)||i++ (i becomes 1)||Continue|
|2||–||1 < 5 (true)||console.log(1)||i++ (i becomes 2)||Continue|
|3||–||2 < 5 (true)||console.log(2)||i++ (i becomes 3)||Continue|
|4||–||3 < 5 (true)||console.log(3)||i++ (i becomes 4)||Continue|
|5||–||4 < 5 (true)||console.log(4)||i++ (i becomes 5)||Continue|
|6||–||5 < 5 (false)||–||–||Exit|
- Initialization happens only once, before the loop starts.
- Condition is checked before each iteration of the loop. If the condition evaluates to false, the loop stops and control exits the loop.
- Code Inside Loop is executed if the condition is true.
- Expression is executed after each iteration of the loop.
- The process of checking condition and running the loop continues until the condition becomes false, at which point the loop exits.
Nested For Loops
For loops can also be nested within one another. This is useful when working with multidimensional arrays or when you want to perform more complex looping logic.
Here’s an example:
In this nested loop:
- The outer loop runs three times, with i taking the values of 0, 1, and 2.
- For each iteration of the outer loop, the inner loop also runs three times, with j taking the values of 0, 1, and 2.
- Therefore, the `console.log` statement is executed a total of 9 times (3 outer iterations times 3 inner iterations).
The Break and Continue Statements
Two keywords that you might find useful when working with for loops are “break” and “continue”.
The break statement allows you to stop the loop before it has looped through all iterations. Here’s an example:
In this example, the loop will stop as soon as `i` equals `5`, and thus it will only output the numbers `0` through `4`.
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop. Here’s an example:
In this example, the loop will skip the iteration where `i` `equals 5`. It will output the numbers `0` through `4` and `6` through `9`.