Results 1 to 8 of 8

Thread: Code - a value between two values?

  1. #1
    Join Date
    Jun 2005
    Location
    Tunbridge Wells, Kent
    Posts
    206
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Code - a value between two values?

    is there short hand when evaulating if a value is between/within two values?

    ie, instead of

    if ((value1 > value 2) && (value1 < value3)) {}

  2. #2
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 24 Times in 24 Posts

    Default

    not that I can think of, except you shouldn't need so many brackets:
    Code:
    if (value1 > value2 && value1 < value3) {}

  3. #3
    Join Date
    Jun 2005
    Location
    Tunbridge Wells, Kent
    Posts
    206
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default

    if i always wondered if there was a shorthand for evaluating an integer against being between two other integers resulting in a boolean perhaps,

    ie, if (value1[value2,value3]) {}

  4. #4
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 24 Times in 24 Posts

    Default

    On paper I would probably write
    Code:
    (value2<value1<value3)
    I don't know of any programming language that lets you write the statement more efficiently though, however there maybe one that provides a function like:
    Code:
    is_between(value1, value2, value3)
    (and if there isn't you could write your own)

    I don't think you would gain anything with it though. At the end of the day to work out whether one number is between two others a computer has to do two separate comparisons, and I expect the most efficient and quick way of writing that is still:
    Code:
    (value1>value2 && value1<value3)
    And as a human being trying to read the code, it also seems better having the comparison written clearly and obviously.
    Last edited by nick; 5th April 2007 at 01:14 PM. Reason: couldn't spell

  5. #5
    Join Date
    Jun 2005
    Location
    Tunbridge Wells, Kent
    Posts
    206
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default

    guess that's a no then.

    like you say, if it's easy to read, it's probably the only option!

  6. #6
    Join Date
    Jun 2005
    Posts
    1,081
    Thanks
    4
    Thanked 15 Times in 15 Posts

    Default

    Quote Originally Posted by nick View Post
    I don't know of any programming language that lets you write the statement more efficiently though
    How about Transact-SQL which would let you do:

    Code:
    SELECT field1, field2 FROM someTable WHERE @value BETWEEN begin_expression AND end_expression

  7. #7
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 24 Times in 24 Posts

  8. #8
    Join Date
    Jun 2005
    Location
    Tunbridge Wells, Kent
    Posts
    206
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default

    that's the code that had me asking questions sol!!

    taping away using between on an sql query, i wondered... (waving retro dream like... waves)...

    'do other programming languages use this short hand?'

    the answer so far is no (for the ones i use, anyhow)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. ASP Files Display Code In Browser
    By MWF in forum ASP (VBScript)
    Replies: 9
    Last Post: 26th July 2006, 01:24 PM
  2. code needed for clock countdown script
    By Space Cowboy in forum PHP
    Replies: 8
    Last Post: 15th June 2006, 01:15 AM
  3. Which form to email ASP Code
    By s80wkr in forum Technical Support
    Replies: 1
    Last Post: 31st May 2006, 03:08 PM
  4. browserCaps code
    By jaimalchohan in forum ASP.NET
    Replies: 0
    Last Post: 9th November 2005, 03:08 PM
  5. specific code for specific browsers
    By Space Cowboy in forum Development Support
    Replies: 4
    Last Post: 30th August 2005, 11:15 AM

Bookmarks

Posting Permissions

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