• Post Reply Bookmark Topic Watch Topic
  • New Topic

do/while loop example  RSS feed

 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56598
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!