Variables and Their Commands
Page Contents
Variables are used extensively in programming, as most programs use variables in one form or another. They are used to keep track of numbers or text or stats; there are many uses for variables. Put simply, programming wouldn't be programming without variables.
A variable is a reference to the information that it holds. Variables allow you to store the information, so that you can later use it for whatever purpose is desired. The thing to remember, though, is that programs all share the variables.
There are more variables than just the ones that are described on this page, but the four main variables that you will be using are reals, lists, matrices, and strings. Reals are used for storing a single number. Lists are used for storing a collection of numbers. Matrices are used for storing numbers in a two-dimensional format. And, strings are used when you want to store text.
Storing & Deleting Variables
Variables have values stored in them so that the values can be recalled for later use. When storing an expression containing a variable into another variable, the value of the variable at that time is used. The store (→) command is used for storing variables, and it is accessed by pressing the [STO] key. When storing a value in a variable, you have the value on the left side of the store command and the variable that it will be stored to on the right side.
Format
:value→variable
When you are done using variables, you should delete them with the DelVar
command to save space. The DelVar
command deletes the contents of a variable from memory. If the DelVar
command is used with a real variable, the variable is not only deleted from memory but automatically set to zero the next time it is used. DelVar
does not work on specific elements of a list or matrix. In fact, it will actually return an error.
Format
:DelVar variable
Real Variables (Reals)
Real variables (reals) are used for storing numbers. There are 27 predefined real variables (from A to Z and θ) that can be used, and each one is capable of holding a single number. Real variables are 15 bytes, but they can have a high level of precision (only the first 10 digits will show when the number is displayed, however, because of scientific notation).
Format
:#→realvariable
Because the X
and Y
variables are used by the calculator during graphing, and they get set back to their defaults when the graphscreen is first accessed, you should avoid using them for storing anything important. Instead, they should only be used as temporary counters (for things such as For
loops).
To access a real variable, press ALPHA and then the key corresponding to whatever letter you want your variable to be. You can initialize a real variable by storing a number, another variable, or an expression into the variable using the STO key. When an expression containing a real variable is evaluated, the value of the variable at that time is used.
List Variables
Lists are very versatile variables. They are used for storing highscores and level information, and just about anything else. Lists are also important because they are the only variable that you can actually create. This adds a certain security to using them. Other programs can still access your lists (and change or corrupt them), however, but there is a smaller likelihood of this happening simply because they can create their own lists, too.
A list is a collection of elements (made up of numbers, variables, or expressions). Although lists can have up to 999 elements, they are limited by the amount of free RAM. In addition to any lists that you create, there are also six built-in lists (from L1 to L6). These lists are accessed by pressing [2nd] and [1] to [6] (for whichever list you want). A custom list name can be one to five characters, comprised of any combination of capital letters and numbers and theta, but it must begin with a letter or theta.
There are two ways to create a list: manually write it or use the dim command. After you create a list, you can access it through the list menu.
In either case, you use the little L
command (which identifies lists). To access the little L
command you need to press [2nd] and [STAT] to get to the list menu. You then press right once and scroll down to the bottom of the menu.
When manually writing a list, you begin with putting a single opening curly brace ({
) that will enclose the list. You then type a number or variable or expression, and put a comma after it. You repeat this for however many elements you want. You then put a single closing curly brace (}
) that will close the list, and store (→) that into whatever list variable you are creating. You have to put the little L
command before the list name.
Format :{#,#,#,...}→Llistname
The dim command can be used to create a list with however many elements you want. It can also be used to find the dimensions or to redimension an existing list. The dim command can be found in the list menu by pressing [2nd] [STAT] and right once. You then scroll down to the command.
When using the dim command, you first enter the number of elements you want. You then store that into the dim command. Inside the dim command is the list variable that you are creating. You have to put the little L
command before the list name.
Format :#→dim(Llistname)
When you first create a list using the dim command, all of the elements will have a value of zero. If the list already existed, the previous elements in the list that are within the new list dimensions are not changed. If the previous dimensions of the list were larger than the dimensions chosen, the elements outside the list dimensions are deleted.
Matrix Variables
Matrices are two-dimensional lists (row by column). They are used for holding lots of information. Matrices are often used for storing a level or map of the screen. Matrices are larger than the other variables, however.
There are only ten built-in matrices (from [A] to [J]). A matrix may have up to 99 rows or columns (depending on available memory). A matrix is like a grid. To access the matrix variables you need to press [2nd] and [X-1]. You can then scroll down to whatever matrix you want to use or press the corresponding number.
There are two ways to create a matrix: manually write it or use the dim command.
When manually writing a matrix, you need to first think about what dimensions (how may rows by how many columns) you want the matrix to be. This is because there can potentially be errors with the numbers or the rows or the columns. It is easy to get confused with all of the braces ([]
) and numbers.
After you know what dimensions you want to use, you can begin making the matrix. You first start with a single opening bracket ([
) that will enclose the matrix. You then put an opening bracket for the first row. You then type each number that will be in the row, separating them with commas (,
). You then close the first row with a closing bracket (]
). You repeat this until you have all of the rows. You then put a single closing bracket that will finish the matrix. Finally, you store that into whatever matrix you want to use.
Format
:[[#,#,#,...][#,#,#,...]...]→matrixname
The dim command can be used to create a matrix with however many rows and columns you want. It can also be used to find the dimensions or to redimension an existing matrix.
When using the dim command, you first need an open curly brace. You then enter a number for the rows between 1 and 99, a comma, a number for the columns between 1 and 99, and a closing curly brace. You then store that into the dim command (found by pressing [2nd] [X-1] and [>] [3]), with the matrix you want to use.
Format
:{rows,columns}→dim(matrixname)
When you first create a matrix using the dim command, all of the elements (coordinate pairs) will have a value of zero. If the matrix already existed, the elements in the old matrix that are within the new matrix are not changed. Elements in the old matrix that are outside the new dimensions are deleted.
Matrix Operations
There are several different matrix commands available to you for manipulating matrices: Fill, randM, and augment. These commands can be used for whatever way you want to change matrices.
The Fill command stores the specified value to every element in the matrix, replacing the current value (with the changes stored back into the matrix). This is useful when you want to change the whole matrix and you don't want to have to use a For loop to go through every element.
Format :Fill(value,matrix)
The randM command creates a matrix with the specified row x column dimensions and fills it with random numbers. The default values for the elements are integers from -9 to 9, but you can multiply the randM command by a number, variable, or expression to get a wider range. Also, the seed value stored to the rand command controls the values. The matrix is not automatically stored into a matrix variable.
Format :randM(rows,columns)
The augment command appends one matrix to another matrix as new columns (the first matrix is added to the second matrix). You can also append the same matrix to itself, creating a larger matrix. Both matrices must have the same number of rows, otherwise an error will be returned. Neither of the matrices are changed.
Format :augment(matrix,matrix)
To use the matrix commands, you should first be in the Program editor for your program. In the Program editor, press 2nd X^-1 for the MATRIX menu, then arrow over to the MATH menu. Next, scroll down to whichever matrix command you want to use and press ENTER. Now the command has been put into your program.
String Variables
Strings are used for storing a sequence of characters (i.e. text) with many different purposes: highscores, level and map data, character names, and whatever else is desired. Although there are only ten built-in string variables (Str0
through Str9
) available to use, strings can hold many different kinds of characters, including letters (both uppercase and lowercase), numbers, functions, and even other commands. The amount of free RAM is the only limit on the number of characters in a string.
Each function and command is counted as one character, though, which can cause problems when displaying strings. The two characters that you can't put in a string are quotation marks (") and the store command (→
). However, you can mimic these respectively by using two apostrophes (''), and two subtract signs and a greater than sign (-->).
When entering a string, you need to put a quote at the beginning and ending of the string to enclose the characters inside the string (the quotes do not count as string characters). This also indicates that it is a string. Inside the string, you then enter whatever characters you want. If you want to store the string to a string variable for later use in a program, you just select which string variable you want to use and then store the string to it with the STO key.
Format
:"Text"→string
To use the string variables, you should first be in the Program editor for your program. In the Program editor, press VARS and then scroll down to Strings (or press 7). You then scroll down to whichever string variable you want to use and press ENTER. Now the string variable has been put into your program, and you have to enter the text that you want in the string.
String Operations
There are several different string commands available to you for manipulating strings: comparison (=
), concatenation (+
), length
, inString
, sub
, and expr
. You can combine these commands to get or test any part of a string, or to manipulate strings in pretty much any way you want.
When comparing strings, you start with the first character (string positions are counted starting with 1) and move from left to right. Because strings are case sensitive, two strings are equal only if all the characters in the same positions match each other.
Format
:If string=string
Concatenation involves putting two or more strings together to form a single, longer string. When concatenating, you enter a string of text or string variable and then add a plus sign (+
) after it. You repeat this process for however many strings you want to add, except you don't put a plus sign after the last string.
Format
:string+string
You use the length
command to get the number of characters in a string (to see how long the string is). The string can be either a string of text or a string variable. Commands and math functions can be put in a string, but they only count as one character. You can store the length to a variable for later use in a program.
Format
:length(string)
The inString
command is used to search strings for substrings (individual parts of text within the string). The inString
command has two arguments: the string that you want to use and the substring that you want to search for within that string. The string can be either a string of text or a string variable.
Format
:inString(string,substring)
The search starts at the beginning of the string and continues until the substring is found. The position of the first character of the substring will be returned if the substring is found, but 0 will be returned if the substring is not found or the start is greater than the length of the string.
There is an optional third argument that allows you to specify the starting position of the search. This can be used to find occurrences of the substring in the middle or near the end of the string, or to skip over part of the string.
Format
:inString(string,substring,start)
The sub
command is used to extract a substring from a string. sub
has three arguments: the string that you want to get the substring from (can either be a string of text or a string variable), the starting position of the first character of the substring, and the number of characters that you want the substring to be. You can store the substring into another string or back into the string itself.
Format
:sub(string,start,length)
When extracting substrings, there are two special cases that you will usually have to deal with: getting the beginning and ending of the substring. While getting the beginning is fairly easy (just use 1 as the start), to get the ending you need to use the length
command to ensure you don't go past the end of the string because the string length can change.
Example
:sub(Str1,1,1
:sub(Str1,length(Str1)-1,1
The expr
command allows you to convert strings into expressions (expressions involve math with numbers and/or variables). The string can be a string of text or a string variable. You can combine other commands inside of it to only convert parts of the string. An error will occur if the string does not return an expression.
Format
:expr(string)
To use the string commands, you should first be in the Program editor for your program. In the Program editor, press 2nd and 0 to open the Catalog menu (which is the only menu that has the string commands). Then scroll down to whichever command you want, or press the key that has the command's first letter to get to the first command with that first letter, and press ENTER. Now the command has been put into your program.