Chapter 1 program process control box AutoLISP file (1) program Process Control

Source: Internet
Author: User

The process of The AUTOLISP program is controlled by flow control functions.

1. Branch Structure

(1) (cond (test expression 1 Result Expression 1) [(test expression 2 Result Expression 2)]...)

[Example]

1 (setq n( cond((<= i 1) 1)2              ((<= i 1) 1)3              ((<= i 2) 4)4              ((<= i 3) 10)5              (t 100)6         )7 )

Note: This function is similar to the switch statement in C language. The last test expression "t (or t)" is equivalent to "default" and refers to other cases. This test expression is also missing. Different from the C language switch, if a test expression is true, the value of the corresponding result expression is returned. If I is equal to 0, n is 1, and the operation ends.

(2) (if test condition expression 1 [expression 2])

If the expression of the test condition is true, expression 1 is executed. Otherwise, expression 2 is executed. for example: (if (> A 1) (setq B 2) meaning: if A is greater than 1, B is equal to 2. Otherwise, no calculation is performed and the evaluation ends. (If (> A 1) (setq B 2) (setq B 2) indicates that, if A is greater than 1, B is equal to 2; otherwise, B is equal to 3, and the end of the evaluate.

When a conditional expression is set, the progn function is required to execute multiple expressions.

(3) (progn expression ......)

(if(> a 1)    (progn (setq b 2)           (print (+ b a))           )    (progn (setq b 4)           (print b)           ))

Explanation: if the condition is true, B is equal to 2, and the sum of A and B is printed, and the sum of A and B is returned. Otherwise, B equals 4, print B, and return the value of B.

2. Loop Structure

(1) (Repeat integer N expression ......)

Repeat n times. Evaluate multiple expressions and return the calculation result of the last expression.

1 (setq a 1 b 100)2 (repeat 103   (setq a(+ 1 a))4   (setq b(+ 10 b))5 )

(2) (while test expression ......)

 (setq i 1 a 10) (while ( <=  i 10)   (setq a(+ a 10))  (setq i(1+ i)) )

I don't know why there are always errors.

Solution: Because <= needs to be connected and written

Chapter 1 program process control box AutoLISP file (1) program Process Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.