Shop OBEX P1 Docs P2 Docs Learn Events
Simple IF THEN ELSE problem I can't get my head around — Parallax Forums

Simple IF THEN ELSE problem I can't get my head around

jackbauerjackbauer Posts: 4
edited 2006-04-12 09:39 in BASIC Stamp
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?

Comments

  • Tom WalkerTom Walker Posts: 509
    edited 2006-04-11 17:28
    Using only the code you presented, how about

    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 jameskelvin james Posts: 531
    edited 2006-04-12 06:19
    The code --- IF Sen2 AND Sen3 <= Correct THEN ( notice the brackets are removed ) tokenizes properly. Make sure you are running pbasic 2.5. The help section ( if -then - else ) explains the logic operations in pretty good detail.

    kelvin
  • jackbauerjackbauer Posts: 4
    edited 2006-04-12 07:00
    I am using pbasic 2.5. I know the brackets shouldn't be there, I just put them in to highlight the problem part as I think the AND function, in the way I have used it, will not check both input values against the desired specified correct value. The code i gave was just off the top of my head to help explain the problem.

    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
  • SSteveSSteve Posts: 808
    edited 2006-04-12 07:24
    The statement "IF Sen2 AND Sen3 <= Correct THEN" will AND together the values of Sen2 and Sen3, which means if they are both non-zero, the result is an unspecified non-zero value. The statement then evaluates to "IF unspecified value <= Correct THEN" which is obviously not what you want.
    jackbauer said...
    As Tom suggested, if I used "IF (Sen2 <= Correct) AND (Sen3 <=Correct) THEN"...(shown below) would that achieve what I'm trying to do?
    Yes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-04-12 09:39
    Folks -

    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 -->
Sign In or Register to comment.