|
We're glad you came by, but you might find what you're looking for elsewhere. TI-Basic Developer is not the site it once was. While its information on commands and other calculator features remains almost second-to-none, its forum, archives, and even hosting service, Wikidot, have been decaying for years. The calculator community would love to see what you're working on, or help you in your next coding adventure, but TI-Basic Developer is no longer the place to do it. Instead, you should head over to Cemetech (primarily American) or TI-Planet (primarily international). Both are active, well-established forums with their own archives, chatrooms, reference material, and abundant coding tools and resources. We'll see you there, we hope. |
Chooses between two values based on a condition.
when(condition,if-true,if-false,[if-undef]])
Menu Location
Starting in the program editor:
- Press F2 to enter the Control menu.
- Press 3 to select when(.
This command works on all calculators.
2 bytes
The when() command — a sort of one-line alternative to If — chooses one of (usually) two values based on a condition. The first argument is the condition to check; if the condition is true, when() evaluates the second argument, and if the condition is false, it evaluates the third.
:when(2+2=4,"Math works!","Math doesn't work!")
"Math works!"Only the chosen alternative is even calculated. If the condition is true, for example, when() will save time by not even bothering to check what the false condition even is.
Unlike If, when() doesn't have a problem with conditions that can't be fully resolved: it will just stay in its unevaluated form. And it gets better…
Advanced Uses
You can give when() a third alternative, that will be taken if the condition isn't certain to be true or false. This usually happens because there's an undefined variable in the condition. For instance:
:when(x>0,"x is positive","x is negative or 0","x is undefined")This can also happen if you give it a strange data type: for instance, if the "condition" of when() is an integer, the third alternative will be taken, because it's neither true nor false.
Optimization
It's usually better to use when() instead of If, at least for short calculations. The result will be more compact, and as the Code Timings page shows, it's marginally faster, as well. However, for complicated conditions, when() is hard to read, especially if there are several when()'s in the same expression.
TI-83 series programmers might be tempted to use when() to convert true and false values to the more familiar 0 and 1:
:when(cond,1,0)This has some applications, but in general you should avoid this: using when() directly is almost always better.
