Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Using a loop w/a Switch (Case) Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using a loop w/a Switch (Case) Statement" Watch "Using a loop w/a Switch (Case) Statement" New topic
Author

Using a loop w/a Switch (Case) Statement

joseph james
Greenhorn

Joined: Nov 02, 2002
Posts: 3
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

Joined: Jun 24, 2002
Posts: 90
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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using a loop w/a Switch (Case) Statement
 
Similar Threads
Back to basics: if statement
equalsIgnoreCase
if else statement
comparison methods
java temperature converter