Simple If/Then Statement Understanding

Simple If/Then Statement Understanding

Anonymous
Not applicable
694 Views
5 Replies
Message 1 of 6

Simple If/Then Statement Understanding

Anonymous
Not applicable

Hey Guys,

 

I'm trying to learn how if then statements work and how to implement them into existing lisps to clean them up. I have tried coding it myself with the help of a couple sites guides but to no avail. I cannot even get it to allow me to pick my first keyword. I have made an oversimplified example of how it should work in my head but it's clearly wrong. Could someone re-code this to do what I want? Code is pretty self explanatory.

 

(defun c:waiter (/ order1)
(setq order1 (initget 1 "Burger Steak" (getkword "What would you like to order?: ")))
  (if(order1 burger) alert "Great Choice!")
  (if(order1 steak) alert "How would you like it cooked?")
  (princ)
  )

 

0 Likes
Accepted solutions (2)
695 Views
5 Replies
  • Lisp
Replies (5)
Message 2 of 6

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

..Could someone re-code this to do what I want? Code is pretty self explanatory.


Here you go

 

(Defun c:waiter ( / order1)
  (initget 1 "Burger Steak" )
  (setq order1 (getkword "What would you like to order?: [Burger/Steak] "))
  (if (eq order1 "Burger")
    	(alert "Great Choice!")
        (alert "How would you like it cooked?")
    )
  (princ)
  )

 

If you need more choices use the cond function

 

(defun c:waiter2 (/ order1)
  (initget 1 "Burger Steak Hotdog Porkchop" )
  (setq order1 (getkword "What would you like to order?: [Burger/Steak/Hotdog/Porkchop] "))
  (cond
    ((eq order1 "Burger")(alert "Great Choice!"))
    ((eq order1 "Steak")(alert "How would you like it cooked?"))
    ((eq order1 "Hotdog")(alert "Do you want mustard on that!"))
    ((eq order1 "Porkchop")(alert "You want potatoes with that?"))
    )
  (princ)
  )

 

Another using assoc function 

 

(defun c:waiter3 (/ order1)
  (initget 1 "Burger Steak Hotdog Porkchop" )
  (setq order1 (getkword "What would you like to order?: [Burger/Steak/Hotdog/Porkchop] "))
  (alert
    (cadr (assoc order1
	    '(("Burger" "Great Choice!")
		( "Steak" "How would you like it cooked?")
		("Hotdog" "Do you want mustard on that!")
		("Porkchop" "You want potatoes with that?"))))
   )    
  (princ)
  )

 

 

 HTH

 

0 Likes
Message 3 of 6

ВeekeeCZ
Consultant
Consultant
Accepted solution

I'll get my usual.

 

(defun c:waiter2 (/ order1)
  (initget "Burger Steak Hotdog Porkchop" )
  (setq order1 (getkword "What would you like to order?: [Burger/Steak/Hotdog/Porkchop] <my usual>  "))
  (cond
    ((eq order1 "Burger")(alert "Great Choice!"))
    ((eq order1 "Steak")(alert "How would you like it cooked?"))
    ((eq order1 "Hotdog")(alert "Do you want mustard on that!"))
    ((eq order1 "Porkchop")(alert "You want potatoes with that?"))
    (T (alert "Your Meatball Baguette well done would be in 5."))
    )
  (princ)
  )

 

0 Likes
Message 4 of 6

Anonymous
Not applicable

Thanks for the support guys much appreciated!

0 Likes
Message 5 of 6

pbejse
Mentor
Mentor

@Anonymous wrote:

Thanks for the support guys much appreciated!


Good for you 🙂

FWIW Here's how pressing enter works with assoc approach similar to @ВeekeeCZ  posts

(defun c:waiter4 (/ order1)
  (initget "Burger Steak Hotdog Porkchop" )
  (setq order1 (getkword
		 "What would you like to order?: [Burger/Steak/Hotdog/Porkchop]<Today's special>"))
  (alert
	  (if
	    (null order1) "The chef would be delighted"      
	    (cadr (assoc order1
		    '(("Burger" "Great Choice!")
			("Steak" "How would you like it cooked?")
			("Hotdog" "Do you want mustard on that!")
			("Porkchop" "You want potatoes with that?"))))
	   )
	  )
  (princ)
  )

HTH

 

0 Likes
Message 6 of 6

Sea-Haven
Mentor
Mentor

My $0.05 instead of initget, this uses a library lisp Multi radio buttons.lsp thanks Pbe like the Alert method.

 

 

(defun c:waiter5 (/ order1)
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (not but)(setq but 1))
(setq order1 (ah:butts but "V"   '("Please Choose  " "Burger" "Steak" "Hotdog" "Porkchop" )))
(alert
	    (cadr (assoc order1
		    '(("Burger" "Great Choice!")
			("Steak" "How would you like it cooked?")
			("Hotdog" "Do you want mustard on that!")
			("Porkchop" "You want potatoes with that?"))))
	   )
  (princ)
)

 

Screenshot405.png

0 Likes