• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

when to use an if statement or switch

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41502
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 77157
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 77157
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic