Jump to content

TI-Basic Z80 Programming/List of Commands/If

From Wikibooks, open books for an open world

The If statement is a simple instruction that directs the program to perform certain functions if the stated criteria is met.

Input

[edit | edit source]

If, PRGM:CTL:1, requires a criteria argument to be stated immediately afterwards which will allow the program to determine whether the following instructions are to be executed. The argument or arguments are boolean results, meaning that they will be true (resulting in a value of 1) or false (resulting in a value of 0). For example, 8>3 is true, therefore a value of 1, while 1>5 is false, therefore a value of 0. The program will only execute the instructions following the If statement if the value of the argument is anything other than 0.

Note: an argument of simply "3" is untestable since there is no criteria to compare it to (ie. >1), therefore the resulting value will remain 3 and the program will still execute the following instructions. This may be useful for when used as tricks and for optimizations.

The criteria following the If statement can be written in several forms:

[...]
:If 1
:Disp "Hello World!"
[...]

This will simply display "Hello World!" since the criteria's value is 1.


[...]
:If 0
:Disp "Hello World!"
[...]

This will skip the instructions to display "Hello World!" since the criteria's value is 0.


[...]
:Prompt X
:If X>3
:Disp "Hello World!"
[...]

The user enters a number via the Prompt instruction and will only display the text if X (the user's input) is greater than 3.


When using If to regulate the execution of more than one instruction, Then is needed. (See the next section.) Without the Then, only the instruction immediately following the If statement will be regulated while the rest will be executed indifferent to the value of the criteria.

For example:

[...]
::If X>3
:Disp "Hello World!"
:Disp "My Name is Seth"
[...]

will display "Hello World!" followed by "My name is Seth" if X (the user's input) is greater than 3, and will display only "My name is Seth" if X is less than 3. This is because :Disp "My Name is Seth" is executed regardless what the value of X is.