How to Test Multipl

How to Test Multipl
Site menu


Login form


Search


Site friends
  • Create your own site


  • Statistics

    Total online: 1
    Guests: 1
    Users: 0


    Welcome, Guest ยท RSS 16 September 2024

    How to Test Multiple Conditions



    There is also another function that acts very much like the if function called Cond. Arguments to cond consists of one or more expressions that have as their first element a test expression followed by an object that is to be evaluated if the test returns T. Cond evaluates each test until it comes to one that returns T. It then evaluates the expression or atom associated with that test. If other test expressions follow, they are ignored.



    Using the Cond function

    The syntax for cond is:

    (cond
    ((test expression)[expression/atom][expression/atom]...)
    ((test expression)[expression/atom][expression/atom]...)
    ((test expression)[expression/atom][expression/atom]...)
    ((test expression)[expression/atom][expression/atom]...)
    )



    Making Decisions with AutoLISP

    the cond function used in place of the if function. Cond's syntax also allows for more than one expression for each test expression. This means that you don't
    have to use the Progn function with cond if you want several expressions evaluated as a result of a test.


    (defun C:MAINBOX (/ choose)
    (setq choose (getstring "\nDo you want a 3D box <Y=yes/Return=no>? "))
    (cond
    ( (or (equal choose "y") (equal choose "Y")) (3dbox))
    ( (or (/= choose "y") (/= choose "Y")) (box1))
    )
    )

    Using cond in place of if


    another program called Chaos that uses the Cond function. Chaos is an AutoLISP version of a mathematical game used to demonstrate the creation of fractals
    through an iterated function. The game works by following the steps shown in Figure 5.8.


    ;function to find the midpoint between two points
    (defun mid (a b)
    (list (/ (+ (car a)(car b)) 2)
    (/ (+ (cadr a)(cadr b)) 2)
    ))
    ;function to generate random number
    (defun rand (pt / rns rleng lastrn)
    (setq rns (rtos (* (car pt)(cadr pt)(getvar "tdusrtimer"))))
    (setq rnleng (strlen rns))
    (setq lastrn (substr rns rnleng 1))
    (setq rn (* 0.6 (atof lastrn)))
    file:///E|/Dhimant/Support/Programming/Dhimant's%20Autolisp/Chapter 5.htm (12 of 25)27/08/2008 2:08:16 PM
    Chapter 5: Making Decisions with AutoLISP
    (fix rn)
    )


    ;The Chaos game
    (defun C:CHAOS (/ pta ptb ptc rn count lastpt randn key)
    (setq pta '( 2.0000 1 )) ;define point a
    (setq ptb '( 7.1962 10)) ;define point b
    (setq ptc '(12.3923 1 )) ;define point c
    (setq lastpt (getpoint "Pick a start point:")) ;pick a point to start
    (while (/= key 3) ;while pick button not pushed
    (setq randn (rand lastpt)) ;get random number
    (cond ;find midpoint to a b or c
    ( (= randn 0)(setq lastpt (mid lastpt pta)) ) ;use corner a if 0
    ( (= randn 1)(setq lastpt (mid lastpt pta)) ) ;use corner a if 1
    ( (= randn 2)(setq lastpt (mid lastpt ptb)) ) ;use corner b if 2
    ( (= randn 3)(setq lastpt (mid lastpt ptb)) ) ;use corner b if 3
    ( (= randn 4)(setq lastpt (mid lastpt ptc)) ) ;use corner c if 4
    ( (= randn 5)(setq lastpt (mid lastpt ptc)) ) ;use corner c if 5
    );end cond
    (grdraw lastpt lastpt 5) ;draw midpoint
    (setq key (car (grread T))) ;test for pick
    );end while
    );end Chaos

    The Chaos game program


    Cond can be used anywhere you would use if. For example:
    (if (not C:BOX) (load "box") (princ "Box is already loaded. "))
    can be written:

    (cond
    ((not C:BOX) (load "box"))


    ((not (not C:BOX)) (princ "Box is already loaded. "))
    )

    Next -> How to Repeat parts of a Program