Results 1 to 9 of 9

Thread: Trying to figure out why "While" doesn't stop as advertised in the help documentation

  1. #1
    Member
    Join Date
    2005-05
    Posts
    38
    Login to Give a bone
    0

    Default While loop failure

    Frustrated - impossible to search forum for "While"

    I am trying to figure out why While doesnt stop as advertised in the help documentation.
    When I use it, it always goes through an extra time, instead of:

    "Evaluates a test expression, and if it is not nil, evaluates other expressions; repeats this process until the test expression evaluates to nil "

    For instance, the following:
    Code:
    (defun C:testwhile ()
            (setq test 1)
     	(while (<= test 10)
       	  (print test)
       	  (setq test (1+ test))
     	)
     )
    Outputs this:

    Command: testwhile
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10 11
    Command:

    Any ideas why this is and how to stop this?
    Thanks.

    [ Moderator Action = ON ] What are [ CODE ] tags... [ Moderator Action = OFF ]
    Last edited by Takuwind; 2006-08-04 at 06:56 PM. Reason: [CODE] tags added.

  2. #2
    100 Club intergrupocr's Avatar
    Join Date
    2006-02
    Posts
    116
    Login to Give a bone
    0

    Default Re: Trying to figure out why "While" doesn't stop as advertised in the help documentation

    If you don't use "(princ)" at the end of your programs any autolisp function will return the last value evaluated. Another important thing is use only "<" instead "<=" because the last loop evaluates "test = 10" = True and then do the next statements, in the last will do (setq test (1+ test)) then test will be 11 and the loop will end. if you use only "<" test will be 10 at the end of the loop.
    Last edited by intergrupocr; 2006-08-03 at 11:07 PM.

  3. #3
    Member
    Join Date
    2005-05
    Posts
    38
    Login to Give a bone
    0

    Default Re: Trying to figure out why "While" doesn't stop as advertised in the help documentation

    Thanks... having one of those days...

    That also helped me figure out the nil test for while, which I was having problems with too.
    Thanks

  4. #4
    Member
    Join Date
    2005-05
    Posts
    38
    Login to Give a bone
    0

    Default Re: Trying to figure out why "While" doesn't stop as advertised in the help documentation

    Ok, so I was wrong, I dont understand the nil test.
    This code:

    Code:
    (defun C:UCSCOPY ()
      (setq table (tblnext "ucs" t))
      (print table)
      (prompt "\nFirst")
    
      (while table
    	(setq table (tblnext "ucs"))
    	(print table)
    	(prompt "\nAgain")
      )
      (prompt "\nEnd")
      (princ)
    )
    (prompts are for tracking purposes)
    returns this:
    Command: ucscopy
    ((0 . "UCS") (2 . "Test 1") (70 . 0) (10 9.94824 6.66425 0.0) (11 0.901829
    0.432093 0.0) (12 -0.432093 0.901829 0.0) (79 . 0) (146 . 0.0))
    First
    ((0 . "UCS") (2 . "Test 2") (70 . 0) (10 30.8981 -5.72374 0.0) (11 0.809488
    -0.587137 0.0) (12 0.587137 0.809488 0.0) (79 . 0) (146 . 0.0))
    Again
    ((0 . "UCS") (2 . "Test 3") (70 . 0) (10 54.7626 34.9228 0.0) (11 0.223615
    0.974678 0.0) (12 -0.974678 0.223615 0.0) (79 . 0) (146 . 0.0))
    Again
    ((0 . "UCS") (2 . "Test 4") (70 . 0) (10 81.6946 16.0317 0.0) (11 0.95056
    0.310541 0.0) (12 -0.310541 0.95056 0.0) (79 . 0) (146 . 0.0))
    Again
    nil
    Again
    End
    Command:
    Why does it go thru the entire While funciton AFTER it becomes nil?

    [ Moderator Action = ON ] What are [ CODE ] tags... [ Moderator Action = OFF ]
    Last edited by Mike.Perry; 2006-08-04 at 12:01 AM. Reason: [CODE] tags added.

  5. #5
    I could stop if I wanted to
    Join Date
    2006-07
    Posts
    233
    Login to Give a bone
    0

    Default Re: Trying to figure out why "While" doesn't stop as advertised in the help documentation

    Here try this not sure if its what your looking for though.


    Code:
    (defun C:UCSCOPY ()
      (setq table (tblnext "ucs" t))
      (print table)
      (prompt "\nFirst")
    
      (while table
    	(if(setq table (tblnext "ucs"))
    	  (progn
    	   (print table)
    	    (prompt "\nAgain")
    	  );;end of progn
    	);; end of if
      );; end of while
      (prompt "\nEnd")
      (princ)
    )

  6. #6
    100 Club intergrupocr's Avatar
    Join Date
    2006-02
    Posts
    116
    Login to Give a bone
    0

    Default Re: Trying to figure out why "While" doesn't stop as advertised in the help documentation

    Quote Originally Posted by eagledon
    Ok, so I was wrong, I dont understand the nil test.
    This code:

    Code:
    (defun C:UCSCOPY ()
    (setq table (tblnext "ucs" t))
    (print table)
    (prompt "nFirst")
     
    (while table
    	(setq table (tblnext "ucs"))
    	(print table)
    	(prompt "nAgain")
    )
    (prompt "nEnd")
    (princ)
    )
    (prompts are for tracking purposes)




    returns this:
    Command: ucscopy


    ((0 . "UCS") (2 . "Test 1") (70 . 0) (10 9.94824 6.66425 0.0) (11 0.901829
    0.432093 0.0) (12 -0.432093 0.901829 0.0) (79 . 0) (146 . 0.0))
    First
    ((0 . "UCS") (2 . "Test 2") (70 . 0) (10 30.8981 -5.72374 0.0) (11 0.809488
    -0.587137 0.0) (12 0.587137 0.809488 0.0) (79 . 0) (146 . 0.0))
    Again
    ((0 . "UCS") (2 . "Test 3") (70 . 0) (10 54.7626 34.9228 0.0) (11 0.223615
    0.974678 0.0) (12 -0.974678 0.223615 0.0) (79 . 0) (146 . 0.0))
    Again
    ((0 . "UCS") (2 . "Test 4") (70 . 0) (10 81.6946 16.0317 0.0) (11 0.95056
    0.310541 0.0) (12 -0.310541 0.95056 0.0) (79 . 0) (146 . 0.0))
    Again
    nil
    Again
    End
    Command:

    Why does it go thru the entire While funciton AFTER it becomes nil?



    [ Moderator Action = ON ] What are [ CODE ] tags... [ Moderator Action = OFF ]
    Thats because you use "(print table)" after "(setq table (tblnext "ucs"))", in the last loop table will be nil and will be printed.

    Try the same code with a little modification:

    Code:
    (defun C:UCSCOPY ()
    (setq table (tblnext "ucs" t))
    ;;(print table) remove
    (prompt "nFirst")
    (while table
    	(print table) ;;moved
    	(prompt "nAgain") ;;moved
    	(setq table (tblnext "ucs"))
    )
    (prompt "nEnd")
    (princ)
    )
    I hope this help you!!!

  7. #7
    All AUGI, all the time
    Join Date
    2015-12
    Location
    Central Oregon
    Posts
    591
    Login to Give a bone
    0

    Default Re: Trying to figure out why "While" doesn't stop as advertised in the help documentation

    Another way to skin the cat:
    Code:
    (defun C:UCSCOPY (/ table first)
      (while (setq table (tblnext "ucs" (not table)))
        (if	(not first)
          (setq first (princ "\nFirst: "))
          (princ "\nAgain: ")
        )
        (print table)
      )
      (prompt "\nDone!")
      (princ)
    )

  8. #8
    Member
    Join Date
    2005-05
    Posts
    38
    Login to Give a bone
    0

    Default Re: Trying to figure out why "While" doesn't stop as advertised in the help documentation

    Damn, thats so elegant, it makes my head spin...


    Quote Originally Posted by miff
    Another way to skin the cat:
    Code:
    (defun C:UCSCOPY (/ table first)
      (while (setq table (tblnext "ucs" (not table)))
        (if	(not first)
          (setq first (princ "\nFirst: "))
          (princ "\nAgain: ")
        )
        (print table)
      )
      (prompt "\nDone!")
      (princ)
    )

  9. #9
    Member
    Join Date
    2005-05
    Posts
    38
    Login to Give a bone
    0

    Default Re: Trying to figure out why "While" doesn't stop as advertised in the help documentation

    Step by step...
    Ok, now why does this not work???
    (building off the above list of 32 items)

    Code:
    (defun c:typelist (\ count ucsent)
    (setq count 0)
    (while (setq ucsent (nth count saveucs))
      (print (car ucsent))
      (print (type (car ucsent)))
      (print (cdr ucsent))
      (print (type (cdr ucsent)))
      (setq count (+ count 1))
      )
      )

Similar Threads

  1. CV234-3P: "Field to Finish": Sculpting a Better Figure
    By Autodesk University in forum Civil Infrastructure
    Replies: 1
    Last Post: 2018-04-08, 08:34 AM
  2. 2015: Raytrace "Stop" button doesn't work
    By narlee in forum Revit - Rendering
    Replies: 0
    Last Post: 2014-05-24, 12:18 PM
  3. 2013: Topo surface countours "Start" and "Stop" from where?
    By dan537c303956 in forum Revit Architecture - General
    Replies: 8
    Last Post: 2013-03-18, 06:15 PM
  4. "Breaking" Survey Figure lines
    By william.lingafelt in forum AutoCAD Civil 3D - General
    Replies: 3
    Last Post: 2007-04-09, 02:15 PM
  5. Replies: 3
    Last Post: 2006-03-23, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •