/    Sign up×
Community /Pin to ProfileBookmark

How to write a ‘select case’ with one ‘case’ set to different values

Which is the correct way to write it?

The idea would be: (in bold)

[code]
function checkLogo() {
var cert_A = document.form_newOffer.certAut.Value;

switch(cert_A) {

[B]case “1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or15 or 16 or 17”:[/B]

if (…) {

} else {
… }
break;

[B]case “18 or 19 or 20 or 21 or 22”:[/B]

if…
[/code]

Or maybe it is better to use something like this:

[code]
function checkLogo() {
var cert_A = document.form_newOffer.certAut.Value;

[B]if (cert_A == 18 or 19 or 20 or 21 or 22)[/B] {

} else {… }
[/code]

How can I do it?

Thank you,
Cesar

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisAug 24.2004 — If you only have two conditions, I would use the "if" statement.

Here is an interesting twist on the switch/case for more flexibility:
<i>
</i>switch(true) {
case (cert_A &lt;= 17):
//
break;
case ((cert_A &gt;= 18) &amp;&amp; (cert_A &lt;= 22)):
//
break;
default:
//
}
Copy linkTweet thisAlerts:
@BillyRayAug 24.2004 — Either way, you would not use "or" - you would use "||".

Dan
Copy linkTweet thisAlerts:
@erick30authorAug 24.2004 — Hi Gil, your code works fine ?:

<i>
</i>switch(true) {
case (cert_A &lt;= 17) &amp;&amp; (cert_A != 0):
//
break;
case ((cert_A &gt;= 18) &amp;&amp; (cert_A &lt;= 22)):
//
break;
default:
//
}


But I have tried it with an "if" statement and it doesn' t work:

<i>
</i>if (cert_A != 0) {

if (cert_A &lt;= 7) {
...
} else {
if ((cert_A &gt;= 18) &amp;&amp; (cert_A &lt;= 22)) {
.... }
}

} else {
... }



What is wrong?
Copy linkTweet thisAlerts:
@steelersfan88Aug 24.2004 — // Try:
if ((cert_A &lt;= 7) &amp;&amp; (cert_A &gt; 0)) {
// number is 1,2,3,4,5,6,7
}
else if ((cert_A &gt;= 18) &amp;&amp; (cert_A &lt;= 22)) {
// number is 18,19,20,21,22
}
else {
// all other numbers
}
Your if statement was a little confusing ...
Copy linkTweet thisAlerts:
@erick30authorAug 24.2004 — If I need to use 'else if' perhaps it is better to use 'Select Case'
Copy linkTweet thisAlerts:
@steelersfan88Aug 24.2004 — Switch ... Case should only be used when you have a group of several equality situations as a list. In other words, never use switch/case when you are using [part-]inequality operations. Never use switch case when you have more than when comparison.

In this case, I believe the if ... then is the best option. I will give my advice > never use switch case. You'll always be safer, and more understanding use if statements. Others will tell you dif't.
×

Success!

Help @erick30 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...