0

I have a primitive question but I can't actually recall the answer.

What if I use Switch Cases instead of IF\ELSE IF. Will all cases get evaluated or break will break out of the whole switch cases and return the first satisfied condition only.

In other words, what if I want to check if someone has a car, bike and plane. And in my particular case someone has all of them would switch return all three as true or it will return the first case only and ignore the rest because of the break?

Sorry for inconvenience.

Thank you

mowienay
  • 1,264
  • 4
  • 19
  • 32

8 Answers8

2

From the Official Java Tutorials :

Each break statement terminates the enclosing switch statement.

In your particular case, if someone has a car, bike or a plane, you should construct more complex if\else statement.

But if you still prefer to use switch, you can do the following:

switch (possession)
{
    case CAR:
    case BIKE:
    case PLANE:
        doSomething();
    break;
}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • It’s not “and” it’s “or” for your code. Any possession of one of these three would lead to the method invocation. – Holger Sep 09 '13 at 10:58
1

It will break out at the first matching. the best thing to do in your case is using logical operators in if statements.

if (hasCar && hasBike && hasPlane)
{
...

}
Rollerball
  • 12,618
  • 23
  • 92
  • 161
1

The break terminates the switch-statement.

Besides, switch evaluates a single variable, your case sounds a bit more complex to me.

Kayz
  • 637
  • 1
  • 8
  • 21
  • The break doesn't **finish** the statement, but **terminates** it. – Konstantin Yovkov Sep 09 '13 at 09:46
  • sorry for my bad english and thanks for correcting me. I`ll edit my answer. – Kayz Sep 09 '13 at 09:48
  • @kocko I think your answer is incorrect. If someone has a car, but not a bike or plane `hasACarBikeAndPlane();`still will be called. (had to post my comment here because of not enough reptation to post it under your answer) – Kayz Sep 09 '13 at 10:47
1

certainly break will break the switch-case flow after encountering first break statementif no break is found then it will start the execution of all statement starting from first matching case,you can implement the logic inside case.and one thing more switch case is little bit faster than if-else please see Why switch is faster than if

Community
  • 1
  • 1
The PowerHouse
  • 560
  • 14
  • 29
1

With switch the first case it finds a match runs and then all the following cases regardless of matching or not, provided you don't use break. By using break,only the actual match case runs and it is almost always only one. Therefore I do not regard in your problem using switch as a good approach, since you can handle it better with if-else.

arjacsoh
  • 8,932
  • 28
  • 106
  • 166
0

Perhaps you would need to rewrite a new style of Data Structure to confirm this. The easyest way that I think is with a Boolean Array (bool[]);

Continuing with your example if you has the

Owner Class

, with bool bike, bool car, bool motocycle... properties, witch you can write a

public bool[] ownArray() 

function, that can be evaluated.

I hope this helps.

Alexfr8
  • 56
  • 3
0
myEnum = 3;

...

switch (myEnum) {
   case 1: System.out.println("This will not print"); break;
   case 2: System.out.println("This will not print"); break;
   case 3: System.out.println("This will print!"); //no break!
   case 4: System.out.println("So will this, because there wasn't a break"); break;
   case 5: System.out.println("But this won't, there was a break");
}
NickJ
  • 9,380
  • 9
  • 51
  • 74
0

Switch-case in java could evaluate one case at a time. The moment it gets into one of the case, it would keep on executing the next statements (no matter, if those statement were part of other case ); until it hit a break;

In nutshell, A switch statement gives you the option to test for a range of values for your variables. They can be used instead of long, complex if … else if statements. The structure of the switch statement is this:

switch ( variable_to_test ) {
case value1:    code_here1;
               break;
case value2:   code_here2;
case value3:   code_here3;
             break;
default:
             values_not_caught_above;

}

In the above example if value2 is satifies, it will execute code_here2 and code_here3 as well (because there is no break statement)

Sadly, you can't test for a range of values after case, just the one value. So you can't do this:

case (user <= 18):
Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53