Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

TI-Basic Z80 Programming/Basic Variables

From Wikibooks, open books for an open world
<TI-Basic Z80 Programming

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. TheAdvanced 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 anumber variable, pressALPHA, then the corresponding key for that letter.
  • To type alist variable, press2ND[LIST], then select the desired list in the list.
  • To type astring variable, pressVARS7, then select the desired string in the list.

To recall the value ofX, pressALPHA[X], then pushENTER:

{{{1}}}

or to recallStr1:

{{{1}}}

or to recallL1:

{{{1}}}

Numbers

Numbers are stored into variables labelledA throughZ andθ and can be real or complex numbers (complex numbers can only be used if the calculator is ina+bi orre^θi modes).

Number variables store both the integer and decimal part of a number. Examples of number variables are0,2.1,5,7.212,3i, or3.1415926. Number variables are accurate up to eight significant digits and can be in the range of-9ᴇ99 to9ᴇ99 (±91099{\displaystyle \pm 9*10^{99}}). If an attempt is tried to evaluate or store a value outside of the range, the calculator returns an error.

The calculator can updateX,Y,R,θ, andT 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
  • Wherevalue is a literal value, a variable, or an expression
  • Wherevariable is the variable to storevalue to

Examples

Literals

5.32→X


Variable

A→X

In this example, only the value ofA is stored toX (i.e., changes toA will not be reflected inX after the assignment).

Equation

10/2+36+89/A→X

In this example, ifA = 89,X = 42, not the actual equation. Only the result of an equation is stored (the equation is5+36+89/89 = 42, soX = 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 is999.

Syntax

{value1,value2,...,valueN}→listName
  • Wherevalue1,value2 throughvalueN are number elements
  • WherelistName 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 exceed999.

To instantiate a list, the following code is used:

DelVar L1
n→dim(L1)
* Where the first line deletesL1 if it exists, and the second line instantiatesL1 with a size ofn.

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

To access a single element in a list, use the formatL1(N), whereN is the N-th element in the list. The index is 1-indexed, so to reference the first element inL1, useL1(1).

If you try to access an element that is out of the bounds of the size of the list (accessing thenth element where the size of the list is less thann orn is less than 1), you will receive an error.

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 by5 then stored toL1.

Strings

Strings hold text.

Syntax

stringstrN
  • Wherestring is the string literal, or some other form of string to store tostrN and
  • WherestrN 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 outputA/B{\displaystyle A/B},(AB)A{\displaystyle (AB)^{A}}, andB+(A2+4B){\displaystyle B+(A^{2+4B})}.

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, usingDisp, 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, press2ND 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

SetStr1 to your first name and setStr2 to your last name. Then, using string concatenation, print your first and last names on two lines, with preceding textFIRST: andLAST:. 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

Retrieved from "https://en.wikibooks.org/w/index.php?title=TI-Basic_Z80_Programming/Basic_Variables&oldid=3767499"
Categories:

[8]ページ先頭

©2009-2025 Movatter.jp