Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

If statements

8 REPLIES 8
Reply
Message 1 of 9
1CAlexander1
396 Views, 8 Replies

If statements

How do I get multiple if statements to work within the same LISP routine? Or is this not possible? If not, what is a good work around?
8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: 1CAlexander1

Silomethis,

COND

W. Kirk Crawford
Tularosa, New Mexico

"silomethis" wrote in message news:5699223@discussion.autodesk.com...
How do I get multiple if statements to work within the same LISP routine?
Or is this not possible? If not, what is a good work around?
Message 3 of 9
1CAlexander1
in reply to: 1CAlexander1

I'm not sure if I was using COND correctly the time I attempted to use it. I used it something like this.

(cond
(if
true
false
);if
(if
true
false
);if
);cond

I'm sure this was incorrect usage though.
Message 4 of 9
EC-CAD
in reply to: 1CAlexander1

Here is an example.
Usage: COND

(cond
( (= X 1)
(setq z 0)
(.. do more stuff)
)
( (= X 2)
(setq z 1)
(.. do more stuff)
)
T

); end cond

Bob
Message 5 of 9
Tom Smith
in reply to: 1CAlexander1

See http://afralisp.net/lisp/conif.htm

You can use all the ifs you want, but IMO lots of ifs are usually a result of poor program design. Cond will usually let you do same thing more clearly and with less code.
Message 6 of 9
1CAlexander1
in reply to: 1CAlexander1

Thanks all for the help and the great link. Got it working. 🙂
Message 7 of 9
Anonymous
in reply to: 1CAlexander1

silomethis said the following on 8/23/2007 2:01 PM:
> How do I get multiple if statements to work within the same LISP
> routine? Or is this not possible? If not, what is a good work
> around?

This is not to say that (cond) is not a better method, but if you really
want to use multiple (if) statements, here is an example:

(if (= 1 1)
(if (= 2 2)
(if (= 3 3)
(if (= 4 5)
(if (= 6 6)
(princ "all true")
)
)
)
)
)


Which fails of course on the 4th (if) statement.


--
R.K. McSwain
http://rkmcswain.blogspot.com
Message 8 of 9
Tom Smith
in reply to: 1CAlexander1

A cleaner way to do the same sequential test would be:

(and
(= 1 1)
(= 2 2)
(= 3 3)
(= 4 5)
(= 6 6)
(princ "all true")
)
Message 9 of 9
Anonymous
in reply to: 1CAlexander1

Tom Smith said the following on 8/25/2007 1:05 PM:
> A cleaner way to do the same sequential test would be:
>
> (and
> (= 1 1)
> (= 2 2)
> (= 3 3)
> (= 4 5)
> (= 6 6)
> (princ "all true")
> )

True, but since the OP asked about "multiple if statements", I was just
showing that it *can* be done.

Just for grins, I ran both versions, using the same timer for 1,000,000
iterations each.
The (if) version averaged about 2.55 seconds
The (and) version averaged about 2.45 seconds.


--
R.K. McSwain
http://rkmcswain.blogspot.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost