If / Else
Cairo supports the condition statements
if ... else.
else if statements are currently not supported.
Conditions can be combined with the
In addition to the regular conditional statement one can also use the "Multiplication Trick" to create an inline-if condition statement (conditional ternary operator).
# Importing booleans from Cairos standard library. from starkware.cairo.common.bool import TRUE, FALSE func main(): alloc_locals local result # `if` statement. if 1 == 0: result = FALSE end assert result = TRUE # `if` statement with `and`. if 1 == 0 and 0 == 1: result = FALSE end assert result = TRUE # `if` - `else` statement. if 1 == 1: result = TRUE else: result = FALSE end assert result = TRUE # The condition we want to check let condition = 1 # The "Multiplication Trick". # `result` will be `0` (false) if `condition` is either `1` or `2`. # Note: This trick helps to avoid issues related to Revoked References. let result = (condition - 1) * (condition - 2) assert result = 0 return () end