6

I can't seem to find a straight forward yes or no to this in my searching. In Android, is there a way to use a conditional statement in case-switch? For example, with age being an int value:

switch (age){
case (>79):
 // Do this stuff
 break;
case (>50):
 // Do this other stuff
break;
etc, etc

I've tried several ways to code this (completely shooting in the dark) and come up with compiler errors, and I've also tried a nested IF statement, but it doesn't support break so the logic breaks down and it ends up also executing ELSE code lower in the nesting. I feel like switch-case is my best bet but I can't find an example of the correct syntax for what I'm trying to do! Any help would be appreciated. All the examples I find just use switch-case for a few things like if it is a 1 do this, if it's a 2 do that, but without making 100 case statements to check against age, I'm not sure how else to work this.

Robert
  • 130
  • 1
  • 1
  • 9
  • Are you running this through a loop? Try posting more of your code. – shinjw Sep 19 '14 at 19:05
  • You might want to look at http://stackoverflow.com/questions/389741/continue-keyword-in-java. This is not a good place to use switch. – shinjw Sep 19 '14 at 19:08

7 Answers7

8

No. You cannot do this,

switch (age){
case (>79):
  // Do this stuff
  break;
case (>50):
  // Do this other stuff
  break;
}

You need an if and else,

if (age > 79) {
 // do this stuff.
} else if (age > 50) {
 // do this other stuff.
} // ...
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • The only problem then is lets say the age is 51, it will do the age 50 if, but also the age 45 if, age 35 if, etc. I tried using "break;" but that, apparently, doesn't work with a nested if. So How do you get it to do just the one without all the lower ones? – Robert Sep 19 '14 at 19:11
  • Exactly as I said in my answer, it won't enter the `else` block if the `if` block is entered. This is not a `switch`. – Elliott Frisch Sep 19 '14 at 19:19
  • So if I understand you correctly, once an if condition is met it no longer evaluates all the other nested if's in that block of nested ifs? So it's like the break is implied? – Robert Sep 19 '14 at 19:27
  • @Robert No. These are not nested ifs. This is `if` and then `else`. `if (a > 5) { AAA; } else if (a > 3) { BBB; } else { CCC; }` if a is 6 would do AAA (and *nothing* else). if a is 4 it would do BBB (and *nothing* else). If a is -1 it would do CCC.... first test, second test, otherwise.... – Elliott Frisch Sep 19 '14 at 19:30
  • Thanks! I think I understand it now. I'm probably not using the right words since I'm very new to this and learning, but I get it! :-) – Robert Sep 19 '14 at 19:31
6

It is not possible. Instead, Try this minimalist approach

 age > 79 ? first_case_method() 
          : age > 50 ? second_case_method() 
          : age > 40 ? third_case_method() 
          : age > 30 ? fourth_case_method() 
          : age > 20 ? fifth_case_method()
          : ... 
          : default_case_method();
Williem
  • 1,131
  • 1
  • 12
  • 19
  • 2
    This does not directly relate to the question, even through it can be seen as a valid answer. Please provide some more details, as the question is about a switch statement and your answer provides an if-then-else solution. – hotzst Jan 17 '16 at 19:41
  • @hotzst Like already mentioned by the others, there is no way to use conditional statements in the java switch loop. instead, maybe this way to handle the situation could be less wordy. – Williem Jan 17 '16 at 20:26
1

You can't do this use if then statement.

if(age > 79)
{
   //do stuff
}
else if(age > 50)
{
   //do stuff
}
else
{
   /do stuff
}

etc...

brso05
  • 13,142
  • 2
  • 21
  • 40
0

If you are using a loop you might want to look at What is the "continue" keyword and how does it work in Java?. This is not a good place to use switch.

if(age > 79)
{
   //do stuff
   continue; // STOP FLOW HERE AND CONTINUE THE LOOP
}
else if(age > 50)
{
   //do stuff
   continue; // STOP FLOW HERE AND CONTINUE THE LOOP
}
Community
  • 1
  • 1
shinjw
  • 3,329
  • 3
  • 21
  • 42
0

each case of switch is supposed to be an integer or String since JavaSE 7 and you are trying to feed a boolean value to it so its not possible .Read oracle doc to know about java switch in detail http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

NavinRaj Pandey
  • 1,674
  • 2
  • 26
  • 40
-1

You can't use conditional statements with switch.

But you CAN do it with if statements! If you have a loop you can use continue to stop any upcoming lines and start from the beginning of the innermost loop.

if(age>76){
    // Code...
    continue;
}else if(age>50){
    // More Code...
    continue;
}else{
    // Even more code...
    continue;
}
drage0
  • 372
  • 3
  • 8
-5

It can be done. The code needs to be slightly altered.

public static void main(String[]arguments)
    {
        int x=1, age=55;
        switch(x)
        {
        case 1: if((age>=60)&&(age<200))
            System.out.println("Senior Citizen");
        case 2: if((age>=18)&&(age<60))
            System.out.println("Adult");
        case 3: if((age>=12)&&(age<18))
            System.out.println("Teenager");
            break;
        default :
            System.out.println("Child");
            break;
        }
}
Daniel
  • 3,541
  • 3
  • 33
  • 46
Vikram
  • 1