The If Command


Command Summary

Sets a condition for a line or several lines to be executed.

Command Syntax

If condition

:If condition Then


Menu Location

Starting in the program editor:

  • Press F2 to enter the Control menu.
  • Press 1 to paste If.

Calculator Compatibility

This command works on all calculators.

Token Size

2 bytes for If or If..Then;
2 bytes for Else;
2 bytes for ElseIf..Then;
2 bytes for EndIf.

The If command is the most basic tool in programming. The idea is simple: it checks a condition, and then does something only if the condition is true.

If by itself

With the simplest use of If, the line after it will be skipped if the condition is false.

In the code below, if x really is 4, then the check x=4 is true, so the program will display "x equals 4". If x is not 4, then the check will be false, so Text "x equals 4" will be skipped. Nothing will be displayed.

:If x=4
: Text "x equals 4"


To have multiple lines of code depend on the same condition, use the If condition Then..EndIf syntax. Every line before the EndIf will be skipped if the condition is false.

In the following example, both true→xisfour and Text "x equals 4" depend on the condition x=4.

:If x=4 Then
: true→xisfour
: Text "x equals 4"


Often, you want to do one thing if the condition is true, and another if the condition is false. The way to do this is to insert an Else into the If..EndIf block. Everything between If condition Then and Else is what happens when the condition is true. Everything between Else and EndIf is what happens when the condition is false.

Note: the Else command is also used inside Try..EndTry blocks.

In the following example, "x is 4" will be displayed if x=4, and "x is not 4" otherwise.

:If x=4 Then
: Text "x is 4"
: Text "x is not 4"


Finally, you can use ElseIf condition Then, inside an If..EndIf block, to consider several conditions at the same time. The way this works is: first the basic If condition is checked. If it's true, then the code just after If runs. If that condition was false, but there's an ElseIf, the ElseIf's condition is checked. If it's true, then the code just after the ElseIf runs. If that condition was false too, the program goes on to check the next ElseIf (if there is one), and so forth. You can also include a final Else (optionally) which will only run if no condition is met.

For example:

:If x=4 Then
: Text "x is 4"
:ElseIf x=5 Then
: Text "x is 5"
:ElseIf x=6 Then
: Text "x is 6"
: Text "x is neither 4, 5, nor 6"


What kind of conditions are possible? Any command that returns a logical value — true or false — is acceptable. This includes:

  • Relational operators: =, , >, , <, and
  • Logical operators: and, or, xor, not
  • Any advanced test command: pxlTest(), isPrime(), and others.
  • A variable that contains one of the values true or false, or a function that returns them.

Of course, these can also be combined: for example, isPrime(x) and x≠2 is a valid condition.


Use If without a Then or EndIf for only one command; use Then and EndIf otherwise.

In addition, the when() command can often replace If.

Error Conditions

20 - A test did not resolve to TRUE or FALSE happens when the condition is indeterminate, or the wrong data type.

280 - Else and ElseIf invalid outside If..Then block happens when Else or ElseIf are used outside If..EndIf.

730 - Missing start or end of block syntax happens when the If-Thens and EndIfs don't match up correctly.

740 - Missing Then in the If..EndIf block happens when a Then is missing.

Related Commands
