Logical AND/OR/ Sequencing question


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default Logical AND/OR/ Sequencing question

    Hello.

    Say here is a logical structure like this

    IF A=1 AND B<6 OR A=2 THEN

    The first part is clear, if a=1 and b is less than 6, then do something
    but what about OR?
    when I add that OR A=2 this means B still should be less than 6, or that part is ignored and only A=2 is checked?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Logical AND/OR/ Sequencing question

    AND, OR, XOR, ANDNOT, ORNOT, XORNOT all have the same precedence and so are evaluated from left to right. But, as the manual says:
    Left-to-right is, in our opinion, unacceptable as a method of specifying the order of
    evaluation. USE PARENTHESES to avoid ambiguity!
    IF (A=1 AND B<6) OR (A=2) THEN makes it much clearer.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Logical AND/OR/ Sequencing question

    OT

    Have you got your HTC dot matrix ? on your other posting http://www.picbasic.co.uk/forum/show...445#post146445

  4. #4
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Logical AND/OR/ Sequencing question

    Nope, it is still on the way, will get it in week or so.

  5. #5
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Logical AND/OR/ Sequencing question

    So my question will be split into things like

    IF A=1 AND B<6

    OR B<6 AND A=2

    I want to make it such way, than if A=2, then it would not care about whenever B is less than 6 or not.


  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Logical AND/OR/ Sequencing question

    Have you TRIED it? Sometimes simply trying a few things out on your own gives you the answer much faster than asking here.
    Code:
    IF (A=1 AND B<6) OR (A=2) THEN
       LED = 1
    ENDIF
    Whenever A = 2 LED will turn on.
    If A = 1 then B must also be <6 for LED to turn on.

    Ugly and verbose but likely smaller:
    Code:
    IF A = 2 THEN LED = 1
    IF (A = 1) AND (B < 6) THEN LED = 1
    /Henrik.

  7. #7
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Logical AND/OR/ Sequencing question

    Also, when using IF statements, it will probably take fewer lines of ASM code to execute if you put the clause most likely to give you a "yes", or the easiest to check first. IF (A = 2)... is the easiest to check so I'd put it first.

Similar Threads

  1. Ports don't stay on when sequencing them.
    By cnc002 in forum FAQ - Frequently Asked Questions
    Replies: 5
    Last Post: - 5th August 2017, 16:08
  2. Logical vs Bitwise ==
    By Archangel in forum General
    Replies: 7
    Last Post: - 19th February 2010, 06:22
  3. Understanding 3 phase sequencing
    By ardhuru in forum General
    Replies: 3
    Last Post: - 22nd June 2008, 10:14
  4. Logical Operators
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 1st May 2008, 20:55
  5. 3 phase sequencing
    By ardhuru in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 26th May 2007, 07:35

Members who have read this thread : 1

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