By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

'if/else' versus 'case' conditional switch

P: n/a
hello. i've been beating my head against a wall over this for too
long.

setting the variables 'z' or 'y' to differing numbers, the following
'if/else' code snippet works fine; however, the 'case' code snippet
does not. (the code's function is illustrative.)

//////////////////////////////////////////
//////// working 'if/else' switch ////////
//////////////////////////////////////////

var z=3 /* set 'z' to any number */

if (z<10)
{
document.write("The value of 'z' entered is less than ten.
<br><br>")
}
else if (z==10)
{
document.write("The value of 'z' entered is exactly equal to
ten.<br><br>")
}
else if (z>10)
{
document.write("The value of 'z' entered is greater than ten.
<br><br>")
}

//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////

//////////////////////////////////////////
/////// non-working 'case' switch ////////
//////////////////////////////////////////

var y=15 /* set 'y' to any number */

switch (y)
{
case (y<10):
document.write("The value of 'y' entered is less than ten.
<br><br>")
break
case (y==10):
document.write("The value of 'y' entered is exactly ten. <br><br>")
break
case (y>10):
document.write("The value of 'y' entered is greater than ten.
<br><br>")

//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////

any simple changes that will make the 'case' switch work?

tia,

c.

Oct 8 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Lee
cl*************@hotmail.com said:
//////////////////////////////////////////
/////// non-working 'case' switch ////////
//////////////////////////////////////////

var y=15 /* set 'y' to any number */
switch (y)
{
case (y<10):
case (y==10):

case (y>10): any simple changes that will make the 'case' switch work?


Reading the manual might help.

http://docs.sun.com/source/816-6408-10/stmt.htm#1018610

The case labels are supposed to be expressions whose values
are compared to the value of the switch expression, not
boolean expressions.

Oct 8 '05 #2

P: n/a
A reasonable misunderstanding for a person relatively new to javascript
(or coding).

The if/else conditional checks are NOT equivalent to a switch check.
Not.

Examine what the structure of a switch is:
switch(y)

you see, you passed already a variable, y.
As such, it is regarded as IMPLIED within all the case statements,
which IMPLICITLY compare it against the value passed to the case (so:
don't repeat 'y' within the case statements):

switch(y){
case 202:

break;
case 'dunno':

break;
}

that is, a switch statements gets in the variable y, and the CASE
statements gets in its possible or presumed VALUES ONLY.

As such, the limitation is that a switch statement isn't but an
if(y==something)
check in disguise.
The stress falls on the == operator.

A switch checks ONLY equivalences, NOT other type of comparisons: == is
IMPLIED.

You should consider it the same as passing arguments to a function,
with which perhaps you're already familiar:
function foo(arg){alert(arg)}
when you invoke that signature, the arg named variable is replaced with
its VALUES:
foo('hallo');
that obviously enough triggers al alert with written in it 'hallo'.

You do NOT repeat arg, in fact:
foo(arg='hallo');
that's wrong.

Please note that in some languages, like PHP, the SIGNATURE (NOT in the
invocation, that is) of a function can instead appear in that form,
which means assigning a DEFAULT:
function foo($arg='hallo'){print $arg;}
which means: this function has a DEFAULT value for the given argument
which is 'hallo' in case NO argument is passed upon invocation:
foo(); //prints the default 'hallo'
foo('hi'); //overrides the default, prints 'hi'
The SAME happens with the switch statement's logics.

I hope this brings in the sense of some coherence, and a lasting or
better insight in the whys of the issue that puzzles you.

If you wonder why it has not been arranged otherwise, I may concede a
switch plus plus allowing for more articulated comparisons say
switch(fantasy){
case (<1):

break;
case(>=2):

break;
}
might have been useful, but the fact is: we use if/else for the rest.
For mere == equivalences, we have the ALTERNATIVE of a switch
statement.

ps better if you put a semiciolon after the break
break;
makes things a bit more clear - stresses that a case ends that is.
Nothing more.

Alberto
http://www.unitedscripters.com/

Oct 9 '05 #3

P: n/a

<cl*************@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
hello. i've been beating my head against a wall over this for too
long.

setting the variables 'z' or 'y' to differing numbers, the following
'if/else' code snippet works fine; however, the 'case' code snippet
does not. (the code's function is illustrative.)

//////////////////////////////////////////
//////// working 'if/else' switch ////////
//////////////////////////////////////////

var z=3 /* set 'z' to any number */

if (z<10)
{
document.write("The value of 'z' entered is less than ten.
<br><br>")
}
else if (z==10)
{
document.write("The value of 'z' entered is exactly equal to
ten.<br><br>")
}
else if (z>10)
{
document.write("The value of 'z' entered is greater than ten.
<br><br>")
}

//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////

//////////////////////////////////////////
/////// non-working 'case' switch ////////
//////////////////////////////////////////

var y=15 /* set 'y' to any number */

switch (y)
{
case (y<10):
document.write("The value of 'y' entered is less than ten.
<br><br>")
break
case (y==10):
document.write("The value of 'y' entered is exactly ten. <br><br>")
break
case (y>10):
document.write("The value of 'y' entered is greater than ten.
<br><br>")

//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////

any simple changes that will make the 'case' switch work?

tia,

c.


replace:
switch (y)
with:
switch (true)
Oct 9 '05 #4

P: n/a
===
replace:
switch (y)
with:
switch (true)
===

absolutely true: with this trick you can use also a switch as if it
were a regular if/else - the reason it is not used is, arguably, that
the line switch(true) appears pleonastic, unnecessary for the purpose
that is, therefore it is normally used the traditional check by
if/else.

The following test SEEMS to prove if/else as being slightly faster than
switch at the same tasks, at least the ones below:

<script language="JavaScript"><!--

/*using SWITCH*/
function foo(){
y=2;
switch(true){
case y>1:
y=10;
break;
case y<1:
y=20;
break;
}
}

var t=new Date();
var tm1=t.getTime();

for(var b=0; b<300000; b++){foo();}

t=new Date();
var tm2=t.getTime();
alert(tm2-tm1);

/*using IF ELSE*/
function foo2(){
y=2;
if(y>1){y=10;}
else if(y<1){y=20;}
}

t=new Date();
tm1=t.getTime();

for(var b=0; b<300000; b++){foo2();}

t=new Date();
tm2=t.getTime();
alert(tm2-tm1);
//--></script>

Alberto
http://www.unitedscripters.com/

Oct 9 '05 #5

P: n/a
> switch (y)
{
case (y<10):
document.write("The value of 'y' entered is less than ten.")
break
case (y==10):
document.write("The value of 'y' entered is exactly ten. <br><br>")
break
case (y>10):
document.write("The value of 'y' entered is greater than ten.


Programming languages have very specific rules for what forms they
accept and what their meanings are. You can't just make up your own
syntax and expect it to work.

Programming requires knowledge. As you have seen, programming in
ignorance is not effective. Some of the information you need can be
found here: http://www.crockford.com/javascript/survey.html
Oct 9 '05 #6

P: n/a
Lee
Vic Sowers said:
replace:
switch (y)
with:
switch (true)


That will work in Javascript, but I think it's a poor practice,
if only because it will NOT work in many other languages, which
require that case option values be constants so that the branch
table can be compiled.

Oct 9 '05 #7

P: n/a
JRS: In article <di*********@drn.newsguy.com>, dated Sat, 8 Oct 2005
15:18:35, seen in news:comp.lang.javascript, Lee
<RE**************@cox.net> posted :
The case labels are supposed to be expressions whose values
are compared to the value of the switch expression, not
boolean expressions.


You go too far.

The case labels can usefully be boolean expressions if the switch
expression has the value true (or false, but that would be strange).
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 9 '05 #8

P: n/a
Lee
Dr John Stockton said:

JRS: In article <di*********@drn.newsguy.com>, dated Sat, 8 Oct 2005
15:18:35, seen in news:comp.lang.javascript, Lee
<RE**************@cox.net> posted :
The case labels are supposed to be expressions whose values
are compared to the value of the switch expression, not
boolean expressions.


You go too far.

The case labels can usefully be boolean expressions if the switch
expression has the value true (or false, but that would be strange).


I suppose I was editorializing a bit. I didn't say that they
"must not" be boolean expressions, simply that they aren't
"supposed" to be. It can be useful, but I don't happen to like
the practice because it won't work for switch constructs in some
other languages.

Oct 9 '05 #9

P: n/a
Lee wrote:
Vic Sowers said:
replace:
switch (y)
with:
switch (true)
That will work in Javascript, but I think it's a poor practice,
if only because it will NOT work in many other languages, which
require that case option values be constants so that the branch
table can be compiled.


How good it is to use in JavaScript depends on:
* Is it legal and will it work with all or most browsers?
* Is the code easy to follow?

This passes those tests. Reading the code for the first time someone
would do a double take, but they would figure it out quickly. After
that, it is clear.

Compiled languages often compile switches into a branch table. They also
want integer constants. The cases can't be strings. Strings are
commonly used with case statements in interpreted languages.

He isn't using other languages. If he tries it in another language, he
will get an error message.
Oct 9 '05 #10

P: n/a
Lee
Rick said:

Lee wrote:
Vic Sowers said:
replace:
switch (y)
with:
switch (true)
That will work in Javascript, but I think it's a poor practice,
if only because it will NOT work in many other languages, which
require that case option values be constants so that the branch
table can be compiled.

He isn't using other languages. If he tries it in another language, he
will get an error message.


And most likely, waste bandwidth on that language's newsgroup
asking why this code that "should work", doesn't. That's enough
for me to want to discourage it.

Oct 9 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.