Do While Loops Versus For Loops in Java for Counting

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












Ok so this is a question that has probably been asked a thousand times, but none of them that I found really had an answer that I was particularly looking for. I have done much research on this topic but to no avail. My question is: When it comes to counting, should a do-while loop be used, or a for loop? Because this:



class Main 
public static void main(String args)
int times = 1;
do
System.out.println("I have printed " + times + " times.");
times++;
while (times < 6);




Seems to do the exact same thing as this:



class Main 
public static void main(String args)
for (int times = 0; times < 6; times++)
System.out.println("I have printed " + times + " times.");





Is it a difference in speed? Preference? Situation? Personal quirks? Some kind of "Java social taboo"? I have no idea. Either seem to be able to be used for effective counting, just that one takes a lot more. And both print the exact same thing.



Sorry if this question isn't written very well, this is the first question I have asked on this site.



Thanks!!










share|improve this question









New contributor




Domani Tomlindo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • To an extent, it comes down to taste. I would prefer to use a for loop in this situtation, because the declaration is scoped to the loop, and incrementing is outside the loop body. The main difference between the types of loop, however, is that a do/while loop executes at least once, whereas a for (or a while) may not execute.
    – Andy Turner
    3 hours ago











  • It doesn't matter. BTW, with Java 8+, you can also do IntStream.rangeClosed(1, 6).forEach(times -> System.out.println("I have printed " + times + " times."));
    – David Conrad
    3 hours ago














up vote
6
down vote

favorite












Ok so this is a question that has probably been asked a thousand times, but none of them that I found really had an answer that I was particularly looking for. I have done much research on this topic but to no avail. My question is: When it comes to counting, should a do-while loop be used, or a for loop? Because this:



class Main 
public static void main(String args)
int times = 1;
do
System.out.println("I have printed " + times + " times.");
times++;
while (times < 6);




Seems to do the exact same thing as this:



class Main 
public static void main(String args)
for (int times = 0; times < 6; times++)
System.out.println("I have printed " + times + " times.");





Is it a difference in speed? Preference? Situation? Personal quirks? Some kind of "Java social taboo"? I have no idea. Either seem to be able to be used for effective counting, just that one takes a lot more. And both print the exact same thing.



Sorry if this question isn't written very well, this is the first question I have asked on this site.



Thanks!!










share|improve this question









New contributor




Domani Tomlindo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • To an extent, it comes down to taste. I would prefer to use a for loop in this situtation, because the declaration is scoped to the loop, and incrementing is outside the loop body. The main difference between the types of loop, however, is that a do/while loop executes at least once, whereas a for (or a while) may not execute.
    – Andy Turner
    3 hours ago











  • It doesn't matter. BTW, with Java 8+, you can also do IntStream.rangeClosed(1, 6).forEach(times -> System.out.println("I have printed " + times + " times."));
    – David Conrad
    3 hours ago












up vote
6
down vote

favorite









up vote
6
down vote

favorite











Ok so this is a question that has probably been asked a thousand times, but none of them that I found really had an answer that I was particularly looking for. I have done much research on this topic but to no avail. My question is: When it comes to counting, should a do-while loop be used, or a for loop? Because this:



class Main 
public static void main(String args)
int times = 1;
do
System.out.println("I have printed " + times + " times.");
times++;
while (times < 6);




Seems to do the exact same thing as this:



class Main 
public static void main(String args)
for (int times = 0; times < 6; times++)
System.out.println("I have printed " + times + " times.");





Is it a difference in speed? Preference? Situation? Personal quirks? Some kind of "Java social taboo"? I have no idea. Either seem to be able to be used for effective counting, just that one takes a lot more. And both print the exact same thing.



Sorry if this question isn't written very well, this is the first question I have asked on this site.



Thanks!!










share|improve this question









New contributor




Domani Tomlindo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Ok so this is a question that has probably been asked a thousand times, but none of them that I found really had an answer that I was particularly looking for. I have done much research on this topic but to no avail. My question is: When it comes to counting, should a do-while loop be used, or a for loop? Because this:



class Main 
public static void main(String args)
int times = 1;
do
System.out.println("I have printed " + times + " times.");
times++;
while (times < 6);




Seems to do the exact same thing as this:



class Main 
public static void main(String args)
for (int times = 0; times < 6; times++)
System.out.println("I have printed " + times + " times.");





Is it a difference in speed? Preference? Situation? Personal quirks? Some kind of "Java social taboo"? I have no idea. Either seem to be able to be used for effective counting, just that one takes a lot more. And both print the exact same thing.



