TI-Basic Z80 Programming/Basic Variables

From Wikibooks, open books for an open world
Jump to navigation Jump to search

What are Variables?

Variables are the meat of any programming language as they are used to store and work with data. With variables, the outcomes of programs can differ depending on the user's input or the purpose of the program. Variables in the TI calculators can store different types of data, whether it be numbers, lists of numbers, strings, mathematical functions, etc. However, each data type has its own type of variable that it can be stored in and the rules must be followed fairly strictly.

TI-BASIC is unusual among programming languages in that it does not support actual variables. Instead, all data are treated like files; there is no distinction between an ordinary number and an image, for example. TI refers to all files as "variables." Henceforth, "variable" will refer to a file usable by a program.

Types of Basic Variables

There are many types of variables, but in this chapter, only the most common ones will be dealt with. The Advanced Variables section will deal with the more complex variable types and uses. The following sections will deal with:

  • Number — numbers (e.g., 1, -0.5, 3.14, i, 3i+2)
  • List — array of numbers (e.g., {1 2 3 4 5})
  • String — text (e.g., "HELLO, WORLD")

Storing and Recalling Variables

Variables can be stored and recalled at the home screen, or within a program by simply using that variable's name. The method for recalling variables varies depending on the type of variable:

  • To type a number variable, press ALPHA, then the corresponding key for that letter.
  • To type a list variable, press 2ND [LIST], then select the desired list in the list.
  • To type a string variable, press VARS 7, then select the desired string in the list.

To recall the value of X, press ALPHA [X], then push ENTER:

{{{1}}}

or to recall Str1:

{{{1}}}

or to recall L1:

{{{1}}}

Numbers

Numbers are stored into variables labelled A through Z and θ and can be real or complex numbers (complex numbers can only be used if the calculator is in a+bi or re^θi modes).

Number variables store both the integer and decimal part of a number. Examples of number variables are 0, 2.1, 5, 7.212, 3i, or 3.1415926. Number variables are accurate up to eight significant digits and can be in the range of -9ᴇ99 to 9ᴇ99 (). If an attempt is tried to evaluate or store a value outside of the range, the calculator returns an error.

The calculator can update X, Y, R, θ, and T during graphing, so you may want to avoid using these variables to store non-graphing data.

Syntax

To store a number to a number variable, the syntax is as follows:

valuevariable
  • Where value is a literal value, a variable, or an expression
  • Where variable is the variable to store value to

Examples

Literals

5.32→X


Variable

A→X

In this example, only the value of A is stored to X (i.e., changes to A will not be reflected in X after the assignment).

Equation

10/2+36+89/A→X

In this example, if A = 89, X = 42, not the actual equation. Only the result of an equation is stored (the equation is 5+36+89/89 = 42, so X = 42).

Lists

Lists are essentially an array: they store an array of numbers. The individual numbers of a list are named elements. The maximum number of elements in a list is 999.

Syntax

{value1,value2,...,valueN}→listName
  • Where value1,value2 through valueN are number elements
  • Where listName is the name of a list. This can be one of two types:
    • Calculator defined: L1, L2, L3, L4, L5, L6
    • User defined: L (2ND [LIST] OPS B) followed by characters denoting a name, maximum of 6 tokens, letters only
  • The amount of elements in a list may not exceed 999.

To instantiate a list, the following code is used:

DelVar L1
n→dim(L1)
* Where the first line deletes L1 if it exists, and the second line instantiates L1 with a size of n.

It is important to first instantiate a list before attempting to access it so that the size is appropriate for usage. The dim( (2ND [LIST] OPS 3) command stands for dimension, and in this case, we have set n as the dimension (or size) of the list.

To access a single element in a list, use the format L1(N), where N is the N-th element in the list. The index is 1-indexed, so to reference the first element in L1, use L1(1).

Lists can only store numbers.

Examples

Literals

{15,20,30}→L1

Custom Named list

{1,2,3,4,5}→LMYLIST

List to List

L1→L2

Equations

{15,20,30}+5→L1

In this example, L1 would consist of {20 25 35} because each element was increased by 5 then stored to L1.

Strings

Strings hold text.

Syntax

stringstrN
  • Where string is the string literal, or some other form of string to store to strN and
  • Where strN is one of the predefined strings for the calculator.

Examples

Literals

"BOB SMITH"→Str1

Str to Str

Str1→Str2

Concatenation/Combination

"MY NAME IS "+Str1+" AND YOU KNOW IT!"→Str2


Incompatible Types

It should be noted that variables can only contain their respective data type. For example, trying to store a number to a string object (0→Str1) will result in an error.

You try it!

Try these examples to practice using the different data types.

Arithmetic

Use variables to store numbers, then perform simple operations on them. Make variables A and B equal to 3 and 7, respectively. Then output , , and .

Solution
ClrHome
3→A
7→B
Disp A/B
Disp (AB)^A
Disp B+(A^(2+4B))

Which outputs:

{{{1}}}

List Operation

Create a simple list using these numbers: 3,6,8. Now, using Disp, output each value to the screen, then output the average of the values of the list by accessing them from the list. Remember, to type a list, press 2ND then a number from 1-6.

Solution
{3,6,8}→L1
ClrHome
Disp L1(1)
Disp L1(2)
Disp L1(3)
Disp (L1(1)+L1(2)+L1(3))/3

Which outputs:

{{{1}}}

String Concatenation

Set Str1 to your first name and set Str2 to your last name. Then, using string concatenation, print your first and last names on two lines, with preceding text FIRST: and LAST:. For example, your output would appear as follows:

{{{1}}}
Solution
"JOHN"→Str1
"DOE"→Str2
ClrHome
Disp "FIRST: "+Str1
Disp "LAST: "+Str2


Previous: Hello, World!
Next: Output
Table of Contents: TI-Basic Z80 Programming