Conditions & Loops

OcaltQL has no ELSE keyword. Fallback logic uses OR. Chained conditions use OR IF. Compound conditions use AND and OR inline between full condition phrases. Loops use WHILE and LOOP.

IF

The condition follows IF. The body is wrapped in OPEN and CLOSE. If the condition is false, execution falls to the OR block if one is present.

Basic IF
IF ?a IS EQUAL TO ?b
OPEN
  EMIT "Equal"
CLOSE
IF with OR Fallback
IF ?a IS GREATER THAN 10
OPEN
  EMIT "Big"
CLOSE
OR
OPEN
  EMIT "Small"
CLOSE
OR IF Chain
IF ?a IS EQUAL TO 1
OPEN
  EMIT "One"
CLOSE
OR IF ?a IS EQUAL TO 2
OPEN
  EMIT "Two"
CLOSE
OR IF ?a IS EQUAL TO 3
OPEN
  EMIT "Three"
CLOSE
OR
OPEN
  EMIT "Other"
CLOSE

Comparison Operators

All comparison operators are multi-word natural language phrases. IS and ARE are interchangeable - ARE is used conventionally with arrays and objects.

Full Operator Reference

Condition
IF ?a IS EQUAL TO ?b
IF ?a IS IDENTICAL TO ?b
IF ?a IS GREATER THAN 10
IF ?a IS LESS THAN 10
IF ?a IS GREATER THAN OR EQUAL TO 10
IF ?a IS LESS THAN OR EQUAL TO 10
IF ?a IS NOT EQUAL TO ?b
IF ?a IS NOT IDENTICAL TO ?b
IF ?text CONTAINS "hello"
IF ?var IS SET
IF ?var IS EMPTY
IF ?var IS NOT EMPTY
IF ?var IS NULL
IF ?var IS NOT NULL
IF ?var IS ARRAY
IF ?var IS NOT ARRAY
IF ?var IS TRUE
IF ?var IS NOT TRUE
IF ?var IS FALSE
IF ?var IS OBJECT
IF ?var IS NOT OBJECT
IF ?var IS NUMBER
IF ?var IS NOT NUMBER
IF ?var IS STRING
IF ?var IS NOT STRING
IF ?var IS BOOLEAN
IF ?var IS NOT BOOLEAN
IF ?text CONTAINS "hello" IS TRUE
IF ?text CONTAINS "hello" IS NOT TRUE

IS ARRAY

IS ARRAY asks what a value is, which is a different question from every operator above it: those ask what a value holds or how it compares. It is the test to make before indexing a value, counting it, or walking it with FOREACH, because a verb that answered a single value where you expected a list will otherwise fail further down, somewhere less obvious than the line that caused it.

An empty array is still an array. IS ARRAY and IS EMPTY are both true of [], and COUNT of it is 0. Ask IS ARRAY about the shape and IS EMPTY about the contents - a value that is not an array at all answers false to IS ARRAY whether it is empty or not.
Checking Shape Before Walking It
IF ?result IS ARRAY
OPEN
  FOREACH ?result SET ?row
  OPEN
    EMIT ?row
  CLOSE
CLOSE
OR EMIT ?result

A value still in flight is not an array yet - a promise answers IS ARRAY for whatever it landed on, and false while it is waiting. A multivariable is not an array either; COLLAPSE is what turns one into an array.

IS OBJECT, IS NUMBER, IS STRING, IS BOOLEAN

These work like IS ARRAY: they ask what kind of value something is, not what it holds. Each one has a NOT form.

Form and query values are always text. !GET('age') is a string even when someone typed 42. Use CAST ?age AS NUMBER first, then IS NUMBER tells you whether it worked.
Handling Whatever PARSE JSON Gave You
PARSE JSON ?raw SET ?data
AFTER IF ?data IS OBJECT
OPEN
  EMIT "One record: " & ?data("name")
CLOSE
OR IF ?data IS ARRAY
OPEN
  COUNT ?data SET ?n
  AFTER EMIT ?n & " records"
CLOSE
OR IF ?data IS NUMBER
OPEN
  EMIT "A number: " & ?data
CLOSE
OR EMIT "Something else"
IS TRUE and IS BOOLEAN Ask Different Things
SET ?flag AS "yes"
AFTER IF ?flag IS TRUE
OPEN
  EMIT "truthy"            (* "yes" is not empty, so it counts as true *)
CLOSE
AFTER IF ?flag IS NOT BOOLEAN
OPEN
  EMIT " but not a boolean" (* it is text *)
CLOSE

CONTAINS

CONTAINS looks inside a value. In text it looks for a piece of text. In an array it looks for an item. In an object it looks through the values. You can write it on its own, or finish it with IS TRUE or IS NOT TRUE if that reads better to you. IF ?a CONTAINS "x" and IF ?a CONTAINS "x" IS TRUE mean the same thing.

