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 7 of 7

Thread: Need Help with a do while loop I'm not sure if right Please Help!!

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Need Help with a do while loop I'm not sure if right Please Help!!

    Hey guys i need help i am trying to execute a do while loop and don't know how to do it properly and to stop the loop please help me
    class Category{
       public static void main(String[] args){
     
       // Declare Variables
       int age, x = 1;
       int s=0;
     
       // User Input
       do
       {
       System.out.print("Enter your age  :  "); // Read Data
       age = Keyboard.readInt();
       System.out.print(x);
     
       // Decisions
       if (age < 14){
       System.out.print("You are in Category A ");}
       else if(age  < 16){
       System.out.print("You are in Category B ");}
       else if(age >= 16){
       System.out.print("You are in Category C ");}
       else
       System.out.print(s);
       } while (x <=10);
      }
    }


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need Help with a do while loop I'm not sure if right Please Help!!


  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with a do while loop I'm not sure if right Please Help!!

    Thanks for replying but i don't know how to stop a do while loop or have an if statement so i can stop it please help me

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Location
    Germany
    Posts
    20
    My Mood
    Bored
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Need Help with a do while loop I'm not sure if right Please Help!!

    You could make a bool variable and set it as second condition to the do-while loop.
    When you change the bool you stop the loop.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with a do while loop I'm not sure if right Please Help!!

    i understand but could u give me an example please

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Location
    Germany
    Posts
    20
    My Mood
    Bored
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Need Help with a do while loop I'm not sure if right Please Help!!

    Sure.
    boolean run=true;
    do{}
    while(condition&&run);

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with a do while loop I'm not sure if right Please Help!!

    i got it thanks for the help man

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  4. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM