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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    quick switch case question

    Here is what i'm trying to do and I'm not sure if it will work with a switch, but i'd be better than writing a million if else statements.

    Code:
    var = c
    var = z
    
    switch (c)
    {
    case c > 1 && < 10:
    z = 5;
    break;
    case c > 10 && < 20:
    z = 15;
    break;
    default:
    z = 0;
    }
    I think you see what I'm trying to do. I have a variable 'c' and it is input by the user and I want to make variable 'z' set according to what 'c' is. I know the above code doesn't work, but I'm not sure why. I've tried a million different things to try and get it to work, but it always just goes to default when it displays 'z'. Any help would be great. Thanks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    You might do better to read the manual.

    The case labels in a switch statement are expressions whose values are compared to the value of the switch expression, not Boolean expressions.

    var = c
    var = z


    You can use also a switch as if it were a regular if/else but it is not normal to do so as it becomes in effect a simple if/else statement.

    Code:
    var c = 13;
    var z;
    
    switch (true) {
    case (c>1 && c <10):
    z = 5;
    break;
    case (c > 10 && c < 20):
    z = 15;
    break;
    default:
    z = 0;
    }
    alert (z);

    It is your responsibility to die() if necessary….. - PHP Manual


  •  

    Posting Permissions

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