INTRODUCTION

Remember what we learned in english?
Since  is used for specific point of time
For  is used for period of time.
Similarly in coding  While Loop  is used for a specific point like did the child get candy? The  For Loop  is used for a specific period of time like run the loop for 10 times, for 5 times etc. That's how they are different and become useful for different purposes. 



FOR LOOP

 For Loop  has few more things though, it's like a time bomb so you have to set a few things before it becomes functional. Like when to start, when to stop, and how to count from start to stop:
⦿ Counter = It will keep track of the how many times the loop ran
⦿ Condition = It will check the condition i.e. did the counter reach the set time?
⦿ Update = Instead of inside the loop code, for loop has specific area for updating code. It updates the code after one iteration (running the loop for a single time is called iteration).

for (counter ; condition ; update)   
    // write the code here to repeat
 } 
You set a counter, then set a condition, then it'll update, then the loop code will run
Note: The update happens after the loop body runs, right before the next check. Here is a demonstration:

public class ForLoopDemo {
    public static void main(String[] args) {
        // Print numbers 1 to 5
        for (int i = 1; i < 6; i++)  
            System.out.println("Number: " + i);
         } 
    System.out.println("Loop exited!"); 
    }
}

The output will be:  Number: 1   (then i becomes 2)
                               Number: 2   (then i becomes 3)
                               Number: 3   (then i becomes 4)
                               Number: 4   (then i becomes 5)
                               Number: 5   (then i becomes 6 and loop stops)
                               Loop exited!  
⦿ The code sets  i  to be 1, then check if it's smaller than 6, then update it by INCREMENTING for next iteration.
⦿ The loop code runs, the  i  which was equal to one is now equal to 2.
⦿ It will keep running, on 5th iteration the update will set i to be 6, the code will run that time. And on start again the  i  will not be less than 6, so the loop will end right there.



REVERSE FOR LOOP

I know what you are thinking, yeah I hate my mind too. Let's focus on the topic. So for loop is not always ascending like from 0 to 10 or 0 to 5. You can actually code a for loop that starts from 10 and come down to 0, just like a time bomb. Here is a demonstration...

public class ReverseForLoop {
    public static void main(String[] args) {
        // Print numbers 5 to 1
        for (int i = 5; i  > 0 ; i--)  
            System.out.println("Number: " + i);
         } 
    System.out.println("Loop exited!"); 
    }
}
The output will be:  Number: 5  
                               Number: 4  
                               Number: 3  
                               Number: 2  
                               Number: 1  
                               Loop exited!  
⦿ The code sets  i  to be 5, then check if it's greater than 0, then update it by DECREMENTING for next iteration.
⦿ The loop code runs, the  i  which was equal to 5 is now equal to 4.
⦿ It will keep running, on 5th iteration the update will set i to be 0, the code will run that time. And on start again the  i  will not be greater than 0, so the loop will end right there.


POINTS TO REMEMBER

⦿ Usually an integer (int) is used for counters, but technically any numeric type works. In practice, we almost always use int.

⦿ Watch out for the signs, If starting value of  i  is smaller than condition value then
     condition sign must be  <  or the loop will not start
     In update the  i  must increase or the loop will become infinite

⦿ If starting value of  i  is larger than condition value then
     condition sign must be  >  or the loop will not start
     In update the  i  must decrease or the loop will become infinite

⦿ You can update the value as you like, it can be:
                                         i++   (For update by 1)
                                         i = i + 2  (For update by 2)
                                         i = i+ 3 (For update by 3)
Here is a demo...

public class EvenNumbers {
    public static void main(String[] args) {
        // Print even numbers from 2 to 10
        for (int i = 2; i <= 10; i += 2) {
            System.out.println("Even: " + i);
        }
        System.out.println("Done printing evens!");
    }
}
The output will be:  Number: 2  
                               Number: 4  
                               Number: 6  
                               Number: 8  
                               Number: 10  
                               Done printing evens!  
It prints 10, because the condition is  i <= 10 . As long as  i  is less than or equal to 10, the loop will run




CLOSING
That's it for  For Loop  in java. With  For Loop  you have control over where to start, when to stop, and how to count, whether you’re counting up, counting down, or skipping numbers.

Before jumping into the next blog (Nested Loops), it’s time to practice. I’ve prepared an exercise and a mini-project that cover everything you’ve learned in this post.



Download the exercise, break it down line by line, and then try the project challenge. This will make sure you really “own” the concepts before moving forward.