15 June 2012

If and if else condition and nested if or cond in AutoLISP


Hi Friends,in this article i will explain about the if and if else condition and  nested if or cond in AutoLISP.

IF:
This is used to execute some code only if a specified condition is true.


Syntax : 
(if (condition)
  (progn
  code to be executed if condition is true
  )
)

Ex:
(setq i 3)
(if (= i 3)
(progn
(alert (strcat “hi......” (rtos i) )
)

If Else:
This is used to execute some code if the condition is true and another code if the condition is false.

  Syntax :  
(if (condition)
  (progn
  code to be executed if condition is true
  )
(progn
code to be executed if condition is not true
)
)

Ex:
 (setq i 3)
(if (= i 3)
 (princ 3)
(princ 4)
)  


Output: 3

cond – This is nothing but nest-if in C.This function test conditions until one of them is true.  At that point it exits the function.
    Syntax :
(cond
(if (condition)
  (progn
  code to be executed if condition is true
  )
(progn
code to be executed if condition is not true
)
)
(if (condition)
  (progn
  code to be executed if condition is true
  )
(progn
code to be executed if condition is not true
)
)
)



Ex:
 (setq i 30)
(cond
(if (< i 10)
 (princ i)
)  
(if (< i 20)
(princ i)
)
(if (< i 30)
(princ i)
)
)





No comments:

Post a Comment