simple IF statement

Discussion in 'AutoCAD' started by kemp, Jan 22, 2004.

  1. kemp

    kemp Guest

    I'm having trouble debugging a nested if statement. Can anyone tell what is
    wrong. I'm sure it's a logic error on my part. I've labelled the area that
    won't work. Thanks for any help!

    (if (= op1 'T)
    (if (= cb1 'T)
    (alert "Standard Outlet - 1/2 Hot")
    (alert "Standard Outlet")
    )
    (if (= op2 'T)
    ;;-------------------- begin of failing statement
    (if (cb2 'T)
    (if (cb3 'T)
    (alert "GFCI - Under Counter - WP")
    (alert "GFCI - Under Counter")
    )
    (if (cb3 'T)
    (alert "GFCI - WP")
    (alert "GFCI")
    )
    )
    ;;-------------------- end of failing statement
    (if (= op3 'T)
    (if (= cb4 'T)

    (alert "AFCI - 1/2 Hot")
    (alert "AFCI")
    )
    )
    )
    )
     
    kemp, Jan 22, 2004
    #1
  2. kemp

    Tom Smith Guest

    I think you've got 3 branches under your opening if instead of the 2
    allowed. Try commenting out ifs , from the innermost outward, to check.

    IMHO a cond structure is usually simpler to understand and debug than
    heavily nested ifs, when multiple conditions are involved.
     
    Tom Smith, Jan 22, 2004
    #2
  3. kemp

    Rudy Tovar Guest

    (if op1
    (progn
    ;do_what_ever
    (if what_ever
    (progn
    ;do_this_or_that
    )
    )
    )
    )
    --

    AUTODESK
    Authorized Developer
    www.Cadentity.com
    MASi
     
    Rudy Tovar, Jan 22, 2004
    #3
  4. kemp

    BillZ Guest

    Ya,
    And you forgot your "=" signs:

    (if (=cb2 'T)
    (if (=cb3 'T)
    (alert "GFCI - Under Counter - WP")
    (alert "GFCI - Under Counter")
    )
    (if (=cb3 'T)
     
    BillZ, Jan 22, 2004
    #4
  5. kemp

    kemp Guest

    Haha, that was it, the = was missing. Thanks BillZ

    I am still a novice, and a cond structure is probably the better way to do
    this, but until Tom Smith mentioned that I didn't know to look for that.

    So thanks everyone for your help!

    kemp
     
    kemp, Jan 22, 2004
    #5
  6. kemp

    ECCAD Guest

    Could do with (progn..
    ;;
    (if (= cb2 'T)
    (progn
    (if (= cb3 'T)
    (alert "GFCI - Under Counter - WP")
    (alert "GFCI - Under Counter")
    ); eif
    (if (/= cb3 'T)
    (alert "GFCI - WP")
    (alert "GFCI")
    ); eif
    ); epr
    ); eif

    Bob Shaw
     
    ECCAD, Jan 22, 2004
    #6
  7. or simply

    (if cb2
    (do this)
    (else do this)
    )


    --
    Ken Alexander
    Acad2004
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Jan 22, 2004
    #7
  8. kemp

    kemp Guest

    Thanks Ken, that is a bit cleaner and simpler.

    kemp
     
    kemp, Jan 26, 2004
    #8
  9. kemp

    Scot-65 Guest

    :

    Kemp,

    Nobody above used the AND Boolean...


    (if (and (= op1 'T) (= cb1 'T))
    (progn
    (alert "Standard Outlet - 1/2 Hot")
    (princ "\n1/2 Hot")
    );progn
    (progn
    (alert "Standard Outlet")
    (princ "\nStandard")
    );progn
    );if

    When more than two actions are needed in a IF,
    you will need to use PROGN...


    You may also try to do this in a COND.

    (cond
    ( (and (= a 1)(= b 2)) ([evaluate]) )
    ( (= c 3) ([evaluate]) )
    ( (or (= d 4)(/= e 5)) ([evaluate]) )
    (a ([evaluate]) )
    );cond


    Scot-65
     
    Scot-65, Jan 30, 2004
    #9
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.