TI-BASIC:SK:Ans

From Learn @ Cemetech
Revision as of 18:33, 24 February 2016 by Maintenance script (talk | contribs) (Initial automated import)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

The Ans token

What is Ans?

The Ans command is a difficult to understand thing. It returns the last value to be assigned in the Home Screen. A value assigned in the home screen is every single variable or string or list... that are created or set to a value. To understand the concept of the Ans we'll go to the main calculator screen and type a number:

:8
		8
:Ans
		8

So whenever an object (a variable or a list or a string ...) appears in the right side of the Main screen it is assigned to it. So the last becomes the Ans. Take a look at the next example:

:8
		8
:Ans
		8
:10
		10
:Ans
		10
:"HELLO"
	HELLO
:Ans
	HELLO
:{1,2,3}
		{1,2,3}
:Ans
		{1,2,3}
And so on...

As you can see the Ans can be everything. Now look at the next example:

:4→A
		4
:Ans
		4

So when you set a variable its value becomes the Ans!!! And in a program it works the same way.

Why should I use the Ans?

Basically the Ans command works like some kind of calculator cache. It is the last value assigned and because of how it is built it is faster to use the Ans (just like a computer gets data from the cache faster than from the main memory). So if I use an Ans command (when possible) instead, the program will process that information faster and the user will be happier.

Some uses of the Ans:

Lets see the next example:


:Repeat D=21
:getkey→D
:IF Ans
DISP "A KEY WAS PRESSED
IF Not(Ans
DISP "NO KEY BEING PRESSED
:END


In this example, the calculator tells the user whether he is pressing a key or not. Let's have a closer look: The REPEAT D=21 will execute the instructions underneath it until the variable D is equal to 21. The getkey command (we'll expand on that later) will check the key you are pressing and return the number equivalent to the key (if you are not pressing any it returns 0) and assign it to the variable D. Now the value you assigned is in Ans, so we may use Ans instead of the D variable. So if Ans is different from 0, we display "A KEY WAS PRESSED". If it isn't different, we display "NO KEY BEING PRESSED". Easy, hum?

Please note that DISP, along with some other functions, won't affect the value of the Ans command.

More on the Subject

An in depth look at the Ans variable can be found here.

<< Loops Table of Contents Chapter Summary >>