Results 1 to 7 of 7

Thread: a while loop

  1. #1
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default a while loop

    Hello

    So I need a while loop, what I have are some fields:
    An integer x, another integer b, a maximum limit, and a minimum limit (all ints).

    What I need to do is:
    When x is smaller than the minimum limit, I need to multiply x by 6, and add 2 to the integer b. I need to keep doing this, until x is between the minimum and maximum limits, then stop and print out the number.
    I am confused how to do this.

    What I have so far is this:

    Java Code:
    while(x < minimum) {
    x = x*6;
    b = b + 2;
    }
    I don't know how to make it stop when it's big enough though.
    Any help would be much appreciated

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ile4 View Post
    Java Code:
    while(x < minimum) {
    x = x*6;
    b = b + 2;
    }
    I don't know how to make it stop when it's big enough though.
    Any help would be much appreciated
    It stops when x >= minimum, that's checked by your while condition.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks, okay I'm now supposed to add an extra bit which will stop the while loop either when x is big enough, or, when b reaches a certain number, (whichever is sooner). how can I do that?

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Java Code:
         while (x isn't big enough || b is less than a certain number) {
              //do stuff...
         }

  5. #5
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks a lot

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by StormyWaters View Post
    Java Code:
         while (x isn't big enough || b is less than a certain number) {
              //do stuff...
         }
    I don't think that is correct; the op wants to stop the loop when x is big enough or b is big enough; that check should be an &&, not a ||. (De Morgan's law).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    :eek: yep you are correct lol. I should have played closer attention. :p

Similar Threads

  1. for loop help
    By soc86 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 10:45 PM
  2. Loop Help Please
    By JavaAssistance in forum New To Java
    Replies: 11
    Last Post: 09-26-2010, 11:05 PM
  3. Replies: 5
    Last Post: 04-30-2010, 06:05 PM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  5. while loop
    By Unknown1369 in forum New To Java
    Replies: 5
    Last Post: 07-08-2008, 11:15 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
  •