simple IF statement

kemp

Usenet Archive
Joined
Dec 18, 2003
Messages
142
Reaction score
0
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")
)
)
)
)
 
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.
 
(if op1
(progn
;do_what_ever
(if what_ever
(progn
;do_this_or_that
)
)
)
)
 
Ya,
And you forgot your "=" signs:

(if (=cb2 'T)
(if (=cb3 'T)
(alert "GFCI - Under Counter - WP")
(alert "GFCI - Under Counter")
)
(if (=cb3 'T)
 
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
 
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
 
Thanks Ken, that is a bit cleaner and simpler.

kemp
 
:

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
 

Latest threads

Back
Top