top
About Buy News Guide Ideas
Get a phatIO device here!

Back to Table of Contents

phatIO provides two control structures: if and while. As all lio functions return a value, they can all be used as a test expression - evaluating to true if non 0.

(if <test> <expr if true> <expr if false>)

If test evaluates to true (non zero) it evaluates and returns the second argument. If false it evaluates and returns the second argument.

; prints yes
(keyboard (if (eq 1 1)  "yes" "no"))


; prints 103 (+ 1 (+ 100 (+ 1 1)))
(keyboard (+ 1 (if (eq 0 1)
                        (- 10 3)
                        (+ 100 (+ 1 1 )))
            ))

Multiple expressions can be executed in the true and false expressions by wrapping in parentheses:

; prints a c d d 2 (keyboard returns the last string printed - d)
(defvar i 0)
(keyboard "a "
    (if i
        ((+= i 10)  ; true expressions
         (keyboard "b "))
        ((= i 2)    ; false expressions
         (keyboard "c " "d "))
    )
    i
)

(while <test> <code>...)

While repeatedly evaluates the test expression as an integer and if it returns true (non zero) evaluates the rest of the code arguments

; prints a b c d e f g h i j 
(defvar i 0)
(while (< i 10)
    (keyboard (fmt "%c" (+ 'a' i)) " ")
    (+= i 1)
)

(eq <a> <b>)

Returns 1 if a equals b, 0 otherwise. Currently a and b are evaluated as integers.

(eq 232 2); returns 0
(eq 2 (+ 1 1)); returns 1
(eq 22 "22"); returns 1 (string is evaluated to integer 22)
(eq "abc" "xyz"); returns 1 (strings are evaluated to integers - both 0)
(eq "332" "332"); returns 1 (strings are evaluated to integers - 332)

(< <a> <b>)

a and b are evaluated as integers and returns 1 if a is less than b, 0 otherwise.

(> <a> <b>)

a and b are evaluated as integers and returns 1 if a is greater than b, 0 otherwise.

(and <a> <b> ...)

Returns 1 if all arguments evaluate to non zero, 0 otherwise

The following types “1 3” (all arguments are evaluated)

(defvar i 0)
(keyboard (and (+ i 1) (+= i 1) (+= i 2) (eq i 4)) " " i)

(or <a> <b> . . .)

Returns 1 after the first argument evaluates to zero, 0 if none do

The following types “1 0” (on the first (+ i 1) is evaluated)

(defvar i 0)
(keyboard (or (+ i 1) (+= i 1) (+= i 2) (eq i 4)) " " i)

(not <a>)

Returns 0 if the argument evaluates to non zero, 1 otherwise.

@Contact  |   Legal & Privacy  |    RSS  |   Development Twitter  |   Official Twitter
© phatIO 2012, All Rights Reserved.