• Post Reply Bookmark Topic Watch Topic
  • New Topic

when to use an if statement or switch  RSS feed

 
Jack Ryu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hy everyone, I have a simple question about JavaScript but it can apply to Java or any programming language for that matter. When do you use an If statement vs and select case statement. Sometimes, I hear people say if you can reduces the the of if statement use a select case;however, from my experience, a select case cannot replace if statements all of the time. Personally, I feel that if you are comparing more than one variable, you would use an if statement otherwise a select case or switch statement will work just fine. If I correct in thinking this way or am I missing something.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37256
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack,
Yes, you'd use an if statement when comparing multiple variables. Or if you had ranges like if score > 90 else if score > 80, etc.

Switch is good when you are comparing values. Is letter "A" or "B" etc.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What applies to Java® may not necessarily apply to JavaScript. The two are completely unrelated languages.

You can use a switch when you have defined values to check for. Remember the values after case must be compile‑time constants. You can write "A" "B" as Jeanne suggested (Java7+ only) but you cannot writeYou can only use a switch if you can write a list of the values with different actions for each. I shall write a variant switch in a few minutes.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a valid switch, as long as everything after case is a compile‑time constant, and they are all different:-Note that a switch‑case can give very fast execution if used appropriately. There are better ways to write months; I would suggest an enumerated type.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Ryu wrote:Hy everyone, I have a simple question about JavaScript but it can apply to Java or any programming language for that matter.

Not true unfortunately, for the reason Campbell mentioned.

When do you use an If statement vs and select case statement.

It's often a matter of style, but my general rule is: Use switch when:
(a) You can.
(b) There are more than two options.
(c) It makes the logic more readable.

And in Java, (a) precludes quite a lot of cases where you might otherwise want to use it, because the value in each case statement MUST be a "compile time constant" - ie, a literal, primitive constant, or the name of an enum instance.

(b) is fairly straightforward: there's not much point in using a switch where a simple if will do. In some cases where there are exactly 3 options, I will use a compound conditional operator because it's a bit more compact, but it's very much a matter of style.

(c) is where the "style" really comes in. IMO, switch is almost always clearer than a long if...else chain, but not everyone will agree with me.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!