Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #10386 > unrolled thread

Skip case is not expected

Started byArchos <raul.san@sent.com>
First post2012-01-19 04:46 -0800
Last post2012-01-19 05:36 -0800
Articles 4 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Skip case is not expected Archos <raul.san@sent.com> - 2012-01-19 04:46 -0800
    Re: Skip case is not expected Captain Paralytic <paul_lautman@yahoo.com> - 2012-01-19 05:11 -0800
      Re: Skip case is not expected "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-01-19 15:34 +0200
    Re: Skip case is not expected Richard Cornford <Richard@litotes.demon.co.uk> - 2012-01-19 05:36 -0800

#10386 — Skip case is not expected

FromArchos <raul.san@sent.com>
Date2012-01-19 04:46 -0800
SubjectSkip case is not expected
Message-ID<aa1b4e7f-7175-476f-b114-f52dbad34b4e@l19g2000yqj.googlegroups.com>
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");
	}
==

[toc] | [next] | [standalone]


#10387

FromCaptain Paralytic <paul_lautman@yahoo.com>
Date2012-01-19 05:11 -0800
Message-ID<c968f646-614f-4c8d-bdeb-819738fc16cf@18g2000yqe.googlegroups.com>
In reply to#10386
On Jan 19, 12:46 pm, Archos <raul....@sent.com> wrote:
> 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");
>         }
> ==

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.

[toc] | [prev] | [next] | [standalone]


#10388

From"Jukka K. Korpela" <jkorpela@cs.tut.fi>
Date2012-01-19 15:34 +0200
Message-ID<jf964t$7fa$1@dont-email.me>
In reply to#10387
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/

[toc] | [prev] | [next] | [standalone]


#10389

FromRichard Cornford <Richard@litotes.demon.co.uk>
Date2012-01-19 05:36 -0800
Message-ID<fe86d632-6def-42e9-8a1b-fa410a6fb948@m4g2000vbc.googlegroups.com>
In reply to#10386
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web