FOR LOOP AND EXAMPLES

Guys today we are going to discuss about the FOR LOOP, its structure, its way of work and some examples which surely will help you to enhance your skill but the prerequisite is to read it all. So now let us put some light on the topic.

FOR LOOP

First let us look its syntax.

              for( initialization ; condition ; updation )
               {
                      #operations  you want to perform.
                               
                    } 

Now we will see each member it's containing individually and will know their function.

Initialization:-

In this part we initialize the variable i.e. we give the starting value to the variable.

Condition:-

In this part we set a parameter that our variable needs to fulfill in order to go ahead in the loop and perform what we demand and if it's not fulfilling the criteria or the parameter it will not enter the loop and no functions defined in that loop will be perform.

Updation:-

In this you will update or set the next value to the variable to be check to perform the desired function.

Now we will put some values and see how it really look like.

              for( int i = 1 ; i < 10 ; i++ )
                 {
                     System.out.println(i);
                  } 

This is the basic for loop statement use to print numbers from 1 to 9 but it really will teach you more than its size says let us look its dry run and understand what really is going on.

Dry Run 

For i = 1 it will enter the loop and print the value of i that is 1.
Now it will go to the updation part .
So it will increase the value of i by 1 as the given criteria,so i=2. 

Now for i = 2 it will first go to the condition  block and check whether i is less than 10 or not as it is in this case it will allow us to print the second value too in the next line.

Now for 3 also it will check the condition first and then will print the value in the next line and this will be continued till the value becomes 10.

Now when the value will be 10 it will again go check the condition but as 10 is not less than 10 it will not allow us to enter the loop and we will be break out off the loop.

Output

1
2
3
4
5
6
7
8
9

Let us now see more examples and try to understand for loop better.

Question 1: Write a program in Java to print even numbers using for loop.

class Even
 {
    public static void main (String args[ ])
     {        int i= 2;
      for( i ; i < 100 ; i = i+2 )
        {
           System.out.println(i);
        } 
     }
} 

Dry Run

First value of i is 2 and it will print value 2 

Now it will go to the update part to update the value instruction given here is to increase the value of i  by 2 so now the value of i will be 4 and then it will go to the condition block and check whether condition is fulfilling or not and it is so it will print the value 4.

Now for 6 it will do the same and the process will be continue till the value becomes 20.

Now when the value of i will be 22 remember after 20 also it will increase the value by 2 only as per the given updation block.  Then it will check the condition as it is not satisfying it will break the loop and class will be ended.

Output

2
6
8
10
12
14
16
18
20

Question 2: Write a program in Java to print Hello and then World after some time in next line.

class HelloWorld
 {
    public static void main (String args[ ] )
     {     
       System.out.println("Hello");
       int i;
      for( i = 1 ; i < 1000 ; i++ )
        {
           int x = 2;
        } 
       System.out.println("World");   
     }
 } 


Dry Run

Firstly it will print Hello 

Now for loop will be run even though it not giving any output 

Now World will be print

Output

Hello 
World

Now purpose behind illustrating this example is to make you aware of the fact that for loop is not just about printing stuffs but it is use for many things like for time delaying. 



Comments

  1. I remember when I used to code in my school days and with this for loop statement I used to make patterns of number and alphabets it was really a fun

    ReplyDelete
  2. Very nice post πŸ‘πŸΎπŸ‘πŸΎ

    ReplyDelete
  3. You have explained in a very detailed manner about loops.Very easy to follow and learn!!

    ReplyDelete

Post a Comment

Popular posts from this blog

DATA TYPES

If and Else