The If Command
IF_ANIMATED.gif

Command Summary

Executes a line or block of code when an expression is nonzero.

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 execute code if and only if an expression is not equal to zero. Advanced uses of the If command allow you to execute a different block of code if the check turns out to be false. The 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 is nonzero. Most expressions you will use with If are called conditional expressions; that is, they return 1 if the condition is true and 0 if it is false. "2+2=4", "A=5", and "pxl-Test(R,C)". Therefore, when the condition is true, the expression evaluates to 1 and the statement is run. When the condition is false, the expression evaluates to 0, and the statement is skipped.

Using Then, Else, and End

When you want more than one line of code to depend on the same condition, use an If:Then block.

:If (condition)
:Then
code to execute if true
:End

An If:Then block also has an optional Else clause, which is used to execute different code when the condition is false.

:If (condition)
:Then
code to execute if true
:Else
code to execute if false
:End

Advanced Uses

If statements can execute and skip other If statements. This leads to odd yet effective constructs like these:

:If A
:If B
//Executes if A is false or B is true
If A:Then
//Executes if A is true
If B:Else
//Executes if A is false or B is false
End

Memory Leaks

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

As far as the TI-BASIC interpreter is concerned, a value of 0 is false, and any other value is true. We can use a numerical expression rather than a conditional one in the condition of the If statement in a case like the following:

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

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

When code in a single-line If statement simply changes a variable, it can often be replaced with an equivalent piecewise expression, which will be smaller and faster.

:If A=B
:C+2→C

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

Code Timings

Single-line If statements are greatly slowed when they are the first line in For( loops without a closing parenthesis. For example,

Very slow
:For(I,1,2000
:If 0:
:End

19 times faster (!)
:For(I,1,2000)
:If 0:
:End

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