Hi Good day.. can give me a sample program which solves with switch case statement with nested if inside?
thanks
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi Good day.. can give me a sample program which solves with switch case statement with nested if inside?
thanks
Can you post the code you are having problems with?
If you don't understand my answer, don't ignore it, ask a question.
Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.
//package // imports // class { // main { // switch { // if statements } } }
That's the base for what it should look like.
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
If i understood you correctly you wish to see an example of a switch being implemented in if-else methods.
If you're trying to decide which is better, know that in practice, they function almost alike, even performance-wise.
Public static void main(String[] args) { int _number; //... a command which will assign an integer into '_number' (_number = 2 for example) Switch (_number) { Case 0 : //What to do when _number == 0 ; //Break; Case 1 : //What to do when _number == 1 ; //Break; Case 2 : //What to do when _number == 2 ; //Break; Default : //What to do when its none of the above //Break; } }
Public static void main(String[] args) { int _number; //... a command which will assign an integer into '_number' (_number = 2 for example) If (_number == 0) { //What to do when _number == 0 ; } else if (_number == 1) { //What to do when _number == 1 ; } else if (_number == 2) { //What to do when _number == 2 ; } else { //What to do when its none of the above }
You could ofcourse do it like
If (_number == 0) { //What to do when _number == 0 ; } If (_number == 1) { //What to do when _number == 1 ; } If (_number == 2) { //What to do when _number == 2 ; } ...
But that wouldn't be as efficient, as it continues checking and comparing, again and again, regardless to the outcome.
a better explanation and examples can be found at java docs:
The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
Last edited by Stum; July 16th, 2014 at 04:46 AM.