Thank you for your great suggestion Kent, I didnt see that there are two conditions to exit the while loop, "one intentional and an other by missing or hiting the wrong object. so I wanted to modify the code based on your suggestion. The code I am writing now extracts numbers from block attributes, adds them and copy the result to a clip board, so I will paste it on a table. I wanted to select as many blocks and exist the loop. what @dbroad suggested me works great in this case. But for the other case which is "missing or hiting the wrong object", It exits not only the loop but also the program. I would rather prefer if the loop continues when such mistakes happen by giving me a message "wrong object, select only blocks with this name". Here is the whole code.
;; I have a text in an attribute which is:
"DIR. BORE (284') 2-1.25" HDPE" the number '284' varies, the other characters are the same always, I wanted the number between the first parenthesis and the ' character in this case 284
(defun C:RF()
(setq addnumber 0) ;;initializing the variable 'firstnumber'
(setq total 0) ;;initializing the variable 'total'
(
while (setq obj (nentsel "Select construction BOX \n"))
(setq str (cdr (assoc 1 (entget (car obj))))) ;;extracting the attribute
(setq a (vl-string-search "\(" str)) ;;the position of the character before the number
(setq b (vl-string-search "'" str)) ;;the position of the character after the number
(setq addnumber (atoi (substr str (+ a 2) (- b (+ a 1))))) ;; extracting the number using the substring function
(setq total (+ total addnumber)) ;; adding the number and exit or continue the loop
)
(setq total (itoa total)) ;;copy the number to a clip board
(vlax-invoke
(vlax-get (vlax-get (vlax-create-object "htmlfile") 'ParentWindow) 'ClipBoardData)
'setData
"TEXT"
total
)
)