TI-Basic Z80 Programming/Conditional Statements

Conditional statements allow a program to take a different path depending on some condition(s). These allow a program to perform a test and then take action based on the result of that test.

Conditions
Conditions are used to control the path of a program. Conditions perform comparisons between different values through the use of relational operators. These include =, ≠, >, ≥, <, ≤. Relational operators always return a true or false result (known as a boolean). To use them, type the values to compare against on either side of the operator:

To type relational operators, press  [TEST]. You may also want to test two different conditions at the same time and join them using a logical operator. These include and, <tt>or</tt>, <tt>xor</tt>, and <tt>not</tt>. The basic syntax is similar to those of relational operators.

To type logical operators, press <tt> [TEST] LOGIC</tt>. For the following list, consider the format, where <tt>A</tt> and <tt>B</tt> are conditions, and <tt>op</tt> is a logical operator. Where <tt>op</tt> equals... The <tt>not</tt> operator is special because it does not directly compare two conditions. Instead, it negates the condition(s) that are nested inside. For example:
 * <tt>and</tt>, the entire condition returns true only if <tt>A</tt> and <tt>B</tt> are both true.
 * <tt>or</tt>, the entire condition returns true if at least one of <tt>A</tt> and <tt>B</tt> is true.
 * <tt>xor</tt>, the entire condition returns true only if one of <tt>A</tt> or <tt>B</tt> is true.

To use conditional statements, they must be placed into a conditional block. Conditional blocks contain the condition to test and the code to execute. The following sections describe the various types of conditional blocks available in TI-Basic.

If
<tt>If</tt> (<tt> CTL 1</tt>) requires a criteria argument (condition) to be stated which determines whether the following instruction is to be executed. '''A standard <tt>If</tt> block can only run one line of code dependent of the condition. For example, if you needed to execute two or more instructions dependent of the condition, you must use an <tt>If Then End</tt> block'''. The argument or arguments is a boolean result, meaning that they will be true or false. If an expression is used as the condition, 0 will represent false and non-0 values will represent true.

Syntax

 * Where <tt>condition</tt> is any statement resulting in a zero or non zero result, or a conditional operator returning true or false
 * If <tt>condition</tt> returns true or non-zero, <tt>statement</tt> is executed
 * If <tt>condition</tt> returns false or zero, <tt>statement</tt> is not executed

Examples
The following example demonstrates a very basic <tt>If</tt> statement:

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

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

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

If Then End
<tt>If</tt> <tt>Then</tt> <tt>End</tt> (<tt> CTL {1,2,7}</tt>) conditional statements are used when more than one statement must be executed if a condition returns true. It is very much like the simple <tt>If</tt> statement, with the difference that multiple statements are executed instead of one.

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

It is also common to manually insert a colon directly after the condition of the <tt>If</tt>, then type <tt>Then</tt>. As an example:
 * Where <tt>condition</tt> is any statement resulting in a true or false result.
 * If <tt>condition</tt> returns true, <tt>statement1, statement2... statementn</tt> (all statements between <tt>Then</tt> and <tt>End</tt>) will execute
 * If <tt>condition</tt> returns false, the statements between <tt>Then</tt> and <tt>End</tt> are not executed, and the program continues with the first statement after <tt>End</tt>

Example
When the program is run, it will be displayed as:

If Then Else End
<tt>If Else</tt>s 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

 * Where <tt>condition</tt> is any statement resulting in a zero or non zero result.
 * If <tt>condition</tt> returns nonzero, <tt>trueStatements</tt> (instructions between <tt>Then</tt> and <tt>Else</tt>) will execute, then the instruction after <tt>End</tt> is executed
 * If <tt>condition</tt> returns zero, <tt>falseStatements</tt> (instructions between <tt>Else</tt> and <tt>End</tt>) are executed, then the instruction after <tt>End</tt> is executed

Examples
Would display

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

Else If Workaround
It should be noted, TI-Basic does not support else if statements. However, to workaround this, nest the conditional blocks in the <tt>Else</tt> portion of the block. For example, in a traditional programming language, the following...

would be notated as, in TI-Basic:

However, this is hard to read, so for this example, we will add spaces to make it more clear (You cannot add extra white space in practice. You will receive syntax errors.):

You try it!
Try these examples to practice using conditional statements.

Even or Odd
Write a program that, when given a number, determines if the number is even or odd and prints the result to the screen.

To help you write this program, you can use <tt>remainder(</tt> (<tt> NUM 0</tt>). It returns the remainder after diving a dividend by a divisor. The syntax for <tt>remainder(</tt> is:

Older versions of the TI-84 Plus and the TI-83s do not have a function for calculating the remainder. However, this is still calculatable via an alternate solution:

BMI
Body mass index (BMI) is a value derived from the mass and height of a person. A person's BMI is calculated with the following formula: $$\frac{\text{weight}_\text{kg}}{\text{height}_\text{m}^2}$$. The value of a person's BMI can be categorized by the following table: Write a program that accepts user input for the person's weight in kilograms and their height in meters, and display their BMI and their category.