Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Nouish

Thread: change do-while loop to while

  1. #1
    Souverain is offline Member
    Join Date
    Dec 2012
    Location
    Ottawa
    Posts
    4
    Rep Power
    0

    Default change do-while loop to while

    Hi i know its probably an easy one but i just cant figure that one out i might be tired ... i need to change the Do-while loop for while.

    Java Code:
    import java.util.*;
    public class DoWhile2
    {
       public static void main (String[] args)
       {
          final int AGE_MIN = 0,
          AGE_MAX = 125;
          int age ;
          boolean valide ;
          String tmp;
          Scanner clavier = new Scanner(System.in);
          // saisir et valider l'âge entre 2 bornes
          do {
             System.out.println("Tapez l'âge d'une personne entre "+ AGE_MIN +
                   " et " + AGE_MAX + ": ");
             age = clavier.nextInt();
             valide = (age >= AGE_MIN && age <= AGE_MAX);
             if (!valide)
                System.out.println(age + " est hors bornes");
          } while (!valide);
          System.out.println("Age saisi : " + age + " an(s)");
          char genre ;
          // saisir et valider le genre (parmi f, F, m et M)
          do {
             System.out.println("Tapez f, F, m ou M pour le genre : ");
             tmp = clavier.next();
             genre = tmp.charAt(0);
             valide = (genre == 'f' || genre == 'F' || genre == 'm' || genre == 'M';
             if (! valide)
                System.out.println("Le caractère saisi " + genre + " est invalide");
          } while (!valide);
          System.out.println("Caractère saisi pour le genre : " + genre );
       }
    }
    never-mind the french ...
    Last edited by pbrockway2; 12-26-2012 at 05:59 AM. Reason: code tags added

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

    Default Re: change do-while loop to while

    Hi Souverain, welcome to the forums!

    When you post code, use the "code" tags. In the pane where you compose your post it's a little # sign next to the speech bubble you used to "quote" the code. Or you can enter the tags by hand: put [code] at the start of the code, and [/code] at the end.

    The following are equivalent (I think):

    Java Code:
    boolean cond;
    do {
        // some code
        cond = some_condition;
        // more code
    } while(!cond);
    Java Code:
    boolean cond = false;
    while(cond {
        // some code
        cond = some_condition;
        // more code
    }
    Personally I prefer a while loop (unless it's really contrived to have one) since it states the "governing" condition right at the start.
    Last edited by pbrockway2; 12-26-2012 at 06:13 AM.

  3. #3
    Souverain is offline Member
    Join Date
    Dec 2012
    Location
    Ottawa
    Posts
    4
    Rep Power
    0

    Default Re: change do-while loop to while

    Thank you ! will do for the code

  4. #4
    Nouish is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: change do-while loop to while

    Quote Originally Posted by pbrockway2 View Post
    Hi Souverain, welcome to the forums!

    When you post code, use the "code" tags. In the pane where you compose your post it's a little # sign next to the speech bubble you used to "quote" the code. Or you can enter the tags by hand: put [code] at the start of the code, and [/code] at the end.

    The following are equivalent (I think):

    Java Code:
    boolean cond;
    do {
        // some code
        cond = some_condition;
        // more code
    } while(!cond);
    Java Code:
    boolean cond = false;
    while(cond {
        // some code
        cond = some_condition;
        // more code
    }
    Personally I prefer a while loop (unless it's really contrived to have one) since it states the "governing" condition right at the start.
    No, a do-while loop will execute one time before checking the condition.

    Java Code:
    do {
    	System.out.println("This line will be printed once, before the condition is checked.");
    } while (false);
    Your while code example would not even execute once, because the condition is always false.
    Last edited by Nouish; 12-29-2012 at 06:59 AM.
    pbrockway2 likes this.

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

    Default Re: change do-while loop to while

    Opps! Yes, you're right: the while loop should have said "while(!cond){". Thanks for the correction.

    Using the OP's variable names it would look like:

    Java Code:
    boolean valide = false;
    while(!valide {
        // some code
        valide = some_condition;
        // more code
    }

Similar Threads

  1. Replies: 12
    Last Post: 04-20-2012, 06:43 PM
  2. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  3. change text colors through nested loop
    By Bader in forum Java Applets
    Replies: 1
    Last Post: 05-09-2010, 03:24 AM
  4. Replies: 18
    Last Post: 11-30-2009, 12:58 AM
  5. Change my for loop
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 12-12-2007, 12:00 PM

Posting Permissions

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