Getting familiar with Java: “for” loop

In this tutorial let us learn about how “for” loop works. First we have to create a new class called as “ExampleFor” and the creation of new class within eclipse is same as before. From Eclipse go to FILE and then NEW and then CLASS and then it will open another window namely NEW JAVA CLASS. In the NAME segment write down “ExampleFor” and then use method public static void main in “which method stubs would you like to create” and then click on FINISH to produce a new class. If you have any doubts regarding creating of class then refer to my previous tutorials.

Now, the java file which is always namely the same as that of class name will be at the right side of src package and it will be “ExampleFor.java”.  Now, the machine code will be like this.

package day1.examples;



public class ExampleFor {



                public static void main(String[] args) {

               



                }



}

 

Let us now add some for loop into this code. This will be like this.

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

Here we write for integer data type variable I is equal to 0, and Ii is less that or equal to 10, i++ is the sign of increment. After int = 0 we have semicolon as this is a self made statement and then after I <=10 this is also the sign of a complete statement so there is semicolon and then after i++ is the statement of increment so there is no completion of statement and that is why no semicolon out there.  Then after parenthesis write down one curly braces and then press enter the closing curly  brace will be automatically appeared so ours next code is:

 

package day1.examples;



public class ExampleFor {



                public static void main(String[] args) {

               

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

                                                System.out.println("i =" +i);

                                }



                }



}

Here, we add the sytem.out.println function. As you must remember I told you about how to write short cut of it as write down syso and then press control and space button and then enter button. Then within parenthesis we have to write within inverted commas the I is equal to and then inverted comma ends and then +i which means the values of integer I in incremental mode till the for condition is satisfied.

Now, we come to realize that the for “for” statement there are three parts. The first part is the initial value which is int i = 0 and then come the condition which is value of I can be <= less that or equal to 10 and then comes the increment or decrement part and here we have increment part which is denoted as i++

The condition I <= 10 means that look through still I is less than or equal to 10. In the initial the value of I is declared as 0. In the condition we have I is less than or equal to 10 as we have initial value of I is equal to zero and then it satisfy the condition as i is less than or equal to 10 or zero is less than or equal to 10. So, initial value now satisfies the condition. Last not the least we have i is in increment function and then in the next line we have print out to system condition and what ever inside parenthesis will be print out or compiled through java application.

The increment of i++ is the same as that of i= i+1 the print ln function which is there within two parenthesis. So, this for statement means if the initial value of i is equal to 0 and then the condition satisfies which is i is less than or equal to 10 and infact the initial value of i is zero satisfies i<=10 and this means that, the increment i++ which is nothing but i = i +1 and exactly the same as that of println proposition.

“for”  loop with initial value, condition and increment and decrement:

So, now the condition is met of intial value of i, so it will keep incrementing with one additional values, till the condition of i is less than or equal to 10 is met. After incrementing when it reaches to value to 10 from zero, then it will check that i value can be 10 so it stops after that. As inside the println function and within parenthesis we have written down as i is equal to +i which means that it will keep increasing its values to one increment every time until and unless i reaches to the value 10. After the value of 10 the value of i will go to 11 which means that it is not less than or equal to 10 and this means that the for loop conditions stops here. In the compiler it will print from 0 to 10. Write down the below code into Eclipse and then save this and run this. Right click any where on the code window and then click RUN AS  and then click on JAVA APPLICATION and you should see the result at the compiler window.  Here is the complete for code of EzampleFor.java

package day1.examples;



public class ExampleFor {



                public static void main(String[] args) {

                    // i++  i = i + 1

                                // for (initial value, condition, increment/ decrement)

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

                                                System.out.println("i =" +i);

                                }



                }



}

 

Now after compilation you can find that the value of i value has been printed to 0 to 10. If you want to write from 0 to 9 then change the condition from i <=10 to i < 10 and the compiler will print out the value of i from 0 to 9. So, in the above example the compilation will be:

i =0

i =1

i =2

i =3

i =4

i =5

i =6

i =7

i =8

i =9

i =10

Now let us look for something like a decrement value. The code should be like this.

package day1.examples;

public class ExampleFor {



                public static void main(String[] args) {

                    // i++  i = i + 1

                                // for (initial value, condition, increment)

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

                                                System.out.println("i =" +i);

                                }

     // j--  j= j-1

                               

                                // for (initial value, condition, decrement)

                               

                                for(int j = 10; j > 0; j--){

                                                System.out.println("j =" +j);

                                }

                }



}

Here, the initial value of integer is j is 10 and then the condition is if j is greater than 0 which satisfies the condition, and from 10 the decrement values with one less that should be print out. So, it should be printing from 10 to 1 with one minus each time in the loop. So, the compilation will be:

j =10

j =9

j =8

j =7

j =6

j =5

j =4

j =3

j =2

j =1

Here the loop continues still j became 1 and that is why we got this result. Here, the initial value of j integer is 10 and it goes on loop still the condition of greater than 0 is satisfied. The decrement is j is less that 1 always and when it reaches to the stage of 1 which means it is greater than 0 and after this the loop will not be working.

In this tutorial we learn about for loop, and how to write initial value and then put condition and then increment or decrement values of its still the conditions of for loop are met correctly.

This entry was posted in Java on by .

Modified: 30th Oct 2017

About Mohan Manohar Mekap

Mohan Manohar is a blogger from India who founded Ittech back in 2007. He is passionate about all things tech and knows the Internet and computers like the back of his hand.