TI-Nspire Programming Editor Guide en
TI-Nspire Programming Editor Guide en
TI-Nspire™ Technology
Program Editor
ii
Contents
General Information 25
Online Help 25
Contact TI Support 25
Service and Warranty Information 25
iii
Getting Started with the Program Editor
You can create user-defined functions or programs by typing definition statements on
the Calculator entry line or by using the Program Editor. The Program Editor offers
some advantages, and it is covered in this section. For more information, see
Calculator.
• The editor has programming templates and dialog boxes to help you define
functions and programs using correct syntax.
• The editor lets you enter multiple-line programming statements without requiring
a special key sequence to add each line.
• You can easily create private and public library objects (variables, functions, and
programs). For more information, see Libraries.
Launching the Program Editor
▶ To add a new Program Editor page in the current problem:
From the toolbar, click Insert > Program Editor > New.
Program Editor menu – This menu is available anytime you are in the Program Editor
À work area using the Normal view mode.
Á Program Editor work area
Status line shows line-number information and the name of the function or program
 being edited. An asterisk (*) indicates that this function is “dirty,” which means that
it has changed since the last time its syntax has been checked and it has been stored.
- You can either type the names of functions and commands or insert them from
the Catalog.
- A line can be longer than the width of the screen; if so, you might have to scroll
to view the entire statement.
- After typing each line, press Enter. This inserts a new blank line and lets you
continue entering another line.
- Use the ◄, ►, ▲, and ▼ arrow keys to scroll through the function or program
for entering or editing commands.
Inserting Comments
Comments can be useful to someone viewing or editing the program. They are not
displayed when the program runs and have no effect on program flow. The © symbol
displays at the beginning of the line with the comment.
Define LibPub volcyl(ht,r) =
Prgm
©volcyl(ht,r) => volume of cylinder À
Disp “Volume =”, approx(p ¦ r2 ¦ ht)
©This is another comment.
EndPrgm
Comment showing required syntax. Because this library object is public and this
À comment is the first line in a Func or Prgm block, the comment is displayed in the
Catalog as help. For more information, see Libraries.
2. Type the text that you want to find, and click OK.
- If the text is found, it is highlighted in the program.
- If the text is not found, a notification message is displayed.
Finding and Replacing Text
1. From the Actions menu, click Find and Replace.
2. On the toolbar, click the Document Tools button and select Check Syntax &
Store > Run.
—or—
Press Ctrl+R.
Handheld: Press b 2 3, or press / R.
3. If the program or function requires you to supply one or more arguments, type the
values or variable names inside the parentheses.
4. Press ·.
Note: You can also run a program or function in Calculator or Notes applications by
typing the name of the program with parentheses and any required arguments and
pressing ·.
Using Short and Long Names
Anytime you are in the same problem where an object is defined, you can access it by
entering its short name (the name given in the object’s Define command). This is the
case for all defined objects, including private, public, and non-library objects.
4. If the program or function requires you to supply one or more arguments, type the
values or variable names inside the parentheses.
libs2\func1(34,power)
5. Press ·.
Using a Private Library Program or Function
To use a Private library object, you must know its long name. For example, the long
name of the object defined as func1 in the library document lib1 is lib1\func1.
Note: If you cannot remember the exact name or the order of arguments required for a
private library object, you can open the library document or use the Program Editor to
view the object.
1. Make sure you have defined the object in the document’s first problem, stored the
object, saved the library document in the MyLib folder, and refreshed the libraries.
2. Open the TI-Nspire™ application in which you want to use the program or function.
Note: All applications can evaluate functions, but only the Calculator and Notes
applications can run programs.
3. Type the name of the object. In the case of a program or function, always follow
the name with parentheses.
libs2\func1()
5. Press ·.
Interrupting a Running Program or Function
2. Run the program to display the volume of a cylinder with a height of 34 mm and a
radius of 5 mm.
volcyl(34,5) Volume = 534.071
Note: You do not have to use the parameter names when you run the volcyl
program, but you must supply two arguments (as values, variables, or expressions).
The first must represent the height, and the second must represent the radius.
Requesting the Values from the User (Programs Only)
You can use the Request and RequestStr commands in a program to make the program
pause and display a dialog box prompting the user for information. This method does
not require users to remember variable names or the order in which they are needed.
You cannot use the Request or RequestStr command in a function.
1. Define the program.
Define calculatearea()=
Prgm
Request "Width: ",w
Request "Height: ",h
area:=w*h
EndPrgm
Use RequestStr instead of Request when you want the program to interpret the user’s
response as a character string rather than a math expression. This avoids requiring the
user to enclose the response in quotation marks (““).
Note: Displaying a result with Disp or Text does not store that result. If you expect to
refer later to a result, store it to a global variable.
©
cos(π/4)→ maximum
Disp maximum
©
Local i À
For i,0,5,1
Disp i
EndFor
Disp i
Note: When possible, declare as local any variable that is used only within the program
and does not need to be available after the program stops.
What Causes an Undefined Variable Error Message?
An Undefined variable error message is displayed when you evaluate a user-defined
function or run a user-defined program that references a local variable that is not
initialized (assigned a value).
For example:
Define fact(n)=Func
Local m À
While n>1
n¦m& m: n–1& n
EndWhile
Return m
EndFunc
Note: Use the Program Editor’s Var menu to enter the Define and Prgm...EndPrgm
commands.
Notes about Using Subroutines
At the end of a subroutine, execution returns to the calling program. To exit a
subroutine at any other time, use Return with no argument.
A subroutine cannot access local variables declared in the calling program. Likewise,
the calling program cannot access local variables declared in a subroutine.
Lbl commands are local to the programs in which they are located. Therefore, a Goto
command in the calling program cannot branch to a label in a subroutine or vice versa.
Avoiding Circular-Definition Errors
When evaluating a user-defined function or running a program, you can specify an
argument that includes the same variable that was used to define the function or
create the program. However, to avoid circular-definition errors, you must assign a
x+1& x À
– or –
For i,i,10,1
Disp i À
EndFor
Causes a Circular definition error message if x or i does not have a value. The error
À does not occur if x or i has already been assigned a value.
In this example, you must store a value to x before executing the If command.
Note: EndIf marks the end of the Then block that is executed if the condition is true.
If...Then...Else...EndIf Structures
To execute one group of commands if a conditional test is true and a different group if
the condition is false, use this structure:
If x>5 Then
Disp "x is greater than 5" À
2¦x& x À
Else
Disp "x is less than or equal to 5" Á
5¦x& x Á
EndIf
Disp x Â
You can then use the Goto command at any point in the function or program to branch
to the location that corresponds to the specified label.
Goto labelName specifies which Lbl command to branch to
Because a Goto command is unconditional (it always branches to the specified label),
it is often used with an If command so that you can specify a conditional test. For
example:
If x>5
Goto GT5 À
Disp x
--------
-------- Á
Lbl GT5
Disp "The number was > 5"
At each iteration of the For loop, the variable value is compared to the end value. If
variable does not exceed end, the commands within the For...EndFor loop are executed
and the loop repeats; otherwise, control jumps to the command following EndFor.
Note: The For command automatically increments the counter variable so that the
function or program can exit the loop after a certain number of repetitions.
At the end of the loop ( EndFor), control loops back to the For command, where the
counter variable is incremented and compared to end.
For example:
For i,0,5,1
Disp i À
EndFor
Disp i Á
À Displays 0, 1, 2, 3, 4, and 5.
Á Displays 6. When variable increments to 6, the loop is not executed.
Notes:
• You can declare variable as local if it does not need to be saved after the function
or program stops.
• You can set end to a value less than begin, provided you also set increment to a
negative value.
While...EndWhile Loops
A While...EndWhile loop repeats a block of commands as long as a specified condition
is true. The syntax of the While command is:
Note: The While command does not automatically change the condition. You must
include commands that allow the function or program to exit the loop.
At the end of the loop ( EndWhile), control jumps back to the While command, where
condition is re-evaluated.
To execute the loop the first time, the condition must initially be true.
• Any variables referenced in the condition must be set before the While command.
(You can build the values into the function or program, or you can prompt the user
to enter the values.)
• The loop must contain commands that change the values in the condition,
eventually causing it to be false. Otherwise, the condition is always true and the
function or program cannot exit the loop (called an infinite loop).
For example:
0& x À
While x<5
Disp x Á
x+1& x Â
EndWhile
Disp x Ã
À Initially sets x.
Á Displays 0, 1, 2, 3, and 4.
 Increments x.
à Displays 5. When x increments to 5, the loop is not executed.
Loop...EndLoop Loops
A Loop...EndLoop creates an infinite loop, which is repeated endlessly. The Loop
command does not have any arguments.
The If command could also use a Goto command to transfer program control to a
specified Lbl (label) command.
Repeating a Loop Immediately
The Cycle command immediately transfers program control to the next iteration of a
loop (before the current iteration is complete). This command works with For...EndFor,
While...EndWhile, and Loop...EndLoop.
Lbl and Goto Loops
Although the Lbl (label) and Goto commands are not strictly loop commands, they can
be used to create an infinite loop. For example:
Command Description
ClrErr Clears the error status and sets system variable errCode to zero. For an
example of using errCode, see the Try command in the Reference
Guide.
Contact TI Support
education.ti.com/ti-cares
Select your country for technical and other support resources.
25 General Information