Jump to content

Using (if) with no condition; what is the use?


Recommended Posts

Posted

hi

 

in the gardenpath toturial , i never understand why the use of (if) ?

 

 

(defun gp:getPointInput	(/ StartPt EndPt HalfWidth)
 (if (setq StartPt (getpoint "\nStart point of path: "))
   (if	(setq EndPt (getpoint StartPt "\nEndpoint of path: "))
     (if (setq HalfWidth (getdist EndPt "\nHalf width of path: "))
;; if you've made it this far, build the association list
;; as documented above.  This will be the return value
;; from the function.
(list
  (cons 10 StartPt)
  (cons 11 EndPt)
  (cons 40 (* HalfWidth 2.0))
  (cons 50 (angle StartPt EndPt))
  (cons 41 (distance StartPt EndPt))
)
     )
   )
 )
)

 

Thanks

Shay

Posted

Maybe the AND function wasn't born in the time of writing that function . :shock:

Posted
Maybe the AND function wasn't born in the time of writing that function . :shock:

only lisper can enjoy your joke...but what about bigginers? :)

Posted (edited)

The

 (if (setq StartPt (getpoint "\nStart point of path: "))
   (if (setq EndPt (getpoint StartPt "\nEndpoint of path: "))
     (if (setq HalfWidth (getdist EndPt "\nHalf width of path: "))

is not the same as

(and (setq StartPt (getpoint "\nStart point of path: "))
    (setq EndPt (getpoint StartPt "\nEndpoint of path: "))
    (setq HalfWidth (getdist EndPt "\nHalf width of path: "))
)

because if it fails a point and providing as an argumente to the getpoint/getdist function will give an error...

So you should test the existence of a valid value before providing it to the next getpoint/getdist function

 

HTH

Henrique

Edited by hmsilva
Typo
Posted
only lisper can enjoy your joke...but what about bigginers? :)

You can not consider that if statement that been written by Autodesk in the Help document is wrong and progamatically it's correct BUT to avoid the repetition of IF function , you can add AND function for the first three variables which it would look more logic and better.

Posted

Perhaps the following comments will help to explain the various arguments:

 

(defun gp:getPointInput (/ StartPt EndPt HalfWidth)

   ;; 1st IF statement:
   (if (setq StartPt (getpoint "\nStart point of path: "))
   
       ;; 2nd IF statement: this is the 'then' argument for the 1st IF statement
       (if (setq EndPt (getpoint StartPt "\nEndpoint of path: "))
           
           ;; 3rd IF statement: this is the 'then' argument for the 2nd IF statement
           (if (setq HalfWidth (getdist EndPt "\nHalf width of path: "))

               ;; 'then' argument for 3rd IF statement
               (list
                   (cons 10 StartPt)
                   (cons 11 EndPt)
                   (cons 40 (* HalfWidth 2.0))
                   (cons 50 (angle StartPt EndPt))
                   (cons 41 (distance StartPt EndPt))
               )
               ;; 'else' argument for 3rd IF statement would go here
           )
           ;; 'else' argument for 2nd IF statement would go here
       )
       ;; 'else' argument for 1st IF statement would go here
   )
)

As noted by others, this could be written using a single IF statement with an AND expression, e.g.:

 

(defun gp:getPointInput (/ StartPt EndPt HalfWidth)

   ;; 1st IF statement:
   (if
       (and
           (setq StartPt (getpoint "\nStart point of path: "))
           (setq EndPt (getpoint StartPt "\nEndpoint of path: "))
           (setq HalfWidth (getdist EndPt "\nHalf width of path: "))
       )
       ;; 'then' argument for 1st IF statement:
       (list
           (cons 10 StartPt)
           (cons 11 EndPt)
           (cons 40 (* HalfWidth 2.0))
           (cons 50 (angle StartPt EndPt))
           (cons 41 (distance StartPt EndPt))
       )
       ;; 'else' argument for 1st IF statement would go here
   )
)

However, note that these two examples are not equivalent due to how the 'else' arguments would be evaluated:

 

(defun foo ( )
   (if <test-expression-1>
       (if <test-expression-2>
           (if <test-expression-3>
               <then-expression-3>
               <else-expression-3>
           )
           <else-expression-2>
       )
       <else-expression-1>
   )
)

(defun bar ( )
   (if
       (and
           <test-expression-1>
           <test-expression-2>
           <test-expression-3>
       )
       <then-expression-1>
       <else-expression-1>
   )
)

 

In the first function, separate 'else' expressions can be evaluated for each test expression that is not validated.

 

Whereas, in the second function it is an 'all or nothing' approach - if all test expressions are validated, the 'then' expression is evaluated, otherwise the single 'else' expression is evaluated.

Posted
in the gardenpath toturial , i never understand why the use of (if) ?

Samifox, to address your question, those IF's (which were not without condition as you noted in thread's title - the input on GETPOINT function is what is tested there) were there to ensure that user had provided correctly an input before proceeding to processing.

Posted
because if it fails a point and providing as an argumente to the getpoint/getdist function will give an error...

Henrique. please note that, unless in other programming languages, the statements from an AND construction in AutoLISP will be evaluated until one of them fails; so you can have subsequent tests that can raise an error based on result of a previous one since those may not be reached if said one failed.

(and nil
    (/ 1 0))

Posted

Agree.

Thank you Mircea

 

Henrique

Posted

Okay, interesting thread.

Thanks Lee for another informative and excellent description.

You were right, the additional comments made all the difference in understanding the parenthesis matching within nested conditionals.

An important distinction to take note of, thanks.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...