Inside the block, !THIS('position') tells you where the match was: the character position in text, counting from 0 the same way SEARCH STRING does, the index in an array, or the key in an object. It is null when nothing was found.

CONTAINS ... IS TRUE with !THIS('position')
STRING "The quick brown fox" SET ?haystack
AFTER IF ?haystack CONTAINS "brown" IS TRUE
OPEN
  EMIT "Found it at position " & !THIS('position')
  (* Output: Found it at position 10 *)
CLOSE
Cutting Text at the Match
STRING "invoice-2026-0042.pdf" SET ?file
AFTER IF ?file CONTAINS "-" IS TRUE
OPEN
  SELECT STRING ?file FROM 0 TO !THIS('position') SET ?kind
  AFTER EMIT ?kind
  (* Output: invoice *)
CLOSE
CONTAINS ... IS NOT TRUE
IF !GET('email') CONTAINS "@" IS NOT TRUE
OPEN
  EMIT "That email address is missing an @."
CLOSE
CONTAINS in an Array
NEW ARRAY ["viewer" AND "editor" AND "admin"] SET ?roles
AFTER IF ?roles CONTAINS "admin"
OPEN
  EMIT "admin is item " & !THIS('position')
  (* Output: admin is item 2 *)
CLOSE
CONTAINS in an Object Gives the Key
NEW OBJECT SET ?team
AFTER SET ?team("lead") AS "Thandi"
AFTER SET ?team("dev") AS "Sipho"
AFTER IF ?team CONTAINS "Sipho"
OPEN
  EMIT "Sipho is the " & !THIS('position')
  (* Output: Sipho is the dev *)
CLOSE
CONTAINS with OR for a Fallback
IF ?subject CONTAINS "urgent" IS TRUE
OPEN
  EMIT "Priority"
CLOSE
OR IF ?subject CONTAINS "invoice" IS TRUE
OPEN
  EMIT "Accounts"
CLOSE
OR EMIT "General"

Compound Conditions

AND and OR connect full condition phrases inline inside an IF or WHILE. These are not chain keywords here - they are logical operators between conditions. Chaining is virtually unlimited.

Context matters. AND and OR between two full condition phrases inside an IF are logical operators. AND and OR between two statements outside a condition are chain keywords. The parser distinguishes by position.
Precedence and short-circuiting. Compound conditions follow the same precedence as PHP's && / || - AND binds tighter than OR. IF ?a IS ... AND ?b IS ... OR ?c IS ... evaluates as (?a IS ... AND ?b IS ...) OR (?c IS ...). Evaluation is left to right and short-circuits - an AND chain stops at the first false condition, an OR chain stops at the first true one; conditions after that point are not evaluated.
AND - Both Must Be True
IF ?a IS GREATER THAN 10 AND ?b IS EQUAL TO ?somevalue
OPEN
  EMIT "Both true"
CLOSE
OR - Either Must Be True
IF ?a IS GREATER THAN 10 OR ?b IS EQUAL TO ?somevalue
OPEN
  EMIT "At least one true"
CLOSE
Mixed AND / OR - Example 1
IF ?a IS IDENTICAL TO "this" AND ?a IS NOT EQUAL TO ?x OR ?b IS GREATER THAN 44
(* Groups as: (?a IS IDENTICAL TO "this" AND ?a IS NOT EQUAL TO ?x) OR ?b IS GREATER THAN 44 *)
OPEN
  EMIT "Complex condition met"
CLOSE
Mixed AND / OR - Example 2
IF ?a IS GREATER THAN OR EQUAL TO 10 AND ?b IS LESS THAN OR EQUAL TO 20
(* The "OR" inside "GREATER THAN OR EQUAL TO" and "LESS THAN OR EQUAL TO" is part of the
   operator phrase itself, not a logical connector. This is a single AND:
   (?a >= 10) AND (?b <= 20) *)
OPEN
  EMIT "In range"
CLOSE
Mixed AND / OR - Example 3
IF ?x IS GREATER THAN 0 AND ?x IS LESS THAN 100 OR ?y IS EQUAL TO "special" AND ?z IS SET OR ?flag IS EMPTY
(* Groups as: (?x > 0 AND ?x < 100) OR (?y IS EQUAL TO "special" AND ?z IS SET) OR (?flag IS EMPTY) *)
OPEN
  EMIT "Very complex condition"
CLOSE

Array & Object Conditions - ARE and SOME

IF ?arr ARE checks that every item in the array or object matches the condition. IF SOME ?arr ARE checks that at least one item matches. Both work with objects as well - checking property values.

ARE - All Items Match
IF ?arr ARE EQUAL TO "target"
OPEN
  EMIT "All match"
