Search code examples
rulesrule-engineclips

break statement not working in function in clips


the break statement is not working in my code.

error : The break function not valid in this context.

(deffunction slabFunction (?q0 ?q1 ?q2)
(if(and(>= ?q0 36)(>= ?q1 36)(>= ?q2 36)) then
(printout t "slab 3" crlf)
(break))
(if(and(>= ?q0 24)(>= ?q1 24)(>= ?q2 24))
then 
(printout t "slab 2" crlf))
(if (and(>= ?q0 12)(>= ?q1 12)(>= ?q2 12))
then 
(printout t "slab 1" crlf)
(break))
)

Please help! If the slab 3 gets applied the other condition shouldnt get applied. If it doesnt, then slab 2 check is done, if statisfied it applies. then no further check is done. so on..


Solution

  • You use a break statement to terminate a loop (or in some languages a switch statement). If you want the function to terminate after a condition has been satisfied, use a return statement.

             CLIPS (6.31 6/12/19)
    CLIPS> 
    (deffunction slabFunction (?q0 ?q1 ?q2)
       (if (and (>= ?q0 36) (>= ?q1 36) (>= ?q2 36))
          then
          (printout t "slab 3" crlf)
          (return))
       (if (and (>= ?q0 24) (>= ?q1 24) (>= ?q2 24))
          then 
          (printout t "slab 2" crlf)
          (return))
       (if (and (>= ?q0 12) (>= ?q1 12) (>= ?q2 12))
          then 
          (printout t "slab 1" crlf)
          (return)))
    CLIPS> (slabFunction  40 40 40)
    slab 3
    CLIPS> (slabFunction 26 26 45)
    slab 2
    CLIPS> (slabFunction 56 13 33)
    slab 1
    CLIPS> (slabFunction 10 3 2)
    FALSE
    CLIPS>