Last modified on 26 January 2013, at 00:08

TI-Basic Z80 Programming/Criteria Instructions

IfEdit

If, PRGM:CTL:1, requires a criteria argument (condition) to be stated immediately afterwards which will allow the program to determine whether the following instructions are to be executed. The argument or arguments (which will be referred to as a condition) is a boolean result, meaning that they will be true (non-zero, ie 1) or false (zero, ie 0). For more help on booleans, see the Boolean Reference (somewhere not yet).

A simple If is used when the programmer wishes to test for a condition, then execute one instruction (following the if statement) if the condition returns true.

Syntax: IfEdit

:If condition
:statement
  • where condition is any statement resulting in a zero or non zero result
    • if condition returns true (again, non-zero), statement is executed
    • if condition returns false (zero) statement is not executed

Ex: IfEdit

The following example demonstrates a very basic If statement that will display "x > Five":

:6→X
:If X>5
:Disp "X > FIVE!"

It will display "x > Five" because the condition x>5 (6 is greater than 5) returns true. If x had been 3, condition would return false (3 is not greater than 5),and nothing would display.

Ex: If OneEdit

The following program will always display "Hello World" because the condition 1 will always return 1 (which is non-zero) and therefore the condition is always true

:If 1
:Disp "Hello World"

and likewise the following will never display "Goodbye" because it always returns 0

:If 0
:Disp "Goodbye World"

If Then End (If Then)Edit

If PRGM:CTL:1 Then PRGM:CTL:2 End PRGM:CTL:7 conditional statements (more commonly known as If Then statements) are used when more than one statement must be executed if a condition returns true. It is very much like the simple If statement, with the obvious difference that multiple statments are executed instead of one.

Syntax If ThenEdit

The If statement requires a condition, then a Then command must follow on the next line, followed by one or more statements which will execute if the condition returns true. The If block is ended by a End statement which tells the calculator that all following statments are to be executed as normal.

:If condition
:Then
:statement1
:statement2
:statementn
:End
  • Where condition is any statement resulting in a true or false result.
    • if condition returns true, statement1, statement2... statementn (all statements between Then and End) will execute
    • if condition returns false, the statements between Then and End are not executed, and the program continues with the first statement after End

Ex: If ThenEdit

:If x>5
:Then
:Disp x
:x+1→x
:Disp x
:End
6
7

if x = 6, but would display nothing if x was less than five

If Then Else EndEdit

If PRGM:CTL:1 Then PRGM:CTL:2 Else PRGM:CTL:3 End PRGM:CTL:7 If elses are used when the programmer needs to make a choice where if the condition returns non-zero, statements are executed, but if the condition returns zero, other statements are executed. Either way, only one set of instructions are executed

Syntax: Else IfEdit

:If condition
:Then
:trueStatements
:Else
:falseStatments
:End
  • Where condition is any statement resulting in a zero or non zero result.
    • if condition returns nonzero, trueStatments (insructions between Then and Else) will execute, then the instruction after End is executed
    • if condition returns zero, falseStatements (instructions between Else and End) are executed, then the instruction after End is executed

Ex: Else IfEdit

:If X>5
:Then
:Disp "Hello World I'm"
:Disp "Big like five"
:Else
:Disp "Goodbye World"
:Disp "I'm small like four"
:End

Would display

Hello World I'm
Big like Five

if X were greater than or equal to five, but would display the following if X were less than five

Goodbye World
I'm small like four

Syntax: WhileEdit

While loop executes a block of commands between the While and End commands as long as the specified condition is true. The condition is tested at the beginning of the loop (when the End command is encountered), so if the condition is initially false, the block of commands will never get executed.

:While condition is true
:statement
:End
  • where condition is any statement resulting in a zero or non zero result
    • if condition returns true (again, non-zero), statement continues to execute
    • if condition returns false (zero) statement stops execution

Ex: WhileEdit

The following example demonstrates a very basic While Loop that will display x until y is not true:

:While y≠0
:Disp X
:End



Previous: Advanced Variables
Next: GetKey
Table of Contents: TI-Basic Z80 Programming