Author Topic: IF THEN  (Read 5837 times)

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
IF THEN
« on: June 14, 2017, 01:33:19 PM »
I am still a little confused on the proper use of a IF or a THEN statement
Below is an example of what I wanted to accomplish but failed
I only want the FOR  and TESTBIT function to work if DM[535] is greater than 0 and I was trying to avoid using a GOTO statement to bypass it.

CLRIO CBP 'First Clear CBP
   IF DM[535] > 0 THEN
FOR N = 0 TO 7
   IF TESTBIT (DM[541], N)
IF TESTBIT(DM[537],N) =0
   SETIO CBP
ENDIF
      ENDIF
NEXT

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:IF THEN
« Reply #1 on: June 14, 2017, 04:17:18 PM »
Lorne,

You should think of the IF statement as IF..ENDIF.  The use of "THEN" is optional and you should just forget about it.

Your program is missing an ENDIF and could not possibly compile.

This is probably what you had in mind:
Code: [Select]
CLRIO CBP 'First Clear CBP

IF DM[535] > 0
   FOR N = 0 TO 7
      IF TESTBIT (DM[541], N)
         IF TESTBIT(DM[537],N) =0
               SETIO CBP
         ENDIF
      ENDIF
   NEXT
ENDIF

I would suggest for "readability" that you indent your code so that the IF lines up with the ENDIF, FOR lines up with NEXT and the statements between then are indented.  This is not required by TBASIC, but it helps to make it easier to see what is going on.

You could simplify your code a bit by using the logical "AND" to test for a "1" in DM[541] and a "0" in DM[537]:

Code: [Select]
CLRIO CBP 'First Clear CBP

IF DM[535] > 0
   FOR N = 0 TO 7
      IF (TESTBIT (DM[541], N) = 1) AND (TESTBIT(DM[537],N) = 0)
           SETIO CBP
      ENDIF
   NEXT
ENDIF

You could probably simplify your code even more by eliminating the FOR/NEXT loop.  You look at all of the bit patterns in parallel by using the bit-wise and operator, "&".  The "~" is the bit-wise NOT operator. I know that this won't make much sense, yet.  But, someday it might:

Code: [Select]
CLRIO CBP 'First Clear CBP (assume that the tests will fail)

IF DM[535] > 0
   ' Test for a "1" in each bit position in DM[541] against a "0" in
   ' the corresponding bit position in DM[537].
   '
   ' TBASIC does most of its operations with 32-bit math and it will
   '  sign-extend the contents of the 16-bit DM[] registers to 32-bit variables
   '  and perform the math as if the variables are 32-bits wide.
   '  
   '  But you are only interested in the least significant 8-bits of the DM[]
   '  registers. The fix is to perform a bit-wise and operation and preserve
   '  the least significant 8-bits and force to 0 all of the rest of the bits that
   '  either you don't care about or never existed (sign-extension).
   '
   IF ((DM[541] & (~DM[537])) & &hff)
       SETIO CBP
   ENDIF
ENDIF

Best regards

Gary D*ckinson
« Last Edit: June 14, 2017, 04:22:47 PM by garysdickinson »

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re:IF THEN
« Reply #2 on: June 15, 2017, 08:48:03 AM »
Gary
Thanks again for your expert help. I will try each of your suggestions.