Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2003
    Location
    Sasolburg, South Africa
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can the switch statement handle floating point numbers

    Hi Everyone,

    I am experimenting with a switch statement in order to avoid using nested if... else statements. However, it appears there are certain limitations with regard to the usage of operators. For example the following code works fine:
    Code:
    var x=2;
    switch (x)
    {
      case 2:
        var y=2
      break;
      case 3:
        var y=3
      break;
      default:
        alert("Value of y: " +y);
    }
    But, once I use
    [code]
    var x=2;
    switch (x)
    {
    case =2:
    var y=2
    break;
    case 3:
    var y=3
    break;
    default:
    alert("Value of y: " +y);
    }
    [c/code]

    I get a syntax error message. Similarly when using [case >=2:] or [<=]. Using ["<=2"] avoids the error message, but 'var y' is not being calculated. I have seen examples where string variables such as "Oranges" and "Apples" etc. where used as well as integers. But what happens in the case when x is a floating point number and one needs to determine whether x <=2, or, x > 2 but smaller than say 5. Can this be done with the switch statement or is a nested if... else more appropiate?

    Please clarify.

    Regards
    Dieter

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The format of the switch statement is the following:
    Code:
    switch( switch expression ) {
        case case expression: statement list
        ...
        default: statement list
    }
    and the action performed in the switch statement is that the switch expression is compared to the case expression, and if they are equal to eachother, execution of the associated statement list commences. So, the only test you can perform in a JavaScript switch statement is equality. It does handle all possible types of objects, but the comparison is for equality.

    =2 as one of your case expressions was, is not a valid expression at all. Neither is <=2. "<=2" on the other hand, being a string literal, is a valid expression, but what you are doing then is that you make a comparison for equality between the switch expression and that string.


    You can't specify an operator in the switch statements. They work by equality comparison, and that's it.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Nov 2003
    Location
    Sasolburg, South Africa
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the quick response. I guess this clarifies it, but I am disappointed by the facts. I have coded a lot in VB and there the Select Case statement is quite powerful and capable of various comparisons. On the other hand, the Select Case statement used in VBScript also seems to be a watered-down version. But I suppose it has it's uses, depending on the application/requirements.

    Regards
    Dieter

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    This is the C/C++ version of the switch statement brought into JavaScript. It's strength is it's speed compared to if..else if..else statements, a speed boost it gets from being simpler.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you wish to do comparisons with switch, try this
    Code:
    switch( true )
    {
        case ( a > b ) :
            // code
            break;
        case ( b > a ) :
            // code
            break;
        default :
            // code
    }
    Note: "switch( true )" doesn't work in NS4.x, in case anyone cares.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Yeah, but that defeats the purpose of a switch statement anyways, as multiple if statements or an if..else if..else statement does the same and are a little better optimised for it. (Internally speaking, they don't have to enter an expression context since that is already hard coded into the functionality of the statement, so they save a scope resolution.)
    Last edited by liorean; 01-10-2004 at 04:25 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    New Coder
    Join Date
    Nov 2003
    Location
    Sasolburg, South Africa
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys,

    I have tried both ways and found both to provide the same answers. At the same time I learned something useful about the switch statement, which I am sure will come in handy some time in the future. Thanks beetle.

    Thanks liorean for pointing out the optimisation issue. In this particular case I will use the if....else if...else statement.

    Regards
    Dieter


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •