What are the 3 Parts of a for Loop?
The for
loop is one of the most useful and fundamental aspects of any programming language. A for
loop lets us automate repetitive tasks we need to perform repeatedly over and over again.
The syntax of for
loop is:
for (initialization; condition; increment/decrement) {
// code block to be executed
}
In most programming languages like C, C++, Java, JavaScript, or PHP, a for
loop consists of three parts:
- Initialization: In this expression, we initialize the loop counter. This expression is executed only once at the beginning of the
for
loop. - Condition: In this expression, we write the logical condition to execute the
for
loop. If this expression evaluates to true then thefor
loop executes otherwise thefor
loop will terminate. - Increment/decrement: In this expression, we increment or decrement the loop counter value. This expression is executed at the end of each iteration after the code block has been executed.
Step by step guide to write a for
loop
In this step by step guide, I will show you how to write a for
loop in JavaScript but it should be fairly straightforward to write for
loop in any other programming language.
Step 1: Initialize a counter
In this step, we initialize a loop counter. E.g. let i = 0
.
You should initialize a variable with var
or let
keyword because it will change in every loop iteration. Therefore it should be a variable and not a constant. You can Initialize a loop counter with any value you want. It doesn’t always have to be 0. E.g. let i = 5
.
for (let i=0; condition; increment/decrement) {
// code block to be executed
}
Step 2: Write the logical condition to run the for
loop
In this step, we write the logical condition that determines until when the for
loop runs. If this condition evaluates to true
, then only the for
loop executes otherwise the for
loop terminates.
for (let i=0; i<5; increment/decrement) {
// code block to be executed
}
The above for
loop will run as long as the value of i
is less than 5.
Step 3: Increment/decrement the loop counter
In this step, we increment or decrement the value of loop counter. E.g. i++
, i--
.
for (let i=0; i<5; i++) {
// code block to be executed
}
++
is an increment operator. i++
is equal to i = i + 1
. i++
increases the value of i
by 1 everytime the loop runs.
--
is a decrement operator. i--
is equal to i = i - 1
. i--
decreases the value of i
by 1 everytime the loop runs.
Sometimes you may also come across ++i
. If the operator comes before the variable (e.g. ++i
), the value of variable is modified before the expression is evaluated. If the operator comes after the variable (e.g. i++
), the value of variable is modified after the expression is evaluated. Inside increment/decrement part of for
loop both i++
and ++i
gives the same result.
Step 4: Write the code to be executed inside the for
loop body
In this step, we write the code to be executed inside the for
loop body.
for (let i=0; i<5; i++) {
console.log("Hello, World!");
}
Output
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Below is a flowchart of for loop to help you understand the concepts of for
loop more clearly.
How for loop works
Now we will take a look at how for
loop actually works.
Example 1: Display numbers 1 from to 5
for (let i=1; i<=5; i++) {
console.log(i);
}
Output
1
2
3
4
5
Now let’s take a look at how the above program works step by step.
Iteration | Counter | Condition: i<=5 | Action |
1st | i=1 | 1<=5 evaluates to true | 1 is printedi is increased to 2 |
2nd | i=2 | 2<=5 evaluates to true | 2 is printedi is increased to 3 |
3rd | i=3 | 3<=5 evaluates to true | 3 is printedi is increased to 4 |
4th | i=4 | 4<=5 evaluates to true | 4 is printedi is increased to 5 |
5th | i=5 | 5<=5 evaluates to true | 5 is printedi is increased to 6 |
6th | i=6 | 6<=5 evaluates to false | for loop terminates |
Example 2: Display numbers from 5 to 1
for (let i=5; i>=1; i--) {
console.log(i);
}
Output
5
4
3
2
1
Now let’s take a look at how the above program works step by step.
Iteration | Counter | Condition: i>=1 | Action |
1st | i=5 | 5>=1 evaluates to true | 5 is printedi is decreased to 4 |
2nd | i=4 | 4>=1 evaluates to true | 4 is printedi is decreased to 3 |
3rd | i=3 | 3>=1 evaluates to true | 3 is printedi is decreased to 2 |
4th | i=2 | 2>=1 evaluates to true | 2 is printedi is decreased to 1 |
5th | i=1 | 1>=1 evaluates to true | 1 is printedi is decreased to 0 |
6th | i=0 | 0>=1 evaluates to false | for loop terminates |
Infinite for
loop
If the logical condition always evaluates to true
, then the for
loop will run infinitely until the memory is full.
For example:
for (var i=0; i>=0; i++) {
// code block to be executed
}
In the above example, the logical condition always evaluates to true
. Hence the loop will run forever until the program runs out of memory.
Since all the parts of a for
loop are optional, if you leave all the parts of for
loop empty, then it also makes the loop infinite.
for (;;) {
// code block to be executed
}