For Loop in Java

For Loop in Java

The basic idea of looping is to execute something repeatedly for a number of times, or till some condition is satisfied. We simply identify the block of code which needs to be run repeatedly and put it inside the loop. Here, first, we will explore the for a loop. But before we move any further, let’s just first understand the syntax of the for a loop –

for (initialization ; condition ; iteration){
// here comes the code which we want to execute repeatedly.
}
example –
for (int i = 0; i<10; i++){
// here comes the code which we want to execute repeatedly.
}

For Loop in Java

The terms like initialization, condition, and iteration, may seem alien to you right now, but let’s explain these terms so that we can comfortably make use of these terms later.

Initialization: Well, here, we are initializing the loop. Basically, you will find the loop running from like… 1 to 10, or 1 to 100, or whatever. Not necessarily starting from 1, but whatever the initialization, we have to do it here. In the example given above, after the syntax, you can find that I have created the variable with the name i(which is just a variable name, so you can give anything), which is initialized to 0. So, in short, our loops start from i = 0.

Condition: Now, after we are done with the initialization, we have to tell the termination condition of the loop. In other words, how are we going to get out of the for loop? In the example, we can have a look that the condition is specified that i <10. This means that every time the loop runs, it will check whether or not the value in the variable i is less than 10. the moment this condition becomes false, we move out of the loop. This means that the loop is running continuously, till the condition becomes false. Once this condition is false, we are out of the loop.

Iteration/iteration step: Now, after we have initialized, and also specified the condition, now is the time to do some iteration, like increment or decrement or some other operation, which leads the value of the initialized variable to the terminating condition. The iteration/iteration step is not necessarily just an increment or decrement, but it can be some other arithmetic operation, like i=i+2 (or i+=2, for even shorter), if there is a need of doing this. Just I wanted to mention that this is not necessarily an increment or decrement. We can say that the iteration/iteration step is used to take the loop in the direction of termination.

In the previous example, the loop runs from 0 to 9, because the initialization is done to 0, so the loop starts from 0. the operation is an incrementation operation, which means that every time, 1 is added to the existing value of the variable. So, at first, the value is 0, then 1, then 2, then 3, and so on, up to 9. The condition is checked every time before the execution. When the value is finally incremented and reaches 10, the condition becomes false because 10 is not less than 10. this is where we come out of the loop.

To visualize the for loop, let’s have an example program here, which demonstrates the working of a for a loop. But I hope that the syntax is clear to you. Have a look at the program –

public class Forloop {
public static void main(String[] args) {
for(int i=0;i<10;i++){
System.out.println(“the value of i is now: ” + i);
}
System.out.println(“out of the for loop”);
}
}

Just to mention that if in case you are unable to run this program, and you have copied the whole code, then see whether or not you have put the name of the program as Forloop.java, since the main method is inside the For loop class. Well, if you try running the above program, we find that the value of i starts from 0 and goes right up to 9, and then we are out of the a loop. This is what we expect. To visualize the values of i, we directly printed the values of the variable at each iteration, so that we can understand and visualize the for loop.

for (int i = 0; i<10; i++)

One more thing, to explain why am I declaring a variable inside the for loop from the above example(shown in red). It’s not compulsory, you can create a variable outside as we do much often and then initialize it here with the for loop as just i=0 in this case. This also works. But why am I doing the whole part of declaring and initialization here, relates to the scope of this variable.

Well, if you are creating a variable here, just like I have done, then it means that we are creating a variable just for this loop. After the execution of the loop is complete, the variable no longer exists. But if you create a variable in a way that we often practice, and you are only going to use the variable for looping purposes(specifically for loop), then it would still have a different scope and take up some memory even if the loop is not executing.

Now, let’s have a brief about some situations we might encounter while working with loops. Note that these situations may not be limited to just the for a loop. We are going to discuss these situations individually so don’t worry about it at all.

One such situation is – An infinite loop →

Well, as the name suggests, an infinite loop is a loop that runs an infinite number of times. In other words, the loop will keep executing forever(not literally though). But we need to observe what is the event which causes the loop to execute for infinite times. Well, we know that we execute the loop, till the condition becomes false. Now, if the condition never becomes false, how come is the loop going to stop? Well, now this makes sense, that the loop will execute for infinite times in such a situation in which the condition never gets false. Now we can apply this case to while as well as do while loop, that we are going to have a look on ahead.

Well, this is an example implementation of an infinite loop using for loop –

public class Forloop {
public static void main(String[] args) {
for(int i=100;i>0;i++){
System.out.println(“the value of i is now: ” + i);
}
System.out.println(“out of the for loop”);
}
}

Well, have a look at the initialization and the condition there. The variable inside the loop is being initialized to 100, and the condition that we are giving there is that the variable i should be greater than zero. The variable is being incremented by 1, which means that the variable value is always going to increase, so the condition is always supposed to remain true, resulting in a never-ending loop.

Another such situation – a loop that never executes –

Well, while we have a loop that forever executes, we also got a loop that never executes. Now, what do you think is the situation which causes the loop to never execute? Well, we know that a loop executes when a condition is true. As soon as the condition becomes false, we are out of the loop. But what if the condition is false right the first time? Or in other words, what if the condition never becomes true? In this case, the loop won’t even execute, resulting in a loop that never executes.

But there is one loop, called the do-while loop, which does not have this condition like never executing. It is going to execute at least once. We are going to have a look at the do-while loop also. Maybe your future self is reading that topic right now, and you will read it later. Ok, enough jokes… now let’s have an implementation example for a for loop which never executes. The only thing we have to do is make the loop in such a way that the condition never becomes true. That’s it. Have a look at the below program –

public class Forloop {
public static void main(String[] args) {
for(int i=100;i<0;i++){
System.out.println(“the value of i is now: ” + i);
}
}
}

Well, if we observe here, we can find that the variable is initialized at value 100, and the condition says that the variable i should be less than 0, which is never possible since we are incrementing the variable value in every iteration. This shows that the condition is never going to be true, so the loop will never be executed.

Now, as the next step, we need to understand how a for loop works, or in other words, what happens when? Like, like when is the condition checked, when is the block of code executed, and what is the order in general? Let’s have a look at this –

Firstly, the initialization takes place. The variable is initialized to some value. Then the condition is checked for trueness, like whether or not is the condition true. If the condition is true, then the block of code that is meant to run (the code written within the curly braces) does execute, and then the iteration operation takes place. So, in short, here is the order of what happens when –

  • Initialization.
  • Condition check.
  • Execution of the code for that particular iteration.
  • Iteration operation.

Please make a note that the initialization is done only once when the loop starts. Just in case you are wondering if the initialization is always done, then the value is never going to go ahead of the initialized value. But it’s not the case. The initialization is done once, after which we check the condition and upon the trueness, we execute the relevant code, and then through the iteration operation, we alter the value of the variable according to the need of the code.

So, this was much about them for a loop.

I hope that you have understood the concept of for loop, and looping in general. If it is so, the other loops are just a matter of understanding syntax and some differences between them. We are going to solve some problems related to looping, but after we go through all the loops and understand them. So, now let’s move on to another type of loop.