• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while loop question

 
anthony dwizzledorf
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
suggestions?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a while loop with a boolean condition and ask the user on each iteration if they would like to continue.
 
anthony dwizzledorf
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Craig. I'll give it a shot.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    sever oon
    Ranch Hand
    Posts: 268
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:

    sev
    [ March 03, 2004: Message edited by: sever oon ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic