Shop OBEX P1 Docs P2 Docs Learn Events
IF THEN with OR — Parallax Forums

IF THEN with OR

ThadThad Posts: 7
edited 2006-11-21 17:11 in General Discussion
Is there a way to do an if then with an or in such as:

If X = 1 or P = 7 then


Thanks
Thad

Comments

  • Keith MKeith M Posts: 102
    edited 2006-11-20 19:27
    This has come up before. I don't think so. You can't have multiple conditions in an IF statement as far as I know. Parallax gave an answer about a year or so ago when I inquired, and it has to do with how to generate a corresponding codeblock in assembly.

    I have usually worked around it.

    Something like

    
    ......
    if X=1 then somesubroutine
    if P=7 then somesubroutine
    ......
    ...
    ..
    
    somesubroutine:
    
    do the same stuff for x=1 and p=7 here
    
    RETURN
    
    



    should work.
  • PJMontyPJMonty Posts: 983
    edited 2006-11-20 19:54
    Keith,

    Actually your answer has a potential subtle bug in it - If both conditions happen to be true at the same time, then "somesubroutine" will get executed twice. If you want to prevent this possibility and SX/B won't allow an OR, the better solution is something like this:

    ConditionMet = 0
    
    If X = 1 then ConditionMet = 1
    
    If P = 7 then Condition Met = 1
    
    If ConditionMet = 1 then Somesubroutine
    



    By using a separate flag to see if either (or both) conditions are met, you can then check the flag and call the subroutine exactly once. This exactly meets the definition of the OR truth table which looks like this:

    0 or 0 = 0
    0 or 1 = 1
    1 or 0 = 1
    1 or 1 = 1
    



    This technique can also be extended to have as many conditional checks as you like. You just have to make sure the flag is cleared before the first conditional check so it is in a known state. You definitely do not want to do this:

    if X = 1 then ConditionMet = 1 else ConditionMet = 0
    if P = 7 then ConditionMet = 1 else ConditionMet = 0
    



    ...or else you end up turning the flag off unless both "If/Then" checks are true, or if only the second condition is true. Not the same results at all.

      Thanks, PeterM
  • BeanBean Posts: 8,129
    edited 2006-11-20 21:03
    Thad said...
    Is there a way to do an if then with an or in such as:

    If X = 1 or P = 7 then


    Thanks
    Thad
    Like this: [noparse][[/noparse]fixed, Thanks PJMonty]


    DO
      IF X <> 1 THEN
        IF P <> 7 THEN EXIT
      ENDIF
      ' Do whatever for IF X=1 OR P=7 THEN
     
    LOOP NEVER
     
     
    


    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com
    Stuff I'm selling on ebay http://search.ebay.com/_W0QQsassZhittconsultingQQhtZ-1

    "People who are willing to trade their freedom for·security deserve neither and will lose both." Benjamin Franklin


    Post Edited (Bean (Hitt Consulting)) : 11/21/2006 1:34:15 PM GMT
  • PJMontyPJMonty Posts: 983
    edited 2006-11-21 06:29
    Bean,

    Like Keith, you have a subtle bug in your answer. If you enter the "Do" with P =7 and X = anything other than 1, then the first conditional check will drop execution out of the loop and thus never get to the conditional check for P = 7 which is true. This means that you'll never execute when P = 7 unless X = 1, which is not an "OR", but an "AND". The flag solution I presented earlier doesn't suffer from this problem.
      Thanks, PeterM
  • BeanBean Posts: 8,129
    edited 2006-11-21 13:34
    Dang, that's what I get for being in a hurry. I have corrected the code above. It should work now.

    Thanks for the catch Peter,

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com
    Stuff I'm selling on ebay http://search.ebay.com/_W0QQsassZhittconsultingQQhtZ-1

    "People who are willing to trade their freedom for·security deserve neither and will lose both." Benjamin Franklin
    ·
  • ThadThad Posts: 7
    edited 2006-11-21 16:51
    Hi,
    I Like Bean's solution because it doesn't require any additional variables to accomplish the conditional statement.
    On a slightly different note, Of the hundreds of bytes of ram available on the sx48, why does it keep telling me I am out of it when I am only trying to use 20 bytes?

    Thad
  • BeanBean Posts: 8,129
    edited 2006-11-21 17:11
    Thad,
    Most of the RAM is available only for arrays.
    In most cases you can use an array element instead of a simple byte variable.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com
    Stuff I'm selling on ebay http://search.ebay.com/_W0QQsassZhittconsultingQQhtZ-1

    "People who are willing to trade their freedom for·security deserve neither and will lose both." Benjamin Franklin
    ·
Sign In or Register to comment.