Welcome to this AutoLISP tutorial! Here, we’ll explore a straightforward program that accepts a range of numbers from the user and prints each value within that range. This hands-on exercise will not only help reinforce your understanding of loops in AutoLISP but will also provide practical experience in interacting with the user and processing their inputs.
ObjectiveYour task is to create an AutoLISP program that prompts the user to input two numbers within specific ranges. The program will then display each number within that range, one by one, in the text screen. Finally, it will output the total count of numbers displayed. |
Specifications:
- Input:
- Prompt the user for a starting number. This number should be between 1 and 10 (inclusive).
- Prompt the user for an ending number. This number should be between 11 and 25 (inclusive).
- Process:
- Display each number in the given range, starting from the start number and ending at the end number, inclusive.
- Count the total number of numbers displayed.
- Output:
- After displaying all the numbers, the program should output the total count of numbers shown to the user.
Remember to consider the user experience when designing your program. Provide clear prompts and messages to guide the user through the process.
AutoLISP Tutorial 7 Solution: Printing a Range of Numbers
Introduction:
In this solution, we will walk through a simple AutoLISP program that takes two number inputs from the user and then displays each number within that range on the text screen. By the end of the sequence, it will inform the user of the total number of values displayed.
Program:
(defun C:TUTO7 ( / num1 num2 cou1) (setq num1 (getint “\nEnter the start number from 1 to 10: “)) (setq num2 (getint “Enter the end number from 11 to 25: “)) (setq cou1 0) (while (<= num1 num2) (textscr) (princ “\n”)(princ num1) (setq num1 (+ num1 1)) (setq cou1 (+ 1 cou1)) ) (princ “\n”)(princ “\n”)(princ cou1)(princ ” numbers were printed.”) (princ) ) |
Explanation:
(defun C:TUTO7 ( / num1 num2 cou1): This line begins our function definition named C:TUTO7. The variables num1, num2, and cou1 are defined as local to this function.
(setq num1 (getint “\nEnter the start number from 1 to 10: “)): Here, we prompt the user to enter the starting number. The input is expected to be an integer between 1 and 10, inclusive.
(setq num2 (getint “Enter the end number from 11 to 25: “)): This prompts the user to provide the ending number, expected to be an integer between 11 and 25.
(setq cou1 0): We initialize our counter variable, cou1, to zero. This will keep track of the total numbers displayed.
(while (<= num1 num2): A while loop is started, which will continue to execute as long as num1 is less than or equal to num2.
(textscr): This ensures that the output is displayed in the text screen of AutoCAD.
(princ “\n”)(princ num1): Each number in the range is printed on a new line.
(setq num1 (+ num1 1)): We increment the value of num1 by one for the next iteration of the loop.
(setq cou1 (+ 1 cou1)): The counter variable cou1 is increased by one every iteration, keeping count of the numbers printed.
(princ “\n”)(princ “\n”)(princ cou1)(princ ” numbers were printed.”): Once the loop completes, this line prints the total number of values displayed to the user.
(princ): This is the function’s ending statement and ensures a clean exit without leaving any unwanted characters or values in the command line.
By following this breakdown, you should have a clear understanding of how the program operates and how each component contributes to the desired output.
Conclusion:
Congratulations on completing the tutorial! By now, you should have a firm grasp on how to get user inputs, run loops based on conditions, and display results back to the user in AutoLISP. Remember, practice is the key to mastering any programming language, so try to experiment further by modifying the program or creating new ones based on the concepts you’ve learned here.