Difference between revisions of "83Plus:Basic:If"

From WikiTI
Jump to: navigation, search
(Examples: + or choose it from the catalog)
 
Line 16: Line 16:
 
  ''commands''
 
  ''commands''
 
  Else
 
  Else
  ''commands''
+
  ''commands-else''
 
  End
 
  End
  
If the specified condition evaluates to a real number equal to 0, ''command'' is skipped. If the condition evalutes to a real number not equal to 0, ''command'' is executed. ''command'' may be a single command, or it can be the command '''Then''', in which case all commands until a corresponding '''End''' token are part of ''command''. When using '''Else''', one must also use '''Then''' and '''End'''.
+
In the first from, ''command'' is executed if and only ''condition'' evaluates to anything other than 0. In the second form and third form, ''commands'' is also executed if and only if ''condition'' is not zero. In the third form, if ''condition'' is zero, ''commands-else'' is executed.
  
 
== Comments ==
 
== Comments ==

Latest revision as of 12:48, 19 July 2005

Synopsis

Token Size: 1 byte

Syntax:

If condition
command
If condition
Then
commands
End
If condition
Then
commands
Else
commands-else
End

In the first from, command is executed if and only condition evaluates to anything other than 0. In the second form and third form, commands is also executed if and only if condition is not zero. In the third form, if condition is zero, commands-else is executed.

Comments

Repeated use of a Goto statement within an If:Then statement will cause an increase in memory allocation, leading to program slowdowns and eventually a MEMORY error. For example, the following code will eventually cause an error:

0->X ;-> == STO operator
Lbl 0
If 1
Then
X+1->X
Disp X
Goto 0
End

If you must use a Goto within an If:Then statement, it is possible to trick the 83+ into thinking the block ended by jumping to an otherwise unused label that uses an End statement followed by a jump to the proper label. For example, the above program could be rewritten like so:

0->X
Lbl 0
If 1
Then
X+1->X
Disp X
Goto 2
End
Return

Lbl 2
End
Goto 0

This would avoid the problem by causing the 83+ to terminate the If:Then block prior to jumping.

Since TI-BASIC has no equivalent of what real BASIC calls Select Case, multiple Ifs must be used in cases where a variable needs to be checked against multiple values.

Examples

A particular example is a keyloop. Assuming a keycode was stored in K, a sequence of If statements would compare K to the corresponding integer key value.

If K=21
Disp "2ND"
If K=31
Disp "ALPHA"
If K=22
Disp "MODE"
If K=32
Disp "X,T,θ,n" ;θ == Alpha+0. To get the italic n, switch to Seq mode and press the X,T,θ,n key or choose it from the Catalog.