This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a loop w/a Switch (Case) Statement

 
joseph james
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginning Java student in a beginners Java programming class and Im trying to figure out if I can use a loop statement (while, do-while) within a Switch statement. My book does not offer examples of such a case. Am I better off going with a standard If/Else?
The scenerio is my program is converting Celcius to Fahrenheit and/or vice versa while letting the user enter multiple numbers, if they choose or terminating the program by entering 'Q'.
I can use the switch statement to let the user enter the initial number but I cant compile a correct method to extend the loop.
Do I also need a boolean with a Swith Statement when using a loop. I know I do with an If/Else statement.
My code is:
Thanks for anyones help.
[ edited to preserve formatting using the [code] and [/code] UBB tags and to break apart long lines -ds ]
[ November 02, 2002: Message edited by: Dirk Schreckmann ]
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
insert all contains of the main body into

About switch and if...
switch can be used only for int types. (int, char).
Not for String selection. Really it isn't important, you can write it as you feel more readable.
[ November 03, 2002: Message edited by: Igor Ko ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, joseph!
Just to be clear, you could use a loop in a switch case or in an if-else construct. As Igor suggested, do what you're best able to understand. Good Luck.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic