'if/else' versus 'case' conditional switch

  • Thread starter clueless_google
  • Start date
C

clueless_google

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.
 
L

Lee

(e-mail address removed) 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.
 
V

vallini

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/
 
V

Vic Sowers

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)
 
V

vallini

===
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/
 
D

Douglas Crockford

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
 
L

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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 8 Oct 2005
15:18:35, seen in Lee
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).
 
L

Lee

Dr John Stockton said:
JRS: In article <[email protected]>, dated Sat, 8 Oct 2005
15:18:35, seen in Lee


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.
 
R

Rick

Lee said:
Vic Sowers said:
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.
 
L

Lee

Rick said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top