Lesson 4:

Getting user input and using boolean logic

There are different forms of getting user input. One can set up the menu( command to have the user choose an option, you can use loops with getKey to have a user move something or make a choice, or you can use the Input and Prompt commands to get data. Getting user input using Input and Prompt would be fairly useless without being able to compare what it is the user input to what value you need it to be. First, we will gather input and redisplay it, and then we shall show you how to "branch" your answers depending on the input recieved.

Gathering Input

The simplest form of getting user input is by using the Input command. This command, when used without any arguements, will place a cursor on the graphscreen, and once the user has pressed [ENTER], stores the values of X- and Y-axis from the graphscreen to the X and Y variables. Something that can be useful, and will be covered more in another lesson.

The primary use of Input is to get a value from the user, and store it into a variable, this includes normal variables, strings, lists, Y= variables, and matrices.

Here is some sample code on how to use Input in it's many forms:

:Input "A",A
:Input "Str1",Str1
:Input "L1",L1
:Input
:Output(1,1,"A:
:Output(1,6,A
:Output(2,1,"Str1:
:Output(2,6,Str1
:Output(3,1,"L1:
:Output(3,6,L1
:Output(4,1,"X:
:Output(4,6,X
:Output(5,1,"Y:
:Output(5,6,Y

Here is a screenshot of the above code in action:

The other common way to get information from a user is by using Prompt. Prompt will allow a programmer to gather input for more variables with one command, instead of having an individual Input command for each variable.

Here is a screenshot using Prompt:

As stated above, there are other forms of getting input from a user, but we will get to them in due time. Now, on to Boolean logic!

Boolean Logic

Boolean Logic is basically comparing values to see if it returns a true or false, and then executing a command, or series of commands based on the result. Commands for working with boolean logic are If, Then, Else, End, IS>( and DS<(. These commands are easy to use incorrectly, so please read through this information carefully and understand it fully before continuing on to any of the other lessons.

Your basic boolean logic in TI-BASIC can be seen in this screenshot:

What you are seeing is the calculator returning "1" for "True", and "0" for "False". Well, how is this helpful, and how does this relate to any of the commands above?? It is simple. What you are seeing is the actual comparing taking place. The If command tells the calculator that: "Hey, we have a single command to execute!" Now before going too far into this, maybe we should take a look at the different formats of If, and some of the commands that go with it (and where they are found).

  • If you need to check a value, and execute a single line of code (i.e. run a program, clear the screen, etc), then you can simply do: If A=1:ClrHome Using If

  • If you need to check a value, and execute more than one command, then you need to include the Then and End commands, like this: If A=1:ClrHome:Disp A:End

  • Say we need to check a variable for 2 possible values, 1 and 0. When the variable equals 1, it performs one task, but when it equals 0, it has to perform a different task. Here is some sample code:
    If A=1:Then:code:code:Else:code:code:End

  • Say you need two or more variables to equal something before a routine can be run, and ALL of them have to be true at the same time, then we need the and command, found in the Logic menu ([2nd], MATH, and >). If A=1 and A=3 and B=6

  • If you need a routine to be run when two or more variables can activate it, but they do not all need to be true at the same time to run it, then you can use the or command. If A=1 or B=3 or C=6

  • xor is exlusive or. It returns a 1 when comparing two variables when one variable is equal to 0. If both variables equal 0, or both are not equal to 0, xor returns a 0. Not many uses in BASIC for this command, asm uses it for sprites, but you will have to read up on an asm tutorial for more information on that.

  • The not( command returns a 0 if the variable does not equal 0. Can be handy to test a variable or list if it does not equal 0 for events or items. You can also change the variable if you want to make it equal 0 or 1, or if you need to switch between the two continously, you can do not(BB

    There are also 6 relational operators that you need to use to do Boolean Logic. They are =, , >, <, <=, >=. These can be found in the TEST menu by pressing 2nd and MATH. When using =, , >, <, <=, >=, you can only compare real numbers, expressions and lists. You can also use them to test the elements of a list and a matrix, but to compare matrix to matrix, they must be the same dimensions, and you can only use = and .

    Command definitions for this lesson:

    
    If: This command tells the calculator to check for boolean logic, and to execute a command.
    
    Then: Attached to an If command, tells the calculator there is more than one command to execute.
    
    Else: Attached to an If:Then line, tells the calculator to go to the next set of commands if the first boolean set was false.
    
    End: Attach to the End of an If:Then statement, including Else commands.
    
    and: Used with an If statement to check if two or more variables are true at the same time.
    
    or: Used with an If statement to check if at least one variable is true when more than one variable is used with an If command.
    
    xor: Exlusive or; used to test two variables, to check if at least one equals 0, if both either equal or dont equal 0, will return a 0.
    
    not(: Command that checks if a variable equals 0, returns 0 if the value does not equal 0, returns 1 if value does equal 0.
    
    

    <<Previous - Contents - Next>>