CLOSE
SOME - At Least One Matches
IF SOME ?arr ARE EQUAL TO "target"
OPEN
  EMIT "Found at least one"
CLOSE
Compound Array Conditions - Example 1
IF SOME ?arr ARE GREATER THAN 10 AND SOME ?arr ARE LESS THAN 100
OPEN
  EMIT "Has values in range"
CLOSE
Compound Array Conditions - Example 2
IF ?arr ARE EQUAL TO "ok" OR ?obj ARE EMPTY
OPEN
  EMIT "Either all ok or object empty"
CLOSE
Compound Array Conditions - Example 3
IF SOME ?obj ARE EQUAL TO "admin"
OPEN
  EMIT "At least one admin role"
CLOSE
Compound Array Conditions - Example 4
IF SOME ?arr ARE EQUAL TO "error" AND ?status IS EQUAL TO "active" OR SOME ?obj ARE NULL
OPEN
  EMIT "Complex match"
CLOSE
WHILE SOME
WHILE SOME ?arr ARE GREATER THAN 0
OPEN
  EMIT "Still has positives"
CLOSE

!THIS - Runtime Context

!THIS() accesses the current execution context from inside a nest. It is only valid inside an OPEN...CLOSE block.

!THIS('state')
IF ?a IS GREATER THAN 10
OPEN
  EMIT !THIS('state')
  (* Output: true - we are inside the nest because the condition was true *)
CLOSE
!THIS('some') - Matching Keys
IF SOME ?arr ARE EQUAL TO "target"
OPEN
  EMIT !THIS('some')(0)       (* first matching index *)
  AFTER COUNT !THIS('some') SET ?total
  AFTER EMIT ?total            (* how many matched *)
CLOSE
!THIS('some') - Additive with AND SOME
(* Keys from both conditions combined, may have duplicates *)
IF SOME ?arr ARE GREATER THAN 10 AND SOME ?arr ARE LESS THAN 100
OPEN
  EMIT !THIS('some')
CLOSE
!THIS('some') - Additive with OR SOME
(* All keys that matched either condition *)
IF SOME ?arr ARE EQUAL TO "error" OR SOME ?arr ARE EQUAL TO "warn"
OPEN
  EMIT !THIS('some')
CLOSE
!THIS('some') - Empty on No Match
IF SOME ?arr ARE EQUAL TO "zulu"
OPEN
  EMIT "Found"
CLOSE
OR
OPEN
  EMIT !THIS('some')   (* empty array - no matches *)
CLOSE
!THIS('some') with Object
IF SOME ?obj ARE EQUAL TO "admin"
OPEN
  EMIT "Admin keys: " & [JOIN !THIS('some') WITH ", "]
CLOSE

WHILE

WHILE repeats the body for as long as the condition remains true. The condition is evaluated before each iteration. Compound conditions work identically to IF.

Basic WHILE
NUMBER 0 SET ?i
AFTER WHILE ?i IS LESS THAN 5
OPEN
  EMIT ?i
  AFTER CALCULATE ?i + 1 SET ?i
CLOSE
WHILE with Compound Condition
NUMBER 0 SET ?i
AFTER NUMBER 10 SET ?limit
AFTER WHILE ?i IS LESS THAN ?limit AND ?i IS NOT EQUAL TO 3
OPEN
  EMIT ?i
  AFTER CALCULATE ?i + 1 SET ?i
CLOSE

LOOP

LOOP iterates over a numeric range. SET ?i is the loop variable, available inside the body. An optional STEP controls the increment. Negative steps count downward.

Basic LOOP
LOOP 1 TO 5 SET ?i
OPEN
  EMIT ?i
CLOSE
LOOP with STEP
LOOP 0 TO 10 STEP 2 SET ?i
OPEN
  EMIT ?i
CLOSE
Descending LOOP
LOOP 10 TO 1 STEP -1 SET ?i
OPEN
  EMIT ?i
CLOSE

BREAK and CONTINUE

BREAK exits the current loop immediately. CONTINUE skips the remainder of the current iteration and moves to the next. Both are only valid inside a loop body.

BREAK - Exit Early
LOOP 1 TO 10 SET ?i
OPEN
  IF ?i IS EQUAL TO 5
  OPEN
    BREAK
  CLOSE
  AFTER EMIT ?i
CLOSE
(* Output: 1 2 3 4 - stops before 5 *)
CONTINUE - Skip Iteration
LOOP 1 TO 10 SET ?i
OPEN
  IF ?i IS EQUAL TO 5
  OPEN
    CONTINUE
  CLOSE
  AFTER EMIT ?i
CLOSE
(* Output: 1 2 3 4 6 7 8 9 10 - skips 5 *)
There is no ELSE in OcaltQL. Use OR as the fallback block and OR IF for chained conditions. When the script runs, the chain behaves exactly like if / else if / else.