What are the 3 Parts of a for Loop?

By James L.

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:

  1. Initialization: In this expression, we initialize the loop counter. This expression is executed only once at the beginning of the for loop.
  2. Condition: In this expression, we write the logical condition to execute the for loop. If this expression evaluates to true then the for loop executes otherwise the for loop will terminate.
  3. 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.

for loop flowchart

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.

IterationCounterCondition: i<=5Action
1sti=11<=5 evaluates to true1 is printed
i is increased to 2
2nd i=22<=5 evaluates to true2 is printed
i is increased to 3
3rdi=33<=5 evaluates to true3 is printed
i is increased to 4
4th i=44<=5 evaluates to true4 is printed
i is increased to 5
5thi=55<=5 evaluates to true5 is printed
i is increased to 6
6thi=66<=5 evaluates to falsefor 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.

IterationCounterCondition: i>=1Action
1st i=55>=1 evaluates to true5 is printed
i is decreased to 4
2ndi=44>=1 evaluates to true4 is printed
i is decreased to 3
3rdi=33>=1 evaluates to true3 is printed
i is decreased to 2
4th i=22>=1 evaluates to true2 is printed
i is decreased to 1
5th i=11>=1 evaluates to true1 is printed
i is decreased to 0
6th i=00>=1 evaluates to falsefor 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
}