The  for loop is an elemental and convenient feature of any programming language. It will let you in automating the repetitive tasks that you need to perform on a daily basis. In this blog, we will solve the question ‘What are the 3 parts of a for loop?’.

Following is the syntax of for loop:

for (initialization; condition; increment/decrement) {
    // code block to be executed
}

Also Read: Python programming structure: Everything you need to know

What are the 3 parts of a for loop?

What are the 3 parts of a for loop?

The for loop consists of 3 parts in most programming languages such as Java, JavaScript, C, C++, or PHP. The 3 parts of a for loop are as follows:

  • Firstly, Initialization: As you can understand from the term, we will initialize the loop counter. You have to execute this expression only once i.e., at the start of the for loop.
  • Secondly, Condition: Now we have to write the logical condition for executing the for loop. If the evaluation of this expression is true then the for loop will execute or else it will terminate.
  • Lastly, Increment/decrement: In this expression, we have to increase or decrease the counter value of the loop. It will execute at the end of every iteration after the code block is executed.

A simple guide to writing a for loop

Now, we will guide you to write a for loop in JavaScript. Let’s dive right into it:

1. Initializing a counter

As mentioned above, firstly, we will initialize a loop counter. For example, let i = 0.

We recommend that you initialize a variable with var or let keyword as it will change in each loop iteration. For that reason, it must be a variable and not a constant. You will be able to initialize a loop counter with any value you wish. That being the case, it does not need to be 0 every time. For example,  let i = 5.

for (let i=0; condition; increment/decrement) {
    // code block to be executed
}

2. Writing the condition to run the for loop

Secondly, we have to write a logical condition that will determine the running time of the loop. If the evaluation of this expression is true then the for loop will execute or else it will terminate.

for (let i=0; i<5; increment/decrement) {
    // code block to be executed
}

This for loop is going to run for as much as the value of i is less than 5.

3. Increment/decrement of the loop counter

Thirdly, we will now increase or decrease the value of the loop counter. For example, i++i--.

for (let i=0; i<5; i++) {
    // code block to be executed
}

++ is an operator of the increment. Also, i++ is equal to i = i + 1. i++ will increase the value of i by 1 at any moment when the loop runs.

-- is an operator of the decrement. Also, i-- is equal to i = i - 1. i-- will decrease the value of i by 1 at any moment when the loop runs.

There may come a time when you will come across ++i. In this case, if the operator is before the variable (e.g. ++i), then the value of the variable is modified before the evaluation of the expression. On the other hand, if the operator is after the variable (e.g. i++), then the value of the variable is modified after the evaluation of the expression. The increment/decrement part of for loop will give the same result in both i++ and ++i.

4. Writing the code for execution

Lastly, we will write the code for execution inside the for loop body.

for (let i=0; i<5; i++) {
    console.log("Hello, World!");
}

The Output will be as follows:

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!

In order to understand the  for loop better, we provide you with a flowchart of the same:

What are the 3 parts of a for loop?

Workings of the For loop

1. Displaying numbers from 1 to 5

for (let i=1; i<=5; i++) {
    console.log(i);
}

Output:

1
2
3
4
5

For a detailed understanding, let’s look at the working of the above program step by step:

IterationCounterCondition: i<=5Action
1sti=11<=5 evaluating to true1 is printed
i will increase to 2
2ndi=22<=5 evaluating to true2 is printed
i will increase to 3
3rdi=33<=5 evaluating to true3 is printed
i will increase to 4
4thi=44<=5 evaluating to true4 is printed
i will increase to 5
5thi=55<=5 evaluating to true5 is printed
i will increase to 6
6thi=66<=5 evaluating to falsefor loop terminates

2. Displaying numbers from 5 to 1

for (let i=5; i>=1; i--) {
    console.log(i);
}

Output:

5
4
3
2
1

Additionally, for a detailed understanding, let’s look at the working of the above program step by step:

IterationCounterCondition: i>=1Action
1sti=55>=1 evaluating to true5 is printed
i will decrease to 4
2ndi=44>=1 evaluating to true4 is printed
i will decrease to 3
3rdi=33>=1 evaluating to true3 is printed
i will decrease to 2
4thi=22>=1 evaluating to true2 is printed
i will decrease to 1
5thi=11>=1 evaluating to true1 is printed
i will decrease to 0
6thi=00>=1 evaluating to falsefor loop terminates

Conclusion for ‘What are the 3 parts of a for loop?’

We hope that all your queries were resolved in this blog regarding the 3 parts of a For loop. In addition to the answer to the above question, we have also provided a step-by-step guide for writing a  for loop. So, that it can make your work easier. There are examples in summary for your better understanding. Thank you!

Tagged in:

, ,