TI-Basic Z80 Programming/Conditional Statements

From Wikibooks, open books for an open world
< TI-Basic Z80 Programming
Jump to navigation Jump to search

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[edit]

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:

X=5
F≥3X+2

* Where the first line will return true if the value of X is equal to 5, and where the second line will return true if the value of 3X+2 is greater than or equal to F

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

X>5 and X<9
F=3 or N=3

To type logical operators, press 2ND [TEST] LOGIC. For the following list, consider the format A op B, where A and B are conditions, and op is a logical operator. Where op equals...

  • and, the entire condition returns true only if A and B are both true.
  • or, the entire condition returns true if at least one of A and B is true.
  • xor, the entire condition returns true only if one of A or B is true.

The not operator is special because it does not directly compare two conditions. Instead, it negates the condition(s) that are nested inside. For example:

not(X=5)

* This will return true if X does not equal 5.

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[edit]

If (PRGM 1) requires a criteria argument (condition) to be stated which determines whether the following instruction is to be executed. A standard If 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 If Then End 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[edit]

If condition
statement
  • Where condition is any statement resulting in a zero or non zero result, or a conditional operator returning true or false
    • If condition returns true or non-zero, statement is executed
    • If condition returns false or zero, statement is not executed

Examples[edit]

The following example demonstrates a very basic If statement:

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

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



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 World because it always returns 0.

:If 0
:Disp "Goodbye World"

If Then End[edit]

If Then End (PRGM [1,2,7]) conditional 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 difference that multiple statements are executed instead of one.

Syntax[edit]

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 statements 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

It is also common to manually insert a colon directly after the condition of the If, then type Then. As an example:

:If X=5:Then
:statements
:End

Example[edit]

:Prompt X
:If X≥5
:Then
:Disp "You entered",X
:Disp "X ≥ 5"
:End
X=?5
You entered
               5

X ≥ 5

If Then Else End[edit]

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[edit]

:If condition
:Then
:trueStatements
:Else
:falseStatements
:End
  • Where condition is any statement resulting in a zero or non zero result.
    • If condition returns nonzero, trueStatements (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

Examples[edit]

: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

Else If Workaround[edit]

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

if (condition1) {
    statements1
} else if (condition2) {
    statements2
} else if (condition3) {
    statements3
} else {
    statements4
}

would be notated as, in TI-Basic:

:If condition1
:Then
:statements1
:Else
:If condition2
:Then
:statements2
:Else
:If condition3
:Then
:statements3
:Else
:statements4
:End
:End
:End

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.):

:If condition1
:Then
    :statements1
:Else
    :If condition2
    :Then
        :statements2
    :Else
        :If condition3
        :Then
              :statements3
        :Else
              :statements4
        :End
    :End
:End

You try it![edit]

Try these examples to practice using conditional statements.

Even or Odd[edit]

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 the remainder function (MATH NUM 0). It returns the remainder after diving a dividend by a divisor. The syntax for remainder is:

remainder(dividend,divisor)

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:

fPart(A/B)*B

* Where A is the dividend and B is the divisor.

BMI[edit]

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: . The value of a person's BMI can be categorized by the following table:

BMI Category
< 18.5 Underweight
18.5-24.9 Healthy
25-29.9 Overweight
30+ Obese

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.


Previous: Input
Next: Loops
Table of Contents: TI-Basic Z80 Programming