Simple IF THEN ELSE problem I can't get my head around
jackbauer
Posts: 4
Hi there
I don't know if this can be done, but if it can, I don't know how to do it. What I'm trying to do is if both inputs are equal to or below a certain value, then I want it to do one thing, and if either of them is above the certain value then it to do something else. Its sort of like:
Sen2 VAR Word
Sen3 VAR Word
Correct CON 30
Sensor:
IF [noparse][[/noparse]Sen2 AND Sen3] <= Correct THEN
GOSUB PULSE
ELSE
GOSUB DRIVE
ENDIF
What I'm wanting it to do is only run the subroutine PULSE only when both variables Sen2 and Sen3 are equal to or below the Correct value. And when either one of them is above the Correct value, I want the DRIVE subroutine run instead.
This was the best way I could think of to explain it, the bit is the square brackets is the part I'm having a problem with because I know it's wrong and won't do what I'm wanting it to do. How can I get it to check both inputs before it decides which part it should run?
I don't know if this can be done, but if it can, I don't know how to do it. What I'm trying to do is if both inputs are equal to or below a certain value, then I want it to do one thing, and if either of them is above the certain value then it to do something else. Its sort of like:
Sen2 VAR Word
Sen3 VAR Word
Correct CON 30
Sensor:
IF [noparse][[/noparse]Sen2 AND Sen3] <= Correct THEN
GOSUB PULSE
ELSE
GOSUB DRIVE
ENDIF
What I'm wanting it to do is only run the subroutine PULSE only when both variables Sen2 and Sen3 are equal to or below the Correct value. And when either one of them is above the Correct value, I want the DRIVE subroutine run instead.
This was the best way I could think of to explain it, the bit is the square brackets is the part I'm having a problem with because I know it's wrong and won't do what I'm wanting it to do. How can I get it to check both inputs before it decides which part it should run?
Comments
IF (Sen2 <= Correct) AND (Sen3 <=Correct) THEN
in the place of your IF statement?
Without seeing the rest of your code, I can not vouch that this will do what you want it to do, but it should be a start...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Truly Understand the Fundamentals and the Path will be so much easier...
kelvin
The two inputs (Sen2, Sen3) will be any 16-bit number, which are unlikely to be the same. What I am wanting is that, going back to the example in the first post;
when both inputs Sen2, Sen3 are below the selected "Correct" value, I want the subroutine "PULSE" to be ran. and when either one or both of the inputs are above the selected "Correct" value, I want the subroutine "DRIVE" to be ran.
As Tom suggested, if I used "IF (Sen2 <= Correct) AND (Sen3 <=Correct) THEN"...(shown below) would that achieve what I'm trying to do?
Sen2 VAR Word
Sen3 VAR Word
Correct CON 300
Sensor:
IF (Sen2 <= Correct) AND (Sen3 <=Correct) THEN
GOSUB PULSE
ELSE
GOSUB DRIVE
ENDIF
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows
There is an underlying principle that seems to missing from this discussion, which should make it a good deal easier to understand. It has to do with the syntax of the language at hand, PBASIC.
There are some other languages which will accept multiple subjects (variables) and/or implied subjects, such as the following:
/code
IF A, B, C = 100 THEN Do_Something <<== multiple subjects
IF (A and B and C) = 100 THEN Do_Something <<== multiple subjects
or
IF A < 100 and > 50 THEN Do_Something_Else <<== implied subject
code/
PBASIC does NOT allow such constructs, as they are wholly unnecesary and only represent programmer "shortcuts". Lacking parenthesis, PBASIC is a strict left to right parser, which may make intermedicate evaluations of compound clauses, and makes no presumptions whatsoever in term of syntax. This is purely by design.
Thus the expression:
IF Sen2 AND Sen3 <= CORRECT ...
will evaluate in four parts, left to right:
1. Fetch and evaluate Sen2
2. Fetch and evaluate Sen3
3. Apply the operator AND to the above evaluations with each other
4. Compare the ANDed result with the variable CORRECT
There are dozens and dozens of other examples, which I'll not get into here. In the end, it's just that simple, left-to-right, with no presumptions, after you break it down.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->