Using "If then" and "and" together


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

    Default Using "If then" and "and" together

    I have a keypad with four numbers. I'm able to decode the keys just fine but it's the password I'm having trouble with. I'm trying to do something like this:

    Code:
    IF CODE_ONE=13 AND CODE_TW0=13 AND CODE_THREE=16 AND CODE_FOUR=21 AND CODE_FIVE=32 THEN....
    but PBP gives me a syntax error. I realize this is probably archaic but I'm wondering two things.

    1. Why won't PBP let me compile this code?

    2. I realize this code is not efficient. How can I improve it?

  2. #2
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Using "If then" and "and" together

    Ok, so apparently I'm not intelligent enough to realize the difference between a 0 (zero) and O (the letter O)

    If there is a way to make this code better, please let me know.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Using "If then" and "and" together

    Hi,
    You could try a bunch of nested IF statements. It won't be "better" but it might compile to smaller code - worth a try.
    Code:
    IF CODE_ONE = 13 THEN
      IF CODE_TWO = 13 THEN  'With an oh
        IF CODE_THREE = 16 THEN
          IF CODE_FOUR = 21 THEN
            IF CODE_FIVE = 32 THEN
              'Correct code
            ENDIF
          ENDIF
        ENDIF
      ENDIF
    ENDIF
    Another option would be to use an array instead of "discrete" variables and iterate thru it with a for-next loop. Might be worth a try.

    /Henrik.

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: Using "If then" and "and" together

    Using PBP 2.60C, MCSP v2.2.1.1, MPASM v5.46, PIC 18F46K22.

    62 bytes:
    Code:
    @   __CONFIG    _CONFIG1H, _FOSC_HSHP_1H & _PLLCFG_OFF_1H & _PRICLKEN_OFF_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    @   __CONFIG    _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L
    @   __CONFIG    _CONFIG2H, _WDTEN_OFF_2H
    @   __CONFIG    _CONFIG3H, _PBADEN_OFF_3H & _HFOFST_OFF_3H & _MCLRE_EXTMCLR_3H
    @   __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    DEFINE OSC 20
    ANSELA = 0
    ANSELB = 0
    ANSELC = 0
    ANSELD = 0
    ANSELE = 0
    TRISA = %00000000
    TRISB = %00000000
    TRISC = %00000000
    TRISD = %10000000
    TRISE = %00000000
    
    CODE_ONE        VAR PortB.1
    CODE_TWO        VAR PortB.2
    CODE_THREE      VAR PortB.3
    CODE_FOUR       VAR PortB.4
    CODE_FIVE       VAR PortB.5
    
      IF CODE_ONE = 13 THEN
        IF CODE_TWO = 13 THEN  'With an oh
          IF CODE_THREE = 16 THEN
            IF CODE_FOUR = 21 THEN
              IF CODE_FIVE = 32 THEN
                'Correct code
              ENDIF
            ENDIF
          ENDIF
        ENDIF
      ENDIF
    END
    138 bytes:
    Code:
    @   __CONFIG    _CONFIG1H, _FOSC_HSHP_1H & _PLLCFG_OFF_1H & _PRICLKEN_OFF_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    @   __CONFIG    _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L
    @   __CONFIG    _CONFIG2H, _WDTEN_OFF_2H
    @   __CONFIG    _CONFIG3H, _PBADEN_OFF_3H & _HFOFST_OFF_3H & _MCLRE_EXTMCLR_3H
    @   __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    DEFINE OSC 20
    ANSELA = 0
    ANSELB = 0
    ANSELC = 0
    ANSELD = 0
    ANSELE = 0
    TRISA = %00000000
    TRISB = %00000000
    TRISC = %00000000
    TRISD = %10000000
    TRISE = %00000000
    
    CODE_ONE        VAR PortB.1
    CODE_TWO        VAR PortB.2
    CODE_THREE      VAR PortB.3
    CODE_FOUR       VAR PortB.4
    CODE_FIVE       VAR PortB.5
    
      IF CODE_ONE = 13 and CODE_TWO = 13 and CODE_THREE = 16 and CODE_FOUR = 21 and CODE_FIVE = 32 THEN
        'Correct code
      ENDIF
    END
    I know, Port be is defined as output and used as input (noticed after test), but that has no impact on test significance.

    More than twice as big using single IF, nested IF is definitely more efficient. I remember reading about that somewhere recently too, can't remember where.

    Robert
    Last edited by Demon; - 24th September 2012 at 15:44.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Using "If then" and "and" together

    you could also turn it on its head,

    IF CODE_ONE <> 13 THEN EXIT_TO_ERROR

    AND SO ON,

    Then after the last comparison you have a valid password.

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


    Did you find this post helpful? Yes | No

    Default Re: Using "If then" and "and" together

    Hi,
    I tried four examples here, compiled for a 16F628.
    First test:
    Code:
      IF CODE_ONE = 13 and CODE_TWO = 13 and CODE_THREE = 16 and CODE_FOUR = 21 and CODE_FIVE = 32 THEN
       'Correct code
      ENDIF
    Result: 75 WORDS

    Second test:
    Code:
    If CODE_ONE = 13 THEN
     IF CODE_TWO = 13 then
       if CODE_THREE = 16 then
         if CODE_FOUR = 21 then
           if CODE_FIVE = 32 THEN
           'correct code
           Endif
         endif
       endif
     endif
    endif
    Result: 26 WORDS

    Third test:
    Code:
    IF CODE_ONE <> 13 THEN EXIT_TO_ERROR
    IF CODE_TWO <> 13 THEN EXIT_TO_ERROR
    IF CODE_THREE <> 13 THEN EXIT_TO_ERROR
    IF CODE_FOUR <> 13 THEN EXIT_TO_ERROR
    IF CODE_FIVE <> 13 THEN EXIT_TO_ERROR
    'correct code
    
    EXIT_TO_ERROR:
    RESULT: 26 WORDS (same as test 2)

    Forth test:
    This one uses an array to hold the entered code and assumes that the correct password sequence is stored in EEPROM, starting at location 0
    Code:
    Correct = 1
    For i = 0 to 4
     Read i, Password
      if Code[i] <> Password THEN
        Correct = 0
      ENDIF
    NEXT
    If Correct THEN
    'correct code
    endif
    RESULT: 41 WORDS

    So between these four method 2 or 3 produces the smallest code - by far. Personally I think Aerostars suggestion is the easiest to read but that might just be me...

    /Henrik.

  7. #7
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Using "If then" and "and" together

    Henrik , can you advise why the big dif in words count between the samples ?

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