Results 1 to 8 of 8
  1. #1
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default When you have a choice between using either a for loop or a while loop...

    When you have a choice between using either a for loop or a while loop, is it generally better to use a for loop? If so, what would the exceptions be to use a while loop?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: When you have a choice between using either a for loop or a while loop...

    Basically you use a for loop when you know how many time you want to do the iteration. For example you want to iterate from 1 to 10.

    A while loop can be use when the number iteration is unknown. Like read all the lines from a file where the number of lines is unknown at the beginning of the loop. So you just read it until it reach the end of the file.

  3. #3
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Re: When you have a choice between using either a for loop or a while loop...

    Quote Originally Posted by wsaryada View Post
    Basically you use a for loop when you know how many time you want to do the iteration. For example you want to iterate from 1 to 10.

    A while loop can be use when the number iteration is unknown. Like read all the lines from a file where the number of lines is unknown at the beginning of the loop. So you just read it until it reach the end of the file.
    Yeah, but there are times when you can use either. In those instances, which one is generally better to use?

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: When you have a choice between using either a for loop or a while loop...

    For example if you want to iterate from 1 to 10. Using a while loop it will be like:

    Java Code:
    int i = 1;
    while (i <= 10) {
        i++;
    }
    Using a for loop it will be:

    Java Code:
    for (int i = 1; i <= 10; i++) {
    }
    The while loop require you to create a variable as a counter. In this case the i variable. And this variable will available after the loop while loop is finish. But if you are using the for loop the i variable will be scoped only in the for loop. When the for loop finish this variable will be gone. And the for loop in this case look simpler. Beside that I think the both operate just the same. For loop can be said as a special kind of while loop.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: When you have a choice between using either a for loop or a while loop...

    Quote Originally Posted by psx2514 View Post
    Yeah, but there are times when you can use either. In those instances, which one is generally better to use?
    Maybe you have an example in mind?
    Please do not ask for code as refusal often offends.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Re: When you have a choice between using either a for loop or a while loop...

    My opinion: Use a for loop when you want to iterate; otherwise, use a while loop. Technically, you could write an entire loop, logic and all, inside a for statement. That doesn't mean you should. Most business coders avoid writing hard-to-read code, but I have come across messy constructs inside a for loop. It doesn't matter until you are looking at someone else's code to either find a bug or make an enhancement. Then, you have to decipher exactly what their code is doing or risk missing a problem or creating one.

    In the same way, I avoid writing complicated conditions in while statements. Many disagree with me, but I prefer while(true) { if (...) break; ... } constructs, just because they are more readable.
    The Java Tutorial. Read it.

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: When you have a choice between using either a for loop or a while loop...

    I generally use for loops for simple loops or loops with known iterations, like going through an array. I use while loops for unknowns, reading through unknowns such as when I'm grabbing information from something. I don't always know what is going to return, so my while statement will go until the file ends. Something that would either be difficult or look messy inside a for loop.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,928
    Rep Power
    4

    Default Re: When you have a choice between using either a for loop or a while loop...

    I tend to use for loops for looping a known number of times or for indexing an array or List. I tend to use while loops with iterators or when the loop condition is manifested within the loop as opposed to being known before hand. However it may boil down to personal preference. I have seen both the following for an infinite loop:

    for(;;)
    while(true);

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Converting a for loop to a do-while loop
    By awesom in forum New To Java
    Replies: 1
    Last Post: 11-23-2011, 03:02 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  5. Replies: 5
    Last Post: 04-30-2010, 05:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •