Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Skip case is not expected

1 view
Skip to first unread message

Archos

unread,
Jan 19, 2012, 7:46:05 AM1/19/12
to
The output on this code is not what I was expecting.
It prints "This won't be printed anyway" but I was expeting that it
were to run the first case. Whats' wrong?

==
var i = 6; switch (i) {
case i < 10:
console.log("i = " + i + " " + "is less than 10\n"); break;
case i > 10: case i < 0:
console.log("i = " + i + " " + "is either bigger than 10 or less
than 0\n"); break;
case i === 10:
console.log("i = " + i + " " + "is equal to 10\n"); break;
default:
console.log("This won't be printed anyway\n");
}
==

Captain Paralytic

unread,
Jan 19, 2012, 8:11:12 AM1/19/12
to
I think you meant to say "what's wrong?"

Well what is wrong is that you are making up your own syntax. If you
want to do that you must also build your own language interpreter.

switch(i) Says examine the value of i
case value The values need to be specific values that the value of i
can be compared with.

Try replacing switch(i) with switch(true) and put your logical
expressions in brackets.

Jukka K. Korpela

unread,
Jan 19, 2012, 8:34:24 AM1/19/12
to
2012-01-19 15:11, Captain Paralytic wrote:

>> var i = 6; switch (i) {
>> case i< 10:
[...]
> Well what is wrong is that you are making up your own syntax.

Technically, the syntax is correct. This is a matter of misunderstanding
the meanings (semantics).

> switch(i) Says examine the value of i
> case value The values need to be specific values that the value of i
> can be compared with.

So what happens here is that the expressions in case clauses are
evaluated, and whether they evaluate to true or false, none of them
matches the value of i.

> Try replacing switch(i) with switch(true) and put your logical
> expressions in brackets.

Using switch(true) is possible but does not make the code particularly
readable. Good old if statements are easier to read.

I don't see why the expressions would need to be put in brackets, or in
parentheses, in case clauses.

--
Yucca, http://www.cs.tut.fi/~jkorpela/

Richard Cornford

unread,
Jan 19, 2012, 8:36:21 AM1/19/12
to
Your problem is that you are comparing the value of - i - with the
results of the expressions used in the - case -s. Those expressions,
such as - I < 10 -, will have Boolean results, so only true or false.
Code that will do what you appear to want would be more like:-

<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var i = 10;
switch (true) {
case i < 10:
document.write("i = "+i+" is less than 10\n");
break;
case i > 10:
case i < 0:
document.write(
"i = "+i+" is either bigger than 10 or less than 0\n"
);
break;
case i === 10:
document.write("i = "+i+" is equal to 10\n");
break;
default:
document.write("This won't be printed anyway\n");
}
</script>
</body>
</html>

- where - true - is used with the - switch -, and then matches
whichever (or rather, the first) of - case - expressions evaluates to
true. However, that is a very non-obvious piece of code and I would
tend to avoid it (probably using an - if/else - structure instead in
this situation).

Richard.
0 new messages