Sorry if this question isn't written very well, this is the first question I have asked on this site.



Thanks!!







java for-loop do-while counting






share|improve this question









New contributor




Domani Tomlindo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Domani Tomlindo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 hours ago





















New contributor




Domani Tomlindo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Domani Tomlindo

313




313




New contributor




Domani Tomlindo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Domani Tomlindo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Domani Tomlindo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • To an extent, it comes down to taste. I would prefer to use a for loop in this situtation, because the declaration is scoped to the loop, and incrementing is outside the loop body. The main difference between the types of loop, however, is that a do/while loop executes at least once, whereas a for (or a while) may not execute.
    – Andy Turner
    3 hours ago











  • It doesn't matter. BTW, with Java 8+, you can also do IntStream.rangeClosed(1, 6).forEach(times -> System.out.println("I have printed " + times + " times."));
    – David Conrad
    3 hours ago
















  • To an extent, it comes down to taste. I would prefer to use a for loop in this situtation, because the declaration is scoped to the loop, and incrementing is outside the loop body. The main difference between the types of loop, however, is that a do/while loop executes at least once, whereas a for (or a while) may not execute.
    – Andy Turner
    3 hours ago











  • It doesn't matter. BTW, with Java 8+, you can also do IntStream.rangeClosed(1, 6).forEach(times -> System.out.println("I have printed " + times + " times."));
    – David Conrad
    3 hours ago















To an extent, it comes down to taste. I would prefer to use a for loop in this situtation, because the declaration is scoped to the loop, and incrementing is outside the loop body. The main difference between the types of loop, however, is that a do/while loop executes at least once, whereas a for (or a while) may not execute.
– Andy Turner
3 hours ago





To an extent, it comes down to taste. I would prefer to use a for loop in this situtation, because the declaration is scoped to the loop, and incrementing is outside the loop body. The main difference between the types of loop, however, is that a do/while loop executes at least once, whereas a for (or a while) may not execute.
– Andy Turner
3 hours ago













It doesn't matter. BTW, with Java 8+, you can also do IntStream.rangeClosed(1, 6).forEach(times -> System.out.println("I have printed " + times + " times."));
– David Conrad
3 hours ago




It doesn't matter. BTW, with Java 8+, you can also do IntStream.rangeClosed(1, 6).forEach(times -> System.out.println("I have printed " + times + " times."));
– David Conrad
3 hours ago












5 Answers
5






active

oldest

votes

















up vote
5
down vote













You're right, these do exactly the same thing. If you know in advance (before the loop starts) how many times you want it to iterate, most Java developers will tell you to go with a for loop. That's what it's designed for.



A while loop or do while loop is better suited for situations where you're looking for a specific value or condition before you exit the loop.






share|improve this answer




















  • True, I forgot about the fact that, (from personal experiences), nothing executes after the do-while loop, and would just have to keep nesting loops.
    – Domani Tomlindo
    3 hours ago

















up vote
2
down vote













When faced with these kind of dilemmas, aim for readability and familiarity. You should not concern yourself with micro-optimizations. Focus on readability and clearly conveying you intent. Do as other do in similar situation.



Like @Bill-The-Lizard said, while loop suggests to the reader of your code that you opted for it, because you're not counting, but repeating until a condition is met. At least once - otherwise you'd have chosen while(...) loop.



In other words, for, do while() and while() generally work the same. But one may better convey you intent in your particular piece of logic.






share|improve this answer



























    up vote
    0
    down vote













    I think it's more of a readability and syntactic sugar. This while condition



    while (condition)


    can also be written as



    for (; condition; )


    but obviously the first one looks lot better and is more readable.






    share|improve this answer




















    • OP is asking about a do/while loop, not a while loop.
      – Andy Turner
      3 hours ago

















    up vote
    0
    down vote













    It depends on the programmer's choice when to use for loop or do while loop but the general practice followed by most of the programmers is



    • For loop

      When you know that the loop will execute a predefined number of times(general practice since you can also use for(;true;) which loops forever).
      For example a loop which runs 10 times or n number of times where n is a variable




    for(int i = 0; i < n; i++) 
    //Do something



    • While loop

      When you know that the loop should terminate after the evaluation of a specific condition as true or else you want the loop to run forever (like while(true)) and check the break conditions inside the while loop.

      Also the while loop is preferred when you can't figure out the conditions in the first place and start with while(true) but once you write code inside the loop you get good understanding of what's happening and get the idea about which conditions to check thus when to exit the loop.
      For example




    while(x != 0) 
    //Do something;
    x--;






    while(true) 
    // some code to execute on which the condition depends
    if(condition is true)
    break;




    • Do while loop

      Do while loop is similar to the while loop but with a small difference. That is it allows the first iteration to happen without checking the condition(specified in the while statement, but you can still evaluate the condition inside the block(curly braces)).





    share|improve this answer










    New contributor




    geektl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.
























      up vote
      0
      down vote













      By convention most Java developers use for loops. Effective Java recommends for loops instead of while loops because the loop variable can use a tighter scope which reduces bugs. http://www.corejavaguru.com/effective-java/items/45






      share|improve this answer




















        Your Answer





        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );






        Domani Tomlindo is a new contributor. Be nice, and check out our Code of Conduct.









         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52728945%2fdo-while-loops-versus-for-loops-in-java-for-counting%23new-answer', 'question_page');

        );

        Post as a guest






























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        5
        down vote













        You're right, these do exactly the same thing. If you know in advance (before the loop starts) how many times you want it to iterate, most Java developers will tell you to go with a for loop. That's what it's designed for.



        A while loop or do while loop is better suited for situations where you're looking for a specific value or condition before you exit the loop.






        share|improve this answer




















        • True, I forgot about the fact that, (from personal experiences), nothing executes after the do-while loop, and would just have to keep nesting loops.
          – Domani Tomlindo
          3 hours ago














        up vote
        5
        down vote













        You're right, these do exactly the same thing. If you know in advance (before the loop starts) how many times you want it to iterate, most Java developers will tell you to go with a for loop. That's what it's designed for.



        A while loop or do while loop is better suited for situations where you're looking for a specific value or condition before you exit the loop.






        share|improve this answer




















        • True, I forgot about the fact that, (from personal experiences), nothing executes after the do-while loop, and would just have to keep nesting loops.
          – Domani Tomlindo
          3 hours ago












        up vote
        5
        down vote










        up vote
        5
        down vote









        You're right, these do exactly the same thing. If you know in advance (before the loop starts) how many times you want it to iterate, most Java developers will tell you to go with a for loop. That's what it's designed for.



        A while loop or do while loop is better suited for situations where you're looking for a specific value or condition before you exit the loop.






        share|improve this answer












        You're right, these do exactly the same thing. If you know in advance (before the loop starts) how many times you want it to iterate, most Java developers will tell you to go with a for loop. That's what it's designed for.



        A while loop or do while loop is better suited for situations where you're looking for a specific value or condition before you exit the loop.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 3 hours ago









        Bill the Lizard

        284k156490786




        284k156490786











        • True, I forgot about the fact that, (from personal experiences), nothing executes after the do-while loop, and would just have to keep nesting loops.
          – Domani Tomlindo
          3 hours ago
















        • True, I forgot about the fact that, (from personal experiences), nothing executes after the do-while loop, and would just have to keep nesting loops.
          – Domani Tomlindo
          3 hours ago















        True, I forgot about the fact that, (from personal experiences), nothing executes after the do-while loop, and would just have to keep nesting loops.
        – Domani Tomlindo
        3 hours ago




        True, I forgot about the fact that, (from personal experiences), nothing executes after the do-while loop, and would just have to keep nesting loops.
        – Domani Tomlindo
        3 hours ago












        up vote
        2
        down vote













        When faced with these kind of dilemmas, aim for readability and familiarity. You should not concern yourself with micro-optimizations. Focus on readability and clearly conveying you intent. Do as other do in similar situation.



        Like @Bill-The-Lizard said, while loop suggests to the reader of your code that you opted for it, because you're not counting, but repeating until a condition is met. At least once - otherwise you'd have chosen while(...) loop.



        In other words, for, do while() and while() generally work the same. But one may better convey you intent in your particular piece of logic.






        share|improve this answer
























          up vote
          2
          down vote













          When faced with these kind of dilemmas, aim for readability and familiarity. You should not concern yourself with micro-optimizations. Focus on readability and clearly conveying you intent. Do as other do in similar situation.



          Like @Bill-The-Lizard said, while loop suggests to the reader of your code that you opted for it, because you're not counting, but repeating until a condition is met. At least once - otherwise you'd have chosen while(...) loop.



          In other words, for, do while() and while() generally work the same. But one may better convey you intent in your particular piece of logic.






          share|improve this answer






















            up vote
            2
            down vote










            up vote
            2
            down vote









            When faced with these kind of dilemmas, aim for readability and familiarity. You should not concern yourself with micro-optimizations. Focus on readability and clearly conveying you intent. Do as other do in similar situation.



            Like @Bill-The-Lizard said, while loop suggests to the reader of your code that you opted for it, because you're not counting, but repeating until a condition is met. At least once - otherwise you'd have chosen while(...) loop.



            In other words, for, do while() and while() generally work the same. But one may better convey you intent in your particular piece of logic.






            share|improve this answer












            When faced with these kind of dilemmas, aim for readability and familiarity. You should not concern yourself with micro-optimizations. Focus on readability and clearly conveying you intent. Do as other do in similar situation.



            Like @Bill-The-Lizard said, while loop suggests to the reader of your code that you opted for it, because you're not counting, but repeating until a condition is met. At least once - otherwise you'd have chosen while(...) loop.



            In other words, for, do while() and while() generally work the same. But one may better convey you intent in your particular piece of logic.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 3 hours ago









            rzymek

            6,55223043




            6,55223043




















                up vote
                0
                down vote













                I think it's more of a readability and syntactic sugar. This while condition



                while (condition)


                can also be written as



                for (; condition; )


                but obviously the first one looks lot better and is more readable.






                share|improve this answer




















                • OP is asking about a do/while loop, not a while loop.
                  – Andy Turner
                  3 hours ago














                up vote
                0
                down vote













                I think it's more of a readability and syntactic sugar. This while condition



                while (condition)


                can also be written as



                for (; condition; )


                but obviously the first one looks lot better and is more readable.






                share|improve this answer




















                • OP is asking about a do/while loop, not a while loop.
                  – Andy Turner
                  3 hours ago












                up vote
                0
                down vote










                up vote
                0
                down vote









                I think it's more of a readability and syntactic sugar. This while condition



                while (condition)


                can also be written as



                for (; condition; )


                but obviously the first one looks lot better and is more readable.






                share|improve this answer












                I think it's more of a readability and syntactic sugar. This while condition



                while (condition)


                can also be written as



                for (; condition; )


                but obviously the first one looks lot better and is more readable.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 3 hours ago









                stackFan

                580618




                580618











                • OP is asking about a do/while loop, not a while loop.
                  – Andy Turner
                  3 hours ago
















                • OP is asking about a do/while loop, not a while loop.
                  – Andy Turner
                  3 hours ago















                OP is asking about a do/while loop, not a while loop.
                – Andy Turner
                3 hours ago




                OP is asking about a do/while loop, not a while loop.
                – Andy Turner
                3 hours ago










                up vote
                0
                down vote













                It depends on the programmer's choice when to use for loop or do while loop but the general practice followed by most of the programmers is



                • For loop

                  When you know that the loop will execute a predefined number of times(general practice since you can also use for(;true;) which loops forever).
                  For example a loop which runs 10 times or n number of times where n is a variable




                for(int i = 0; i < n; i++) 
                //Do something



                • While loop

                  When you know that the loop should terminate after the evaluation of a specific condition as true or else you want the loop to run forever (like while(true)) and check the break conditions inside the while loop.

                  Also the while loop is preferred when you can't figure out the conditions in the first place and start with while(true) but once you write code inside the loop you get good understanding of what's happening and get the idea about which conditions to check thus when to exit the loop.
                  For example




                while(x != 0) 
                //Do something;
                x--;






                while(true) 
                // some code to execute on which the condition depends
                if(condition is true)
                break;




                • Do while loop

                  Do while loop is similar to the while loop but with a small difference. That is it allows the first iteration to happen without checking the condition(specified in the while statement, but you can still evaluate the condition inside the block(curly braces)).





                share|improve this answer










                New contributor




                geektl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





















                  up vote
                  0
                  down vote













                  It depends on the programmer's choice when to use for loop or do while loop but the general practice followed by most of the programmers is



                  • For loop

                    When you know that the loop will execute a predefined number of times(general practice since you can also use for(;true;) which loops forever).
                    For example a loop which runs 10 times or n number of times where n is a variable




                  for(int i = 0; i < n; i++) 
                  //Do something



                  • While loop

                    When you know that the loop should terminate after the evaluation of a specific condition as true or else you want the loop to run forever (like while(true)) and check the break conditions inside the while loop.

                    Also the while loop is preferred when you can't figure out the conditions in the first place and start with while(true) but once you write code inside the loop you get good understanding of what's happening and get the idea about which conditions to check thus when to exit the loop.
                    For example




                  while(x != 0) 
                  //Do something;
                  x--;






                  while(true) 
                  // some code to execute on which the condition depends
                  if(condition is true)
                  break;




                  • Do while loop

                    Do while loop is similar to the while loop but with a small difference. That is it allows the first iteration to happen without checking the condition(specified in the while statement, but you can still evaluate the condition inside the block(curly braces)).





                  share|improve this answer










                  New contributor




                  geektl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    It depends on the programmer's choice when to use for loop or do while loop but the general practice followed by most of the programmers is



                    • For loop

                      When you know that the loop will execute a predefined number of times(general practice since you can also use for(;true;) which loops forever).
                      For example a loop which runs 10 times or n number of times where n is a variable




                    for(int i = 0; i < n; i++) 
                    //Do something



                    • While loop

                      When you know that the loop should terminate after the evaluation of a specific condition as true or else you want the loop to run forever (like while(true)) and check the break conditions inside the while loop.

                      Also the while loop is preferred when you can't figure out the conditions in the first place and start with while(true) but once you write code inside the loop you get good understanding of what's happening and get the idea about which conditions to check thus when to exit the loop.
                      For example




                    while(x != 0) 
                    //Do something;
                    x--;






                    while(true) 
                    // some code to execute on which the condition depends
                    if(condition is true)
                    break;




                    • Do while loop

                      Do while loop is similar to the while loop but with a small difference. That is it allows the first iteration to happen without checking the condition(specified in the while statement, but you can still evaluate the condition inside the block(curly braces)).





                    share|improve this answer










                    New contributor




                    geektl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    It depends on the programmer's choice when to use for loop or do while loop but the general practice followed by most of the programmers is



                    • For loop

                      When you know that the loop will execute a predefined number of times(general practice since you can also use for(;true;) which loops forever).
                      For example a loop which runs 10 times or n number of times where n is a variable




                    for(int i = 0; i < n; i++) 
                    //Do something



                    • While loop

                      When you know that the loop should terminate after the evaluation of a specific condition as true or else you want the loop to run forever (like while(true)) and check the break conditions inside the while loop.

                      Also the while loop is preferred when you can't figure out the conditions in the first place and start with while(true) but once you write code inside the loop you get good understanding of what's happening and get the idea about which conditions to check thus when to exit the loop.
                      For example




                    while(x != 0) 
                    //Do something;
                    x--;






                    while(true) 
                    // some code to execute on which the condition depends
                    if(condition is true)
                    break;




                    • Do while loop

                      Do while loop is similar to the while loop but with a small difference. That is it allows the first iteration to happen without checking the condition(specified in the while statement, but you can still evaluate the condition inside the block(curly braces)).






                    share|improve this answer










                    New contributor




                    geektl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    share|improve this answer



                    share|improve this answer








                    edited 3 hours ago





















                    New contributor




                    geektl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    answered 3 hours ago









                    geektl

                    214




                    214




                    New contributor




                    geektl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.





                    New contributor





                    geektl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.






                    geektl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.




















                        up vote
                        0
                        down vote













                        By convention most Java developers use for loops. Effective Java recommends for loops instead of while loops because the loop variable can use a tighter scope which reduces bugs. http://www.corejavaguru.com/effective-java/items/45






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          By convention most Java developers use for loops. Effective Java recommends for loops instead of while loops because the loop variable can use a tighter scope which reduces bugs. http://www.corejavaguru.com/effective-java/items/45






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            By convention most Java developers use for loops. Effective Java recommends for loops instead of while loops because the loop variable can use a tighter scope which reduces bugs. http://www.corejavaguru.com/effective-java/items/45






                            share|improve this answer












                            By convention most Java developers use for loops. Effective Java recommends for loops instead of while loops because the loop variable can use a tighter scope which reduces bugs. http://www.corejavaguru.com/effective-java/items/45







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 3 hours ago









                            neildo

                            98767




                            98767




















                                Domani Tomlindo is a new contributor. Be nice, and check out our Code of Conduct.









                                 

                                draft saved


                                draft discarded


















                                Domani Tomlindo is a new contributor. Be nice, and check out our Code of Conduct.












                                Domani Tomlindo is a new contributor. Be nice, and check out our Code of Conduct.











                                Domani Tomlindo is a new contributor. Be nice, and check out our Code of Conduct.













                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52728945%2fdo-while-loops-versus-for-loops-in-java-for-counting%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                Confectionery