-
Notifications
You must be signed in to change notification settings - Fork 2
Control Flow
If you already know how to use BASIC control flow you can skip to the next
section on Grammer's control flow perks. If
not, then read up on this section
before moving on. There is a lot of info to retain.
Control Flow is the ability to control the flow of your program. We do this by
using commands such as If
, While
, Repeat
, For
, and Goto
/Lbl
.
If I use If
in a program Then
, if the condition is true then the
following code will be executed until the corresponding End
token is reached.
Code:
If 1
Then
<<<code to execute>>>
End
Note: In Grammer, true
simply means non-zero, while 0
is false
, as in
TI-BASIC.
However, as in TI-BASIC, if we only need to execute one line of code, Then
and
End
can be omitted.
Code:
If 1
<<<code to execute>>>
If I use an If
, Then
, and Else
in a single block then if the condition
is true, the following code will be executed until the Else
token. If the
condition is false the code under Else
will be executed until the
corresponding End
token is reached.
Code:
If 0
Then
<<<code to execute if true>>>
Else
<<<code to execute if false>>>
End
A condition is a line of code to be evaluated. I used 1
and 0
as to represent not just how If
sees the equation but all control flow
commands. However, you can use a variable such as A
or an expression like
A>1
which would return 1
if A
is greater than 1
and 0
if less than
A
.
Unlike TI-BASIC, conditions are evaluated up to a newline
. This means, for
example, that you can include :
and →
in a condition. For example, you can
store 3*N
to K
, and check if it is less than 100:
If 3*K→N:<100
Then
<<do stuff if 3*K is less than 100>>
Else
<<do stuff if 3*K is not less than 100>>
End
While
is expressed in Grammer just like it is in BASIC. The syntax is While <<<condition>>>
. This is a type of loop that executes the code inside of its block while the expression is true. If the expression is not true then the whole block is skipped and the program continues executing after the End
.
Code:
.0:Return
While 1
ClrDraw
Text(<sub>o<sub>"Hi there!
DispGraph
End
Stop
This code will continue to clear the graph screen then display the text "Hi there!" forever, unless you break it by pressing [ON].
Instead of using just numbers we can get more control over the flow of the program if we use pvar's and modify them.
Code:
.0:Return
42→L
While L ;While L is a true statement(not equaling 0) then execute the following code.
If getKey(15
0→L
End
Stop
But that's not all! You can also use commands that return an output.
.0:Return
Pixel-On(0,0
While pixel-Test(0,0 ;Check for pixel
GetInc(B ;Inc B
If B=50 ;If B=50 change the pixel State
Pixel-Change(0,0 ;The pixel logic becomes 0 and the loop ends.
End
Stop
Repeat
is the complete opposite of While
. Instead of repeating until the statement becomes false
it repeats until the statement become true
. This coding block is ideal for checking keys or values to be equal to the desired value or amount.