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 4 of 4
  1. #1
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,025
    Thanks
    56
    Thanked 567 Times in 564 Posts

    shorthand for else if?

    hello,

    I have googled (I promise) but I can't find any examples... basically my code is getting bogged down in if/else if/ else statements, and I am wondering if there is a shorthand. I know (I think) that you can do if/else like this:

    variable = (condition) ? trueValue : falseValue;

    but can you add an else if into the code?

    thanks in advance.

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Hy,
    Try this:
    Code:
    variable = (condition) ? trueValue : ((condition) ? trueValue : falseValue);

  • Users who have thanked MarPlo for this post:

    xelawho (07-02-2011)

  • #3
    Regular Coder
    Join Date
    Jun 2011
    Posts
    103
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Word of caution here, it is not recommended to nest ternary statements....as they can sometimes lead to unexpected results.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,025
    Thanks
    56
    Thanked 567 Times in 564 Posts
    thank you for the word of caution.

    I actually ended up using a switch in the end - partially because it is more intuitive to read (for me, anyway), but I tested MarPlo's code and it worked perfectly.


  •  

    Posting Permissions

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