Re: Switch() Statement Not Working
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Switch() Statement Not Working

From: Lasse Reichstein Nielsen <lrn@hotpop.com>
Date: Wed Feb 01 2006 - 02:12:54 CET

rdavis7408@gmail.com writes:

> The calculation worked until I added the switch statement. Why is that?

> switch(b)
> {
> Case >=1.25 && <=1.30:
> this.form.dlnt.value=.01;
> Case >=1.31 && <=1.369:
> this.form.dlnt.value=.02;
> Case >=1.37 && <=1.429:
> this.form.dlnt.value=.03;
> default:
> this.form.dlnt.value=.99
>
> }

You are guessing blindly. That rarely works in programming, since
computers are notoriously bad at guessing what you really meant.

The syntax of a case statement is:
  case <expression>: <statement>

Example:

 switch(num){
   case 1: // something for 1
           break;
   case 2: case 3: // something for 2 or 3
           break;
   default: // ...
 }

In your case, you want to match intervals, not values, so
a switch statement isn't the immediate choice. Instead
use a sequence of if-statements:

 if (b >= 1.25 && b <= 1.30) {
     this.form.dlnt.value=.01;
 } else if (b > 1.30 && b < 1.37) {
     ...
 } else if (b >= 1.37 && b < 1.43) {
     ...
 } else {
     ...
 }

You can get the same effect with a switch, but it's not really
smarter, and it's much less readable.

 switch(true) {
   case (b >= 1.25 && b <= 1.30) :
     this.form.dlnt.value=.01;
     break;
   case (b > 1.30 && b < 1.37) :
     ...
     break;
   ...
 }

/L

-- 
Lasse Reichstein Nielsen  -  lrn@hotpop.com
 DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'
Received on Tue Feb 7 21:29:28 2006