TI-Basic Z80 Programming/Basic Variables

From testwiki
Revision as of 17:00, 10 November 2020 by imported>SweetCanadianMullet (Fix link, reword)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 recall the value of X, press Template:TI-Basic/Key [X], then push Template:TI-Basic/Key: Template:Bcode:FOutput or to recall Str1: Template:Bcode:FOutput or to recall L1: Template:Bcode:FOutput

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 (±9*1099). 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: Template:Bcode:Syntax

Examples

Literals

Template:Bcode:Example

Variable

Template:Bcode:Example 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

Template:Bcode:ExampleIn 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

Template:Bcode:Syntax To instantiate a list, the following code is used: Template:Bcode:Example It is important to first instantiate a list before attempting to access it so that the size is appropriate for usage. The dim( (Template:TI-Basic/Key [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).

Template:Warning

Lists can only store numbers.

Examples

Literals

Template:Bcode:Example

Custom Named list

Template:Bcode:Example

List to List

Template:Bcode:Example

Equations

Template:Bcode:Example 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

Template:Bcode:Syntax

Examples

Literals

Template:Bcode:Example

Str to Str

Template:Bcode:Example

Concatenation/Combination

Template:Bcode:Example

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 A/B, (AB)A, and B+(A2+4B). Template:Collapse top Template:Bcode:Example Which outputs: Template:Bcode:FOutput Template:Collapse bottom

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. Template:Collapse top Template:Bcode:Example Which outputs: Template:Bcode:FOutput Template:Collapse bottom

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: Template:Bcode:FOutput Template:Collapse top Template:Bcode:Example Template:Collapse bottom Template:BottomNav