Author Topic: IF statement error  (Read 1214 times)

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 465
IF statement error
« on: May 26, 2013, 02:44:08 AM »
Could anyone give me a help for the code below?
Code: [Select]
(defun c:test ()
(setq A (getreal "\nEnter a number: "))
(setq B (getreal (strcat "\nEnter a number "
                         (progn (if (<= A 100) "(<= 50): ")
(if (> A 100) "(> 50): ")))))
)
If A = 150, it will give the user "Enter a number (> 50): "
If A = 10, it will give the user "; error: bad argument type: stringp nil"

What is wrong it gives an error message when A < 100?
Thanks for your help.

Tharwat

  • Swamp Rat
  • Posts: 712
  • Hypersensitive
Re: IF statement error
« Reply #1 on: May 26, 2013, 03:23:22 AM »
Maybe this ...

Code - Auto/Visual Lisp: [Select]
  1. (and (setq A (getreal "\nEnter a number : "))
  2.          (setq B (getreal (strcat "\nEnter a number "
  3.                                   (cond ((<= A 100.) "(<= 50): ")
  4.                                         ((> A 100.) "(> 50): ")
  5.                                   )
  6.                           )
  7.                  )
  8.          )
  9.     )
  10.  

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: IF statement error
« Reply #2 on: May 26, 2013, 05:08:57 AM »
There is no need for the second condition: 'A' will either be less than or equal to 100, or greater than 100.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / A B )
  2.     (if
  3.         (and
  4.             (setq A (getreal "\nEnter a number: "))
  5.             (setq B (getreal (strcat "\nEnter a number (" (if (<= A 100) "<=" ">") " 50): ")))
  6.         )
  7.         ...
  8.     )
  9. )

MeasureUp

  • Bull Frog
  • Posts: 465
Re: IF statement error
« Reply #3 on: May 26, 2013, 10:03:54 PM »
Thanks to Tharwat & Lee again.
A "cond" statement definitely works.

And if comparing Lee's code:
Code: [Select]
(if (<= A 100)
; do this
; otherwise do that.
); end if

with the one simplified from my code:
Code: [Select]
(if (<= A 100)
; do this
); end if
(if (> A 100)
; do that
); end if
Both of 2 codes should work logically.
Why my code in post #1 doesn't work properly?
I want to learn from this.
Thanks.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: IF statement error
« Reply #4 on: May 26, 2013, 11:25:49 PM »
Because progn returns the last value
Code - Auto/Visual Lisp: [Select]
  1.   (if (<= A 100) "(<= 50): ") ; <---<<<  this value is never returned
  2.   (if (> A 100) "(> 50): ")   ; <----<<<  when this is nil this is the value returned.
  3. )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.