Loops

Jess like other programming languages ​​allows to use the loop. You can use the following loops:

  • Foreach loop, loop "in the collection" involves the repetition of iterations for all elements of the selected data, such as array or list. This loop is useful in a situation where we have a list of variables. It has the form:

    (bind ?lista (create$ imie nazwisko adres))
    (foreach ?e ?lista (printout t ?e crlf))

    Example writes on the screen all the elements of the list.

  • The while loop executes as long as its condition is true (has the value true). Instruction checks the condition before the loop body. The while loop can execute an infinite number of times, the expression will never be 0 (false), you may not ever do when the value before the first course will be FALSE.

    (bind ?i 3)
    (while (> ?i 0)

    ( printout t ?i crlf)
    (-- ?i))

    Example writes the number 3 2 1 and FALSE.

  • The if statement is the basic conditional statement. When condition is met - it will execute the code contained the word then. Fragment else is optional. It has the form:

    (bind ?x 75)
    (if (> ?x 100) then
    (printout t "X is big" crlf)
    else (if (> ?x 50) then
    (printout t "X is average" crlf)
    else
    (printout t "X is small" crlf)))

    As you can see the variable x is set to 75. After following the instructions on the screen will appear information that x is average.