for loop in java

for loop in java:

for loop in java is more efficient loop as compared to while and do while loop.

 Because all necessary three steps for loop define in single line.

 For example we see in while loop like

int i=0; //1 step is initialization of loop

while(i<5) // condition of loop
{

      System.out.println(“value of i is::”+i);

      i++; i--; // increment or decrement
}

and in do while like this.

int i=0; //1 step is initialization of loop

do{

       System.out.println(“value of i is::”+i);

        i++; i--; // increment or decrement
 } while(i<5); // condition of loop

Now see same programme with for loop: but first to fall we need to understand

Syntax of for loop.

Workflow diagram of for loop:

 

you can see above basic flow diagram of for loop.

Working of for loop:

for loop in java
for loop in java

1.first initialization block like :

     for(int i=0;;)
2. Second block is condition block like:

      for(int i=0;i<5;)
3. Third block is increment or decrement block like:

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

all necessary three steps for loop define in a single line that is the
power of for loop.

 

for(int i=0;i<5;i++)
{
    //Statement
}

Now we need to understand working of for loop;

1 .First initialization block will run
2. After that conditional block will run then
3. if condition is true flow will go in body of loop and execute number of statement
those write with in loop body.
4. After that flow will go for increment or decrement block
5. After again flow will go for conditional block if condition is true then flow again will enter in body of loop.
6. if condition is false then flow exit from loop.
So that is working of for loop

Now we will solve above problem with help of for loop;

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

Output:

1
2
3
10
:
:
99
100

we make that programme with help of while loop ,do while loop and for loop
so we can see that most efficient way with for loop.

Infinite for loop:

if our condition never be false for loop then that loop will go in infinite state.

Example:

class test
{
public static void main(String [] ar)
{
    for(int i=0;i<5;) {
        System.out.println("test");
     }
}
}

Output:

test
test
test
test
till infinite time

Hope so you like this article Thanks…

111 thoughts on “for loop in java

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Thanks a lot for sharing this with all of us you really know what you are talking about! Bookmarked. Please also visit my website =). We could have a link exchange arrangement between us!

  2. It’s the best time to make a few plans for the longer term and it is time to be happy. I’ve learn this post and if I may just I desire to suggest you few interesting things or suggestions. Maybe you could write next articles regarding this article. I wish to read more issues about it!

  3. I?m not sure where you’re getting your information, but good topic. I needs to spend some time learning much more or understanding more. Thanks for great info I was looking for this info for my mission.

  4. You actually make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand. It seems too complicated and very broad for me. I’m looking forward for your next post, I?ll try to get the hang of it!

  5. Hi, I do believe this is a great site. I stumbledupon it ;
    ) I am going to return once again since i have bookmarked it.

    Money and freedom is the best way to change, may you be rich and
    continue to help others.

  6. Pretty section of content. I just stumbled upon your web site and in accession capital to assert that I get actually enjoyed account your blog posts.
    Any way I’ll be subscribing to your feeds and even I
    achievement you access consistently quickly.

  7. That is very attention-grabbing, You’re an excessively skilled blogger.
    I have joined your feed and look forward to searching
    for more of your wonderful post. Additionally, I have shared your website in my social networks

  8. Excellent beat ! I wish to apprentice while you amend your website, how could i subscribe for a weblog web site? The account aided me a appropriate deal. I were tiny bit acquainted of this your broadcast provided brilliant clear concept

  9. “Have you ever thought about creating an e-book or guest authoring on other sites? I have a blog based upon on the same subjects you discuss and would love to have you share some stories/information. I know my visitors would value your work. If you are even remotely interested, feel free to shoot me an e mail.”

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.