Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: TO do OR NOT TO do

  1. #1
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default TO do OR NOT TO do

    So, my question is simple:

    Why use do-while loops? Theyre harder to read than whiles because the condition is posted after the do command? Nesting do's looks just aweful and is REALLY hard to read. Is there anyone who uses do-while loops frequently? I've been programming for years and I've avoided it like the plague. Think I've had to use it once or twice the last couple of years but thats about it and thats only because assignments require me to.

    Are there people who use it because its a useful method? Do you use it frequently and if so, why?
    I'm not ranting, I'm trying to figure out if I've missed something useful


    // Charlie

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: TO do OR NOT TO do

    do{} while() if you ALWAYS want to execute the loop once.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: TO do OR NOT TO do

    Do-while loops in my opinion is a great way to not have to manually create "one and a half" while loops.

    Instead of having to copy and paste the same code into two different places with a while loop if you want it to execute at least once, you can simply put it into a do-while loop and manage the code this way.

    public static void main(String[] args)
    {
        // a one and a half while loop asking for user password
        String password;
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter your new password:");
        password = reader.nextLine();
        do
        {
            System.out.println("Re-enter your password: ");
        }
        while(!password.equals(Scanner.nextLine());
    }

    Granted, this is a fairly simple example but for longer pieces of code similar to this a do-while can be quite useful. Of course, you don't have to use do-while loops, it's easy enough to get around ever having to ever use do-while loops (similar to how you never need to use the ternary comparison operator).

  4. #4
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: TO do OR NOT TO do

    The password example was actually a really good example. But I do think that the syntax is very hard to read. Alone theyre simple enough to read with proper indentation and such, but nesting them is a real pain. It should be more like:

    do while(blabla){

    }

    Or something. Maybe I should try to use it more often to see if it gets easier with proper training. Thx for your input guys.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: TO do OR NOT TO do

    The reason the while condition checking is at the end is because of the way you'd read it (and also because it's the way it's done in C/C++):

    You'll read the "do", then it doesn't matter what the condition is the first time through. Once you reach the end, then you'd read the condition, and it's only then that you check the condition.

  6. #6
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: TO do OR NOT TO do

    Yes I know its stolen c++/c syntax, Im just trying to get support in hating it. It struck me today how nested do-whiles are almost impossible to make out. I know youre supposed to read them like do it, repeat IF (blabla) but I guess I'm alone in skipping to check for the whiles before reading the while's anyways.

    Figures, I've just gotta pratice it more I guess : /
    Sucks to be me...

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: TO do OR NOT TO do

    Na, I avoided using the for a while, too, and it's not too difficult at all to just ignore using them

    I also ignore for-each loops a lot. I don't know why, but I just like having the control that using indexes gives.

  8. #8
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: TO do OR NOT TO do

    Same here! But lately Ive been thinking for-each looks a lot more professional so Im trying to change. (Prolly mainly because I watched some google seminar and they almost used for-each exclusively for loops ).

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: TO do OR NOT TO do

    Quote Originally Posted by Charlie View Post
    Same here! But lately Ive been thinking for-each looks a lot more professional so Im trying to change. (Prolly mainly because I watched some google seminar and they almost used for-each exclusively for loops ).
    That's because Google prescribes to the Python philosophy (I think it's called "Pythonic"), where you only get for-each loops Both for loop styles has it's uses in my opinion, and it's very easy to duplicate a for-loop with indices using just a while-loop.