The AutoCADET's Guide to Visual LISP: Optimize and Customize Your AutoCAD Design Environment

Almost all of the example functions and utilities you've looked at so far have included conditionals and loops, critical components in programming computers. A conditional is a test, the result of which causes your program to take a particular path. A loop is a repeating group of program code that works with lists of data or that collapses on an answer through an iterative process such as those commonly used in engineering computations involving linear algebra.
In this chapter, you explore the concepts behind conditionals and then look at the many options available for testing data in Visual LISP. The two basic conditional structures, IF and COND, are formally introduced, and you learn how to choose the right one for your programs. The chapter then describes a related structure, the loop.
The difference between a script and a program is logic. A script plays back a series of commands in order. It will not deviate from that sequence except to stop working when something is wrong. You may have developed scripts in AutoCAD for menu macros or command streams (a sequence of commands). You can save a command stream in a text file with an SCR extension. These SCR files are called script files and can be played back using the SCRIPT command. Although scripts can be useful for combining AutoCAD commands that you use frequently in sequence, that is all they can do. If you must apply some sort of logic to when...