Introduction: Calculator TI 83+/83+SE/84+/84+SE Tutorial Lesson 2: Codeflow

If you haven't visit lesson 1 so far, then do it.
If you have, then continue reading! :)
So, in this lesson I'll teach you how to do simple codeflow controls.
Please keep in mind that later on you rarely use them but it is still VERY important to know first how this is working.
So, have fun with the tutorial!
Be sure to comment, rate and subscribe!


Be also sure to check out my Website!

If you have any questions about programming be sure to check out Omnimaga!

Step 1: Simple If-conditions

*YAY* finally there are if-conditions! You use if-conditions everywhere, also in everyday life, for example, "If it is sunny today I'll go swimming.". If-conditions are in fact as easy as that. You just say stuff like this:
:Prompt A
:If A=5
:Then
:Disp "A IS 5"
:End

Try to guess first by just looking at the code that that program does.

For a screenie of it in action:
So if, and only if, A is equal to five it will execute that line of code, that Disp command. It executes everything between the Then and the End, no matter what you will add.
Let's say we want to add now a else part, so like "If it is sunny today I'll go swimming else I'll learn programming.".
That is also pretty straight-forward. Here's our example extended:
:Prompt A
:If A=5
:Then
:Disp "A IS 5"
:Else
:Disp "A IS NOT 5"
:End

Now, if A is equal to 5 it will execute everything between Then and Else, so in this case it will display "A IS 5".
If A is now NOT equal to 5 it will execute everything between the Else and the End, in this case it will display "A IS NOT 5".

Do you get that? It's in fact very simple, isn't it? :)
So, it's time for a little task!
Make a program that inputs two variables, adds them and if the sum is equal to 99 it displays "IS 99" and else it displays "IS NOT 99"
So, let's start programming!

For my sollution:
Notice that in this case I used the Ans-variable to save a variable.
If you understood this, then let's move on to the next step!

Step 2: Math Operatores

So far you could just do if-conditions if something was equal to a certain number, but what if you want a if-condition where for example something is not a number?
"If it will not be sunny i will learn programming"
:Prompt A
:If A≠5
:Then
:Disp "A IS NOT 5"
:End
If you enter 5 it will display nothing, else it will display "A IS NOT 5"

Now, you want to have something greater than a number.
"If it is hotter than 20°C I'll go swimming."
:Prompt A
:If A>5
:Then
:Disp "A IS MORE THAN 5"
:End
Now, if A is greater than 5 it'll display the message, note that if A is exactly 5 it won't, as exactly a number is not greater than it.

It's exactly the same with less than.
"If it is colder than 20°C I'll learn programming."
:Prompt A
:If A<5
:Then
:Disp "A IS LESS THAN 5"
:End
Again, it will not be displayed when A is exactly 5.

Now, we want a variable to be larger or equal to a number.
"If it is hotter or as hot than 20°C I'll go swimming."
:Prompt A
:If A≥5
:Then
:Disp "A IS ≥ 5"
:End
Note that in this case it will also display the message if A is 5.

Again, exactly the same just with less or equal than.
"If it is colder or as cold than 20°C I'll learn programming."
:Prompt A
:If A≤5
:Then
:Disp "A IS ≤ 5"
:End
Note again, that if A is 5 the message will be displayed.

Now, that you understood all the math operators, we can go on to the next step!

Step 3: Bool Operators

First of all, what are bool operators?
Bool operators are operators for comparison.
Now you probably say: wait a sec, didn't I just learn how to compare a variable with a number?
Well, that's true, but with bool operators you can do even more, like this:
"If it is sunny today and it is hotter than 20°C I'll go swimming."
The big difference is that here is a and in there.
:Prompt A,B
:If A=5 and B=5
:Then
:Disp "A AND B ARE 5"
:End
Now only, and if only, A and B are equal to five the message is displayed.

Now let's look at or:
"If it is sunny or it is hotter than 20°C I'll go swimming"
:Prompt A,B
:If A=5 or B=5
:Then
:Disp "A OR B ARE 5"
:End
Now if A or B are 5 the message is displayed. Note that it is also displayed if A and B are 5, as at least one of these conditions is still true.

Now, there is also exclusive or (xor).
:Prompt A,B
:If A=5 xor B=5
:Then
:Disp "A XOR B ARE 5"
:End
Now, it's the same as with or, but if A and B are 5, so both conditions are true, the message will not be displayed.

Now, that you understand these operators, let's take a little look closer look at if conditions.
It executes the space between the then and the end/else if the if-condition is true.
So we have now some massive if-condition here:
:If ((A=B and (B>5 or B<1)) xor (C=4 and (D≤A or A>25)) and G=25
Wow, that is some looooooooong if-condition! :D
So, let's take it apart.
The first innermost parenthesis is this: B>5 or B<1. Now, if B is greater than 5 the left part is true, so the hole part would be already true as it is a or condition.
Now we go outwards:
A=B and Part1
so, if Part one, the part we already looked at is true, and A=B is true, then that part is true either.

I hope you get what i mean, that the calculator just looks what is true, and it can be also very important to set parenthesis. Just take a look at these two if-conditions:
:If (A=5 or B=6) and C=7
:If A=5 or (B=6 and C=7)
The topper condition could be true if B was not six, but the lower condition couldn't. So pay always attention on how you set your parenthesis.

Now, we could just compare true stuff, there is also a command that allows us to compare false stuff, the not( command. All it does is if something is true it outputs false and if something is false it outputs true.
:If A=5 and not(B=5)
You can actually read it along what it does. So, if B is 5, then inside the not is true, but as not turns around the right part of the and is false so the hole if-condition is false. But if B is something else than 5 that inside the not is false, and as the not turns around the right side is now true, so if A is now 5 either the condition would be true.
Hope you understood this step, so let's go on! :D

Step 4: Shortening the If-conditions

Now, there is also a way to shorten syntax for an if-condition. In fact, it is, that if you let out the Then it will only execute the next line if the condition is true, so you shouldn't use a Else or a End either.
Let's get a code example:
:Prompt A
:If A=5
:Disp "A IS 5"
:Disp "END OF PRGM"
As you can see, here isn't a Then or a End, so just the next line is executed if the condition is true. As the line after that is two lines away from the if-condition it will always be executed.
It is very important for simple code-flow manipulation to get this difference, why will be discussed in the next step.

Step 5: Labels and Goto

Labels and Goto are in the beginning of programming very important, and later on if you are to lazy to think to much to ^^.
So, what are labels? A label is a marked place in the program. In TI-Basic a name of a label consists out of one to two characters, e.g. "F" or "GK" or "F4". These labels can be called, so that it jumps to the label, no matter if it is in front of it or behind it. For that there is the Goto command. You set a label with the Lbl command. Here's a little example:
:Lbl A
:Prompt X
:Goto A
Please note that to exit this program you have to hit the [ON]-key as it never reaches the end.
Now, let's take a look at it. At the beginning is Lbl A. That line does nothing at the moment. Now, there is Prompt X. It asks for input, which we don't use at the moment :D. Then there is Goto A. This jumps to the Label A (Lbl A). Hey, wait a sec, now it asks again for X as it is the next line and then jumps up again! Yes, that's a infinity loop.
Now, please note that Labels and variables are not the same, so you can have a label called A and also the variable A.
Example:
:0→A
:Lbl A
:Disp A
:A+1→A
:Goto A
All this program does is counting up until you hit on (force a error).
After understanding Labels and Goto we can go further deep into thinking, so let's move on to the next step!

Step 6: Making Some Loops

So far we just had infinity loops that just loop for ever, but that isn't very user-friendly as you could just exit it by forcing an error. Let's take a look at this:
:Lbl A
:Prompt A
:If A=5
:Goto B
:Goto A
:Lbl B
Let's take a closer look at it. First, there is a simple infinity as we had before:
:Lbl A
:Prompt A
:
:
:Goto A
But in the two empty lines here there is more happening:
:If A=5
:Goto B
Now, if you input 5, then the if-condition is true and the next line is executed which jumps to the Label B (Lbl B), which is after Goto A, so it never reaches the line Goto A if you enter 5. But if you don't enter now 5, the if condition is false, the next line is jumped, and you get to Goto A which gets you back to the beginning of the program.
It's important to get this, so here's a little programming task!
Make a program that asks the user for two variables in an infinity loop, if they are both same and greater than 5 it will exit the infinity loop.

For possible sollution: Your solution may look somewhat different, but!
IMPORTANT: When jumping away with an if-condition we had, never EVER use a Then and a End, as that will cause after some time memory errors.
That is because you jump out of the if-condition and so the calculator will be keep searching for the end that isn't there, taking up more and more memory. If you have to do some stuff before you jump then do it like this:
:If <condition>
:Then
:<stuff>
:End
:If <condition>
:Goto A
Ok, after you understood that let's move on to the next step!

Step 7: The Menu( Command

Now, let's start with doing menus! First it sounds rather complicated, but in fact it is fairly easy! TI-Basic has a command for that, the Menu( command.
It makes TI-like menus, so you have a top caption and up to 7 options, that menu doesn't support scrolling or tabs.
To use it you first define your header, then you define each option, you just use as many as you want. After the text of each option you put in the label name where it will goto if you click the menu point, so here's an example!
:Lbl ST
:Menu("WELCOME","OPTION 1",1,"OPTION 2",2,"OPTION 3",3,"EXIT",EX)
:Lbl 1
:Disp "OPTION 1"
:Pause
:Goto ST
:Lbl 2
:Disp "OPTION 2"
:Pause
:Goto ST
:Lbl 3
:Disp "OPTION 3"
:Pause
:Goto ST
:Lbl EX
Here's a pic of it in action:

You could also optional add more menu points, as you see, after the menu points is always the label where it shall jump to when selected.
Let's go on to the next step!

Step 8: A Programming Task

Now, here's a little programming task:
Make a program that can check if a number is greater than 5 and smaller than 100, it can check if a number is even (fPart() is to get the fraction part, iPart() is to get the integer part. Well, that's it! Let's start programming!


For my solution:

Step 9: The End

Whew, you finally made it!
You worked yourself through another TI-Basic tutorial.
So, here is another contest for you!
Make a program that can calculate radian into degree and vice versa.
Email me your program or your code within one week.
So, be sure to comment, rate and SUBSCRIBE!
Be also sure to check out my Website!

Comments

About This Instructable

1,004views

6favorites

License:

Bio: You can find me over on Knexflux! https://knexflux.net
More by Sorunome:Knex Ball Machine IcarusHow to add a speaker to your TI-84+Knex Ball Machine Dysphoria
Add instructable to: