jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes while loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "while loop question" Watch "while loop question" New topic

while loop question

anthony dwizzledorf

Joined: Mar 01, 2004
Posts: 2
I am just startin out with programming so bare with me. I wrote a simple program that allows a user to enter an amount of money and it will output an amount in change. I was wondering how I would incorporate a loop so that a user has the option of inputing a yes or no statement if they want to keep using the body of the program over and over again. Any
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Use a while loop with a boolean condition and ask the user on each iteration if they would like to continue.
anthony dwizzledorf

Joined: Mar 01, 2004
Posts: 2
Thanks Craig. I'll give it a shot.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch, anthony!
As you're new around these parts and have asked a good beginner's type question, allow me to recommend the following list of resources that you might find useful to have around.
  • Sun's Java Tutorial
  • Introduction to Computer Science using Java by Bradley Kjell
  • Introduction to Programming Using Java by David J. Eck
  • Dick Baldwin's Java Programming Tutorials
  • Interactive Programming In Java by Lynn Andrea Stein
  • Bruce Eckel's Thinking In Java
  • JavaRanch's own Campfire Stories
  • Allen B. Downey's How To Think Like A Computer Scientist

  • Also, when you start to make use of the standard Java libraries a bit more, you'll likely find the API documentation nice to reference to learn just what things can do and how they do it.
    [ March 02, 2004: Message edited by: Dirk Schreckmann ]

    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    sever oon
    Ranch Hand

    Joined: Feb 08, 2004
    Posts: 268
    Not to nitpick , but the only difference between the do/while and the while/repeat loop is that the do/while is guaranteed to run the loop body at least one time before exiting:

    The "do something else" is the only code that will run, and it will only run once in the above example. Since that's the only purpose for the do loop's existence, and you meet the criteria, I say give that poor do loop its shot at the limelight (it gets used so infrequently). You may want to consider defining your exit criteria according to a bunch of exit constants, that way you can have the program exit with information about why it's exiting:

    [ March 03, 2004: Message edited by: sever oon ]
    I agree. Here's the link: http://aspose.com/file-tools
    subject: while loop question
    It's not a secret anymore!