2

Hello
Out of interest, when programming in java, would it be more economical to use an if statement or a switch statement for something that could only have two results.

For example:

char c = 'a';
if (c == 'a') {
   //do something
}
else if (c == 'A') {
   //do something else
}

or

char c = 'a';
switch (c) {

   case 'a':
      //do something
      break;

   case 'A':
      //do something else
      break;
}



Which would be better?

Paul Alexander
  • 2,686
  • 4
  • 33
  • 69
  • 3
    count the line-number and indentation. I think, for this case it's obvious enough – injecteer Sep 18 '14 at 14:58
  • If it can only have 2 results wouldn't be better to have only "else" instead of else if? – Wabonano Sep 18 '14 at 14:59
  • This question has already been asked several times. See http://stackoverflow.com/questions/2086529/what-is-the-relative-performance-difference-of-if-else-versus-switch-statement-i – Mikel Urkia Sep 18 '14 at 14:59
  • 1
    Note that if the purpose of the `if-else` statement is to assign one value or another (but limited to only one of these two possibilities) then the simplest way is to use a ternary operation. – mdl Sep 18 '14 at 15:03
  • Performance-wise you're probably not going to see much difference. In your case, it appears to be more a matter of personal preference. I'd go with the `if/else if` or `if/else` because it's what I write more often for simple cases such as that. – M7Jacks Sep 18 '14 at 15:37

4 Answers4

2

The answer is really depends on the interest of usage. What usually I do is, If my conditions are more (3 or above) I'l go for switch. Otherwise I simply use if-else-if.

Java switch statement with two cases or if statement?

For your case the first is good enough which is if else. If there are more cases like that, prefer switch. That increases readability versus ugly good amount of if else's.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Go with if else, if you are sure your just have 2 options. If you're not sure and it's a bigger project you're working on, I'd go with switch case because its easier to add cases if needed.

Zuop
  • 584
  • 5
  • 7
  • 21
1

In your case, with only two values, the if-else statement is faster than the switch statement. But it is not always true, especially when you have many cases to test. You will find more information here :

Why switch is faster than if

PS : One thing i noticed, but maybe it is needed by your code : if the element to test can only have two values, if-else should be better than if-else if.

Hope it helps.

Community
  • 1
  • 1
Snorky35
  • 426
  • 6
  • 15
0

A switch statement is often considered more error prone than an if-else i.e. forgetting a default case or break. In the case of a short condition say two results an if-else is probably preferable, it is unlikely there will be any discernible difference in speed.

Aside, Robert Martin in Clean Code p37 puts forward a good argument why switch statements are not very OOP.

clD
  • 2,523
  • 2
  • 22
  • 38