News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Compound "IF" evaluations.

Started by dr-zin, Jun 24, 2021, 08:09 PM

Previous topic - Next topic

dr-zin

Greetings.  I am trying to set up a common endless polling process at the beginning of my Main: program body to establish when to begin performing the program steps based on the value of certain variables.  It's done all the time for single variables.  However, I want to continue looping until one of two variables go low.  Does the IF...THEN...ELSEIF...(etc.) command allow for multiple Boolean evaluation of several variables using AND or OR conjunctions?  I see no reference for it.

What I want:
("Up" and "Dn" are i/o pins tied to pullup resistors).
Main:
  If Up = 1 AND Dn = 1 then Main
{Enters program body here when either is false/0}

My workaround is:
Main:
  If Up = 0 then Move
  If Dn = 0 then Move
Goto Main
Move:
{Enters program body here when either is false/0}

I suppose an additional option would be to use an ElseIf structure to handle the second comparison, but my main interest is to check on whether an IF statement can be used to do compound comparisons as part of its decision process.  Any definitive answer to this?  Any comments would be helpful.  Thanks.

trastikata

#1
No problem with compound evaluations, however you can't use brackets to combine different logic expressions.

P.s. Edit: For more information check the manual, paragraph "Boolean Logic Operators"

RGV250

Hi,
Is this what you want?
If up = 1 Then
 If down = 1 Then
    GoTo Main
 EndIf
 EndIf

Bob

RGV250

Hi,
Just re read you post so ignore my last post, try this.

 If up = 1 Then If down = 1 Then Main

Bob

trastikata

#4
I think better solution would be with a While loop:

Main:
    While Up = 1 And Dn = 1 : Wend
Move:

Another possibility with If statements

Main:
    If Up = 1 And Dn = 1 Then Main
Move:

Yet another one

Main:
     Do : Loop Until Up = 0 Or Dn = 0
Move:

All mentioned options generate virtually the same Assembler code.

But I guess from the variable naming that those are buttons. Then you will need proper initialization of the pin variables and some de-bouncing after the button is pressed.

dr-zin

Hi again; thanks for the speedy replies.  Yes, there seems to be a multitude of workaround approaches to do this (I really like the While...Wend structure for this).  However, I was able to implement my desired "If....And....Then Main" answer and get it to compile (as in the post by trastikata above).  I guess my initial error was caused because I used (parenthesis) when I tried it the first time.  That's why I tried looking through the manual documentation of IF...Then...Elseif...Else for an example of this use.  I suppose you can't include every possible variant in the manual without turning it into an encyclopedia.  Thanks again for your help.  Cheers-

top204

Within the manual's "Boolean Logic Operators" section, it has this text:

Parenthesis (or rather the lack of it!).
Every compiler has its quirky rules, and the Positron8 compiler is no exception. One of its quirks means that parenthesis is not supported in a Boolean condition, or indeed with any of the If-Then-Else-EndIf, While-Wend, and Repeat-Until conditions. Parenthesis in an expression within a condition is allowed however. So, for example, the expression:-

If (Var1 + 3) = 10 Then do something. Is allowed.

but:-

If((Var1 + 3) = 10) Then do something. Is not allowed.

The Boolean operators do have a precedence within a condition. The and operator has the highest priority, then the or. This means that a condition such as:-

If Var1 = 2 and Var2 = 3 or Var3 = 4 Then do something

Will compare Var1 and Var2 to see if the and condition is true. It will then see if the or condition is true, based on the result of the and condition.

dr-zin

Got'cha.  My initial search was in the If/Then/Elseif/Else section, and did not extend to the operator area.  It bit me again!  Thanks.

dr-zin

I keep waiting for my "photographic memory" to kick in so I will only have to read the manual through once to completely assimilate all the information, but it doesn't look likely that that's going to turn on any time soon.  Alas...  :(

Pepe

#9
if I need

if a = 0 and (b = 1 or b = 2) then ...

as parentheses are not allowed


if b = 1 or b = 2 then c = 1 else c = 0
if a = 0 and c = 1 then ...

why parentheses give error

dr-zin

One of the mysteries of life.  Everything was rules, and we don't always get to make them...
Just happy to be able to generally do what I want when it comes to decisions and branching.