Lesson 9: Conditionals

Toka provides a few basic comparison functions for handling conditional execution of code.

The comparison functions are: =, <>, <, and >. These correspond to checks for equality, inequality, less than, and greater than. Each consumes two values from the stack and returns a flag of TRUE or FALSE.

To conditionally run code, you need to use ifTrue, ifFalse, or ifTrueFalse. Some examples of using these:

  1 100 = [ 1 . ] ifTrue
  1 100 = [ 2 . ] ifFalse
  1 100 = [ 3 . ] [ 4 . ] ifTrueFalse

Note that each takes a flag, followed by either one (ifTrue and ifFalse) or two (ifTrueFalse) quotes.

In the examples, the first case will invoke the quote if the flag returned by = is TRUE. The second invokes the quote if the flag is FALSE, and the third form invokes the [3 .] quote if TRUE, or the [4 .] quote if FALSE.

changed October 14, 2007