programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# do/while loop example

Daniel Martos
Ranch Hand
Posts: 126
1
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
• 1
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
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
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.