If Then Range


Closed Thread
Results 1 to 12 of 12

Thread: If Then Range

  1. #1
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382

    Default If Then Range

    I have run into this a number of times but have yet to come up with a good solution.

    I would like to do the following:

    If Variable= 5 thru 8 then
    ...do stuff here
    endif

    I have tried the AND, OR route with some success but I wasn't sure if that was my only option.

    Thoughts on the best way to do this?

  2. #2
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Default

    I would suggest the following:
    Code:
    If x => 5 Then
         If x <= 8 Then
         [do something code here]
         Endif
    Endif
    Of course there are probably other possibilities.

  3. #3
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    One problem, if something is greater than 8 then you miss it completely.

  4. #4
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Dynamo

    Mytechs code:

    If x => 5 Then
    If x <= 8 Then
    [do something code here]
    Endif
    Endif


    Dynamo>>One problem, if something is greater than 8 then you miss it completely<<

    If X is greather than 8, It will still work.

    Lets suppose X is 9.

    The first X statement says if X >= 5 do the next step.... it is, right?

    So the next step is "If X<=8" And it isn't... so it will fall through that iff statement and fall out of the primary If statement (if X >=5).

    Unless you have accidently left something out of your question??


    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  5. #5
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    You are correct, didn't think through it completely.

    I can do it that way if need be...I was just looking for a "cleaner" solution.

  6. #6
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Dynamo,

    Here is another way of expressing the same thing.
    Code:
    If x => 5 And x <= 8 Then
    [do something code here]
    Endif
    This does give a simpler look to your basic source code, but unfortunately it will cause PBP to compile it fatter (in otherwords it will take more program space in your PIC chip). Any time you use "And" in your If...Then statements you run this risk. The first code sample I gave you circumvents this problem, but does take a little getting used to.

    Go ahead and try both examples, compiling each, and see the difference in the amount of bytes used.

    edit: I just realized that you can save a heck of a lot more bytes by eliminating the "=>" and "<=" and replacing them with ">" and "<" (see examples below). Of course you will need to adjust your range values to compensate, but I think this would be well worth it for the greatly reduced program space needed.

    Code:
    If x > 4 Then
         If x < 9 Then
         [do something code here]
         Endif
    Endif
    
    ' -or-
    
    If x > 4 And x < 9 Then
    [do something code here]
    Endif
    Hint: Compile all the examples to see the differences in program space required.

    I hope that answers all your questions,
    Last edited by mytekcontrols; - 29th June 2005 at 00:48.

  7. #7
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    I did try your second example prior to posting. What I found was it includes everything leading up to 8. I think this has to do with the second condition in the statement. It was very odd to see.

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Code:
    If (x > 4) And (x < 9) Then
    [do something code here]
    Endif
    should fix it
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  9. #9
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    I will give it a try and see how it effects my code size.

    Thanks for all the help so far

  10. #10
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    You can test various comparison equations with something like this. Just
    compile each one separately.
    Code:
    X CON 10
    
    Main:
        IF (X=5) or (X<=8) THEN   ' 30 words
        @ nop 
        ENDIF
    Code:
    X CON 10
    
    Main:
        IF X => 5 THEN
           IF X < 9 THEN  ' 11 words
             @ nop
           ENDIF
        ENDIF
    Then you could do something like this to save yet a few more words.
    Code:
    X CON 10
    
    ' Upper boundary # = 8
    ' Lower boundary # = 5
    ' Range = 4  (5,6,7,8)
    
    ' IF unknown value (X - lower range #) < range, it's in range.
    
    Main:
        IF (X-5) < 4 THEN  ' 8 words
        @ nop
        ENDIF
    Just place some heavy-duty comments in there so you don't forget what the
    heck it's doing down-the-road...;o]
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  11. #11
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Thumbs up

    Hey Bruce,

    Wow I had to look at that last example a couple times before it sunk in! But sure enough it does the trick with the least amount of code required. Bravo!

  12. #12
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    Bruce you rock. Between Bruce and Melanie they are PICBasic sages.

Similar Threads

  1. Expanding Timer1 counting range
    By sycluap in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 3rd February 2014, 22:00
  2. using a pot to control a time range
    By glkosec in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th June 2008, 19:59
  3. Long range communication
    By Christopher4187 in forum Off Topic
    Replies: 10
    Last Post: - 6th February 2008, 16:12
  4. Sharp GP2D12 Range Finder???
    By Gixxer in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 13th December 2007, 09:21
  5. HPWM frequency range
    By jcsquire in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 16th June 2006, 02:11

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

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