Skip case is not expected

A

Archos

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");
}
==
 
C

Captain Paralytic

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 lessthan 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");
        }
==

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

Jukka K. Korpela

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

Richard Cornford

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.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top