Quote:
| Originally Posted by YottaFlop I tried to make a simple program in order to test gravity on a simple point. Here's the code.
Code:
ClrHome
ClrDraw
Horizontal -10
0->X
5->Y
Lbl 1
Pt-Off(X,Y,2)
Pt-On(X,Y,2)
If Y>-9
Then
Y-.5->Y
Goto 1
The idea is to have the point move down until the specified area, which it does, and delete all the previous points, which it fails to do. What's the problem? Thanks. |
I looked at your program, and found it was because you have the point on and off backwards. Also, you should delete the Then statement, because it looks like if you let it go long enough, then you're going to fill up it's memory.
With If statements, you don't have to use Then if there's only one line you want it to do.
Also, If you use Then, then you have to use a End statement to signify, "Here's where I want it to stop doing things only if it meets the statement."
This is kind of where knowing other programming languages is helpful, but by far not necessary.
Here's an example from Java.
Code:
...some coding...
If(X>10)
{
X=X-A; ------Ignore this line, as this is basically store on TI BASIC, except backwards.
A=2A; ------This one too.
}
...and the rest of the program...
You see the opening and closing brackets?
The opening one says, "Do everything below here if the if statement is true" and is like Then on the calc. The closed one below it says, "OK, now whether or not that statement was true, continue the program below here as normal." and is like the End statement on the calc.
The reason why you would run out of memory if you let this code run long enough is because you have a goto statement before the (nonexistent) End statement. When it does a Then statement, the calculator tells itself, "ok, I got one more End statement to look for from here on out", but when there aren't anymore End statements, the calculator has too many to handle in it's RAM, so it overloads, and stops the program.
Avoid using goto statement's in between a Then and End statement, especially if it's in a part where the program will be going to a lot.
On the other hand, though it will only run through this program 20 times, so you don't really have to worry about it, but please don't make this sort of thing a habit. One minute you think you're program is doing great, next thing you know, have to re-write the entire thing to keep that from happening.
Ok, sorry for the lecture, but there's a lot that needs be said. If you want a program to loop through itself quickly, there's better ways than Lbl and Goto, as these will (generally speaking) slow down large programs significantly. There are better and faster ways to do it. The calculator has 3 (built in) types of loops you can use: For(, While, and Repeat. I say built in because you can also execute a program inside itself (called recursion) but that generally is not recommended.
For( is a loop that counts as it goes, with it's syntax (or parameters) as follows, with the thing in the brackets optional:
Code:
:For( Counting Variable, Starting Value, Ending Value [, Step Value])
:End
Like I said, it's a counting loop, so it needs 3 things. Some variable to use to count, a place to start, and a place to end. It also needs a value to count by, but if you leave that off, it will automatically count by ones. If you choose to try and better yourself in programming, then you will use For( all the time. I'm pretty good myself, and I think I use it more often than any other type of loop. If you wanted to display every number from 1 to 100, then you would probably use a for loop, like so:
Code:
For(A,1,100
Disp A
End
Look at it and you will be able to tell that it uses the Variable A, stores it as 1, then counts up by one, because I didn't specify to count by any other number, and keep counting up until 100, then it finishes.
If you wanted to do every even number, then this would be what you would want to do:
Code:
For(A,0,100,2
Disp A
End
This time, because I told it specifically to count by 2, it will display 0, then add 2, then display 2, and so on and so forth.
And the same goes for any other value you entered into the fourth parameter.
And that's about all you need to know for the For( loop.
While is simpler. What it basically does is go through the loop, WHILE the statement you tell it is true.
This is often used for the engine of a program. Keep letting the player control the character, WHILE he's alive.
Keep the player working on the puzzle WHILE he hasn't beaten it.
(From personal experience,) Have the player keep working on the Sudoku puzzle while he doesn't have 81 correct numbers.
And so on.
Usually one would do this with a statement like X>0, or A=X, and so on, but you can change it if you want into an infinite loop.
Notice that if you enter in the homescreen X=X, it displays 1. And if you display X=X-1, it displays 0. This is because the way the calculator displays true or false is 1 or 0, respectively. So essentially if you told the program While true, then it would infinitely loop through it right?
While 1 yields an infinite loop, as 1 never stops being 1, and thus the loop is never false, and always will loop.
Same works for If statements, but if you tell it If 1, then you may as well not have the If statement in the first place.
And that's about all there is to a While statement, except that it's good to know that it checks to see if it's true or false at the beginning of the loop. So you can replace this:
Code:
...some code...
If X=1
Then
While X=1
...some coding here...
End
End
...rest of code...
with this:
Code:
...some code...
While X=1
...some coding here...
End
...rest of code...
This gets to be pretty helpful, but sometimes you will need it to loop through at least once. That's where the Repeat command comes in (please bear with me, this is the last part of this lecture of epic proportions, even for my own standards, and now that you know of the other two loops, this will be easy)
Repeat, is basically the opposite of While.
Repeat will, simply put, Repeat the inside of it, UNTIL it's true.
This also is used many times for engines, but is also used for getKey, which if you don't already know about, then tough luck, my hands are getting tired.
Also another big difference between While and Repeat is that Repeat checks at the end of the loop, so it will always go through the loop at least once. So if you were making a card game and needed all the cards to be different, then you would tell it to randomly choose a card, and if that card were to equal the value of any other card, then choose another card.
And that's pretty much all I can think of for Repeat.
Like I said before, technically you can execute the program inside itself so it repeats itself, but you can also run out of memory if you do that, and such a method of looping (called recursion) is often fragile, and could "break down" catastrophically if done incorrectly (Error after Error, shouldn't sound like much fun if you're a programmer).
And that's about it.
Use those loops wisely and be sure you're using the right one for the situation. Never forget you're End commands, because those should be seen as often as For('s, While's, and Repeat's combined.
And if you have any other questions, I'll try and give them shorter explanations.
Oh, and for the program, I can post my own bouncing programs that use actual physics equations to accurately replicate reality (the ball accelerates to the ground instead of moving at a constant speed, and loses inertia as it impacts the ground)
They're pretty cool to watch, especially when bored in school, and work pretty well, but one of the is meant for the 84+, and will go very slow on the 83...