PDA

View Full Version : Using "If then" and "and" together



Christopher4187
- 24th September 2012, 14:05
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:


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?

Christopher4187
- 24th September 2012, 14:16
Ok, so apparently I'm not intelligent enough to realize the difference between a 0 (zero) and O (the letter O):smile:

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

HenrikOlsson
- 24th September 2012, 14:28
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.

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.

Demon
- 24th September 2012, 15:40
Using PBP 2.60C, MCSP v2.2.1.1, MPASM v5.46, PIC 18F46K22.

62 bytes:


@ __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:


@ __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
:)

aerostar
- 25th September 2012, 17:08
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.

HenrikOlsson
- 25th September 2012, 18:16
Hi,
I tried four examples here, compiled for a 16F628.
First test:

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:

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:

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

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.

longpole001
- 26th September 2012, 12:18
Henrik , can you advise why the big dif in words count between the samples ?