Java Switch and Or statement [duplicate] - java

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Can I use OR statements in Java switches?
I have a switch statement. Is there a way to do an or statement within the switch. Ie
switch(val){
case 1
//Do Stuff
break;
case 2
//Do Stuff
break;
case 3
//Do Stuff
break
}
Instead do something like
switch(val){
case 1 or 3
//Do Stuff
break;
case 2
//Do Stuff
break;
}

Yes, try this:
switch(val){
case 1: case 3:
//Do Stuff
break;
case 2:
//Do Stuff
break;
default:
//Do Stuff
break;
}
Always remember to include the default case too.

switch(val) {
case 1:
case 3:
//Do Stuff
break;
case 2
//Do Stuff
break;
}

switch(val) {
case 1:
case 2:
//Do Stuff
break;
case 3:
//Do Stuff
break;
}

Yes:
switch(val){
case 1:
case 2:
//Do Stuff
break;
case 3:
//Do Stuff
break;
default:
break;
}

Related

Java switch statment, break statement unreachable

I am working on a program that is a simple game. I'm using a switch statement to return the file location of images that are assigned to buttons. In order to do this I am using a switch statement inside a method called "get Image View" it returns a string that can be fed into an image view that I will need to compare the image in the button to another image elsewhere. I think I may be over-explaining, anyway. my IDE (NetBeans) is telling me that my break statement is unreachable and I can not figure out why. I have used a switch statement that looks very similar to mine and there is an example in my textbook that is also very similar. I know that I still need to have a default return statement I just want to know what's up with my break statements. Thank you in advance!
public String getImageView(int button)
{
switch(button)
{
case 0: System.out.println("error");
case 1: return "1.png";
break;
case 2: return "2.png";
break;
case 3: return "3.png";
break;
case 4: return "4.png";
break;
case 5: return "5.png";
break;
case 6: return "6.png";
break;
case 7: return "7.png";
break;
case 8: return "8.png";
case 9: return "9.png";
case 10: return "10.png";
}
}
You can try something like below :
public String getImageView(int button){
String imageViewName = "";
switch(button)
{
case 0: System.out.println("error");
case 1: imageViewName = "1.png";
break;
case 2: imageViewName = "2.png";
break;
case 3: imageViewName = "3.png";
break;
case 4: imageViewName = "4.png";
break;
case 5: imageViewName = "5.png";
break;
case 6: imageViewName = "6.png";
break;
case 7: imageViewName = "7.png";
break;
case 8: imageViewName = "8.png";
break;
case 9: imageViewName = "9.png";
break;
case 10: imageViewName = "10.png";
break;
}
return imageViewName;
}
Hope this helps.
Well switch-state-statements behaves not like a big if-else-statement.
Consider this code:
int a = 0;
switch (a) {
case 0:
//do something
break;
case 1:
//do something else
break;
default:
//default case
}
Here the first case will be triggered as you can tell by the value of a.
If you want to first case be executed but also want to slip in the second case anyway you can omit the break in the first case.
So this code:
int a = 0;
switch (a) {
case 0:
System.out.println("case 0");
case 1:
System.out.println("case 1");
break;
default:
//default case
}
will output:
case 0
case 1
just like it is in your Code.
So maybe you should consider the break; in your first case. Otherwise it will execute the println but also returning 1.png.

How to use the switch statement?

I have a problem using switch statement when I tried to deal with a special situation. For example, I have 3 cases: A, B, C.
for A, I want to do statement_1 and statement_3.
for B, I want to do statement_2 and statement_3.
for C, I want to do nothing
if I use if-else statement, it will look like this:
if ( not Car){
do statement_3
if Bag
do statement 2
else if Apple
do statement 1
}
when i try to do it from switch statement im getting trouble
switch (variable){
case A: do statement_1
case B: do statement_2
// how to do statement 3 here?
}
The default section handles all values that are not explicitly handled by one of the case statements.
switch(var){
case A:
//do stuff
break;
case B:
//do stuff
break;
default:
//do stuff
break;
}
EDIT: I have just read the question again and I think I understood you, if you want to "do statement_1 and statement_3 for A and statement_2 and statement_3 for B" you just have to write it:
switch(var){
case A:
statement_1
statement_3
break;
case B:
statement_2
statement_3
break;
}
I guess you forgot to add a break statement. For example if expression == A and you don't have break all statements will be executed, whereas if you add break, it will not execute the rest of the cases. Also, the default is the one which is executed if expression doesn't satisfy any of the cases.
switch(expression){
case A:
statement_1
statement_3
break;
case B:
statement_1
statement_2
break;
default:
break;
}
For each case, you have to write all the required "statements" and then add break; otherwise the next case will be also executed:
switch (variable) {
case A:
statement_1;
statement_3;
break;
case B:
statement_2;
statement_3;
break;
case C:
default:
break;
}
Here default case and C case are the same and do nothing.

Java switch case statement - multiple entries for a case

I need to have the same statement for 2 or 3 conditions.
How can I do this ?
ex:
switch(var)
case 0:
statement1
break;
case 1:
statement1
break
I need to say sth like
case 1,0:
statement1
is this feasible ?
Use this
switch(in){
case 0:
case 1:
//statement1 ; // now statement1 valid for both case 0 and 1
break;
case 2:
//statement2
break;
}
IdeOne example.
Try it this way:
switch(var)
case 0:
case 1:
statement1
break;
Yes! This is why switch has fall through behaviour in Java -
switch(var)
case 0: // no break, so it falls through
case 1: // case 0 or 1
statement1();
break;
default: // not 0 or 1
// something else.
}
Or you can use an if with an || like so
if (var == 0 || var == 1) {
statement1();
} else { // not 0 or 1
// something else.
}

Unable to get behaviour of Switch case in java

I have written small code in java 6
public class TestSwitch{
public static void main(String... args){
int a = 1;
System.out.println("start");
switch(a){
case 1:{
System.out.println(1);
case 3:
System.out.println(3);
case 4:
System.out.println(4);
}
case 2:{
System.out.println(2);
case 5:
System.out.println(5);
case 7:
System.out.println(7);
}
}
System.out.println("end");
}
}
Output: start 1 2 end
My editor is showing orphaned case for 'case 3' and 'case 5'.Still it is running
and showing output.
Does Nastated cases like concept is there in Java?
And Why it is giving above output? rather i thought it will be 'start 1 end'
Your response will be greatly appreciated!!
Switch replaces if else's but switch syntax != If else syntax.
You forgot to put break after each case.
So conditions under fall through.
Example:
case 0:
mColor.setText("#000000");
break;
You can find that in docs
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
public static void main(String... args){
int a = 1;
System.out.println("start");
switch(a){
case 1:
System.out.println(1);
break;
case 2:
System.out.println(2);
break;
case 3:
System.out.println(3);
break;
case 4:
System.out.println(4);
break;
case 5:
System.out.println(5);
break;
case 7:
System.out.println(7);
break;
default:
System.out.println("nothing");
}
switch(a){
case 1:{
System.out.println(1);
case 3:
You cannot nest cases like this. Switch should look either like :
switch(a){
case 1:
System.out.println(1);
break;
case 3:
....
or like this :
switch(a){
case 1:
System.out.println(1);
switch(a) {
case 3:
//...
break;
case 5 :
//...
And if you don't add break at the end of a case, the execution will continue after. You should add a break at the end of each cases if they should be executed separately.
You have wrong closing braces before case 2.
case 3,4 are interpreted as labels not cases.
Your code will give compilation errors as we can't use curly brace after case :
Exact code is:
public static void main(String... args){
int a = 1;
System.out.println("start");
switch(a){
case 1:
System.out.println(1);
case 3:
System.out.println(3);
case 4:
System.out.println(4);
case 2:
System.out.println(2);
case 5:
System.out.println(5);
case 7:
System.out.println(7);
}
System.out.println("end");
}
}
and output will be start
1
3
4
2
5
7
end because you have not use "break" after each case.
As their no break statement in case 1: the execution directly jumps to case 2: and ends up printing "start 1 2 end"..
You have not added break statement before case 2.
Refer this to know more about fall through.
Each break statement terminates the enclosing switch statement. Control flow continues
with the first statement following the switch block. The break statements are necessary
because without them, statements in switch blocks fall through: All statements after
the matching case label are executed in sequence, regardless of the expression of
subsequent case labels, until a break statement is encountered.
int a = 1;
System.out.println("start");
switch (a) {
case 1: {
System.out.println(1);
break;
}
case 3: {
System.out.println(3);
break;
}
case 4: {
System.out.println(4);
break;
}
case 2: {
System.out.println(2);
break;
}
case 5: {
System.out.println(5);
//no break will fall through and print 7 too
}
case 7: {
System.out.println(7);
break;
}
default:{
System.out.println("none");
}
}
See if a=1 then your case 1 will work then 1 will pe printed if as we have not using break after case 1 so all cases are working in flow so output is coming like this if you want to execute only one case at one time then you have to put break after one case like
switch(a){
case 1:
System.out.println(1);
break;
case 3:
System.out.println(3);
break;
case 4:
System.out.println(4);
break;
Then it will break out of the switch case on encountering break statement

Variable value switch cases in Java

I'm looking to make a switch where 5 of the cases are functionally identical, but then there will be other unique cases. Is there a way to list a case value that handles 5 different values? Thanks
You can compound the labels in the switch
switch (variable) {
case 'a': case 'b' : case 'c' : case 'd' :
do something;
break;
case 'e': case 'f' :
do something else
break;
default:
do something;
}
Thinking of a switch as a jump to a label (possibly coupled with a jump (the break) to the end) will help. That means the switch
switch (variable) {
case 'a': case 'b' : case 'c' : case 'd' :
do something;
// note that there's no break here.
case 'e': case 'f' :
do something else
break;
default:
do something;
}
will "do something" and "do something else" for 'a', 'b', 'c', and 'd'; while it will only "do something else" for 'e' and 'f'. Finally if it's not any of the above it hits the default block of "do something".
switch (value) {
case 1:
case 2:
case 3:
case 4:
doSomethingIdentical();
break;
case 5:
doSomethingDifferent();
break;
default:
break;
}
This is very easy to do. Instead of just having one case value that handles all 5 different values, let the 5 case values fall through to each other, like so:
switch(value)
{
case 1:
case 2:
//case 1 and 2 will both result in this code being executed
doSomething();
break;
case 3:
doSomethingElse();
break;
}
As long as you don't put a break; on a switch it will fall through to the next statement.
In that way, you can have something like this:
String value(int val) {
String out = "";
switch(val) {
case 0:
out = "case 0";
break;
case 1:
out = "case 1";
break;
case 2:
case 3:
case 4:
case 5:
case 6:
out = "case 2, 3, 4, 5 or 6";
break;
case 7:
out = "case 7";
break;
}
return out;
}
Yes, just use a switch like this:
switch(v) {
case 1:
case 2:
case 3:
identicFunctionality();
break;
case 4:
other01();
break;
case 5:
other02();
break;
default:
_default();
}
As of Java 12 I believe it is supported. Check out JEP 354. I have never used the syntax but I think it would run like this for your case.
switch (day) {
case 1, 2, 3, 4, 5 -> System.out.println("1-5");
case 7 -> System.out.println(7);
case 8 -> System.out.println(8);
case 9 -> System.out.println(9);
}
on a related note JEP 325 is also cool.

Categories