benmc Posted November 23, 2015 Share Posted November 23, 2015 I have what will probably be a basic question for a lot of you. It revolves around LISP programming, IF statements and looping. See the code below just as a simple example. Basically what I am trying to get is..using the IF statement against two real number variables.. if "A" is larger than "B", I want the user to be alerted to this fact, and then redirected to the top of the program to input the variables again. And if "A" is not larger, then do nothing. Any help would be greatly appreciated. Thanks (defun c:testif () (setq a (getreal "\nEnter a Number : ") b (getreal "\nEnter Second Number : ") );setq (if (> a b) (prompt "\nFirst number is larger") );if (princ) );defun (princ) Quote Link to comment Share on other sites More sharing options...
satishrajdev Posted November 23, 2015 Share Posted November 23, 2015 (defun c:testif (/ a b) (while (progn (setq a (getreal "\nEnter a Number : ") b (getreal "\nEnter Second Number : ") ) ;setq (cond ((or (not a) (not b)) (prompt "\nPlease specify the values") T ) ((> a b) (prompt "\nFirst number is larger") T) (T nil) );cond );progn );while (princ) ) Quote Link to comment Share on other sites More sharing options...
benmc Posted November 23, 2015 Author Share Posted November 23, 2015 Thanks so much Quote Link to comment Share on other sites More sharing options...
BIGAL Posted November 23, 2015 Share Posted November 23, 2015 Make the input a seperate defun within the body of your master code the other way is to swap a & b (defun testif () (alert "Enter Val1 less than val 2") (setq a (getreal "\nEnter a Number : ") b (getreal "\nEnter Second Number : ") );setq (if (> a b) (progn (Alert "First number is larger\n please try again") (testif) ) );if (princ) );defun (princ) master code ..... (testif) ; returns a < b Quote Link to comment Share on other sites More sharing options...
satishrajdev Posted November 23, 2015 Share Posted November 23, 2015 Thanks so much You're welcome Quote Link to comment Share on other sites More sharing options...
benmc Posted November 23, 2015 Author Share Posted November 23, 2015 Make the input a seperate defun within the body of your master code the other way is to swap a & b (defun testif () (alert "Enter Val1 less than val 2") (setq a (getreal "\nEnter a Number : ") b (getreal "\nEnter Second Number : ") );setq (if (> a b) (progn (Alert "First number is larger\n please try again") (testif) ) );if (princ) );defun (princ) master code ..... (testif) ; returns a < b Thanks bigal! What if I were to have more than just the two variables. For instance, maybe i had 6 variables (a,b,c,d,e,f) and wanted to compare "a against b", then "c against d", and "e against f". Would I simply write the same code you have suggested above, 3 times consecutively? Just replacing the variable names. Quote Link to comment Share on other sites More sharing options...
BIGAL Posted November 24, 2015 Share Posted November 24, 2015 I think using the swap is maybe easier but yes (testif) (setq vala a (setq valb b) (testif) (setq valc a vald b) (testif) (setq vale a valf b) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted November 24, 2015 Share Posted November 24, 2015 Assuming I have understood what you are looking to achieve, here is another method, allowing for any number of values: (defun c:test ( / l x ) (if (setq x (getreal "\nEnter a number: ")) (progn (setq l (list x)) (while (setq x (getreal (strcat "\nEnter a number greater than " (rtos (car l)) " <done>: "))) (if (<= x (car l)) (princ (strcat "\nNumber must be greater than " (rtos (car l)))) (setq l (cons x l)) ) ) ) ) (reverse l) ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.