Results 1 to 7 of 7
  1. #1
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default When......."while" vs "do...while"

    i fully understand the difference between while and do...while repetition statements in that
    while tests the loop condition first;
    do...while tests it after;

    therefore if initially false:
    "while" would not execute, and
    "do...while" would execute one time.

    i get that, however, simply:

    what simple example can't you give as to when you would need one instead of the other?

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You answered your own question. Do while executes at least once. As for where to use it... that's up to the context of the problem at hand.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Basically you can interchange any of the looping methods java provides(While, Do-While, For) and design them to work for any situation where one is needed. The key thing is that you realize that a Loop is needed. Selecting which one to use to *best* do something is kind of up to the programmer, though I must admit, some are not as obvious as others.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Java Code:
    A
    C
    while (B) {
        C
        D
    }
    Java Code:
    A
    do {
         C
         D
    } while (B);
    Java Code:
    C
    for (A; B; D) {
        C
    }

    For all intents and purposes, those are equivalent where A and D are single statements, C is a block of code, and B is a conditional.

    As for uses, you will come across uses for each as you go along. For loops, I find, are generally used for array walking or similar functions. While loops are more for waiting for a condition to be true. Do/While loops are the ones I use the least, though occasionally I will use them for setting a number of variables (in the DO part) and, if one isn't correct (the WHILE part), they it re-sets them using new qualities.

    Of course, the uses of these extend way beyond my simplification paragraph above, but you get the gist, am I right?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Do/While loops are the ones I use the least

    Me too - I think because it's less clear as you read the code what is going on if the condition comes after the block that it controls.

    I suppose do-while might be favoured if the block C is long and/or complex. But even then I'd be tempted to make it a method if I could.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    I would make a method AND use a do-while so it was just a line or two of code:
    Java Code:
    do { checkMethod(); }
    while (someCondition);
    That's so much prettier than a basic while loop with a fifty-line method repeated twice. ;)

    But either way, the same effect is achieved. As long as it's not ugly or bloated, if it works--it works.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    Java Code:
    A
    C
    while (B) {
        C
        D
    }
    Java Code:
    A
    do {
         C
         D
    } while (B);
    Java Code:
    C
    for (A; B; D) {
        C
    }

    For all intents and purposes, those are equivalent where A and D are single statements, C is a block of code, and B is a conditional.
    I can't see why those three blocks are equivalent: the execution paths are respectively:

    while: ACBCDBCDBCD ...
    do: ACDBCDBCDB ...
    for: CABCDBCDBCD ...

    The paths are most certainly different ...

    kind regards,

    Jos

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  3. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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