The If Command

IF_ANIMATED.gif

Command Summary

Checks if a condition is true, and if it is, runs an optional statement or group of statements.

Command Syntax

If condition
statement

If condition
Then
one or more statements
End

If condition
Then
statement(s) to run if condition is true
Else
statement(s) to run otherwise
End

Menu Location

While editing a program, press:

  1. PRGM to enter the PRGM menu
  2. ENTER or 1 to choose If

Calculator Compatibility

TI-83/84/+/SE

Token Size

1 byte

The If command is crucial to most programs. It allows you to check if some condition is true, and then follow up on that by doing something if that check is true. Advanced uses of the If command allow you to have an alternate block of commands to do if the check turns out to be false. The very simplest form of the command is quite easy to understand:

:If condition
:statement

When the calculator gets to that point in your program, it will check to see if the condition (which may be something like 2+2=4, or A=5, or pxl-Test(R,C), or even more complicated checks) is true. If it is, then the calculator runs the statement beneath the If, otherwise, it gets skipped. This explanation may sound complex, but in reality, If commands are so simple that you can just read most code as though it were English and understand it.

Using Then, Else, and End

Often, you want more than one statement to depend on the same condition. While you could use many If commands with that condition, one after the other, that would get long and complicated. Fortunately, TI-Basic has a solution:

:If condition
:Then
:one or more statements
:End

Not only does this solve the problem, but it's also faster: when the condition turns out to be false, using Then increases the speed of skipping the statement(s) greatly. The trade-off, of course, is size: if you only need to work with one command to depend on the condition, then a simple If is smaller.

The next tier of complication is the Else command (note that you can only use it if you're already using Then):

:If condition
:Then
:statements if condition is true
:Else
:statements if condition is false
:End

This provides an alternative path of running the program if the condition turns out to be false.

Advanced Uses

Each time the program enters an If-Then block, the calculator uses 35+(size of the condition) bytes of memory to keep track of this. This memory is given back to you as soon as the program reaches End. This isn't really a problem unless you're low on RAM, or have a lot of nested If-Then statements. However, if you use Goto to jump out of such a statement, you lose those bytes for as long as the program is running — and if you keep doing this, you might easily run out of memory, resulting in ERR:MEMORY.

Optimization

The "condition" that the If statements look for is just a number, because TI-83 series calculators don't really have specific 'true' and 'false' values. As far as the If statement is concerned, a value of 0 is false, and any other value is true. This allows for a lot of optimizations:

:If A≠0
:Disp "A IS NOT 0
can be
:If A
:Disp "A IS NOT 0

While the If command is effective, you can typically replace it with a piecewise expression when the nested statement changes a variable. Piecewise expressions function on the same 'true' or 'false' principle that the If command does, and they are not only usually smaller but more importantly faster, so they are a worthwhile alternative.

:If A=B
:C+2→C
can be
:C+2(A=B→C

Code Timings

The code timings page shows that If statements with no Then and a false condition are susceptible to being greatly slowed down in For( loops that don't close a parenthesis. Specifically:

:For(I,1,2000)
:If 0:
:End

Takes 9 bars and 7 pixels.
:For(I,1,2000
:If 0:
:End

takes 190 bars and 0 pixels — almost 20 times as long!

This is such a significant effect that you should watch out for it even if the condition is mostly true.

Error Conditions

  • ERR:DATA TYPE occurs if the parameter is complex, even if it's complex in a silly way like 0i.
  • ERR:INVALID occurs if this statement is used outside a program.
  • ERR:SYNTAX occurs if an If is the last statement in the program, or the last except for one empty line.

Related Commands

.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License