• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

do/while loop example

 
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I asked a question about do/while loops a few days ago about do/while loops for a tuition calculation program.  I need an example of how to do one.  Let's not use the student's residency to figure out the student's tuition cost.  Let's do something much simpler to see if I could get a better understanding of do/while loops.
Let's say I want to print a statement with input "What kind of animal are you (enter D for Dog, C for Cat, or S for squirrel): " I'm going to use this input to determine if the animal is going to get a bone, a fish, or a nut.  Can someone give me an example of how to do this in a do/while loop?
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bad example. You want something simpler for the continuation condition. Try reading a word from the keyboard until the word is STOP. Use !"STOP".equalsIgnoreCase(word) as your continuation. Prime the loop withThe equalsIgnoreCase method will accept stop StOp Stop STOP etc. to terminate the loop. Remember the bang sign ! for NOT.
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Bad example. You want something simpler for the continuation condition. Try reading a word from the keyboard until the word is STOP. Use !"STOP".equalsIgnoreCase(word) as your continuation. Prime the loop withThe equalsIgnoreCase method will accept stop StOp Stop STOP etc. to terminate the loop. Remember the bang sign ! for NOT.



ok, would it be better to do it with a switch statement?



If I did it this way, how do I set up the variables?  
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends whether you are writing real code, in which case you shou‍ld decide what you want to do (and explain it), or whether you are trying to learn do‑while statements. If the latter, I would suggest you start with the simplest thing you can which isn't positively trivial.
 
reply
    Bookmark Topic Watch Topic
  • New Topic