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 ]
Joined: Jun 24, 2002
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 ]
Joined: Dec 10, 2001
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.