IF
Conditional expression that evaluates one of two branches based on a boolean condition.
Syntax
IF condition THEN consequent ELSE alternative
Purpose
IF expressions select between two values based on a condition. Unlike imperative if-statements, L4's IF is an expression that always returns a value.
Examples
Example file:
-- IF keyword examples
-- Basic If-Then-Else
-- Simple conditional
DECIDE `basic if example` IS IF TRUE THEN "yes" ELSE "no"
-- With variable
GIVEN x IS A NUMBER
`check if positive` x MEANS IF x > 0 THEN "positive" ELSE "not positive"
#EVAL `basic if example`
#EVAL `check if positive` 5
#EVAL `check if positive` -3
-- Multi-line Format
GIVEN n IS A NUMBER
`absolute value of` n MEANS
IF n >= 0
THEN n
ELSE 0 MINUS n
#EVAL `absolute value of` 5
#EVAL `absolute value of` -5
-- Nested If-Else (Chained Conditions)
GIVEN score IS A NUMBER
`letter grade for` score MEANS
IF score >= 90 THEN "A"
ELSE IF score >= 80 THEN "B"
ELSE IF score >= 70 THEN "C"
ELSE IF score >= 60 THEN "D"
ELSE "F"
#EVAL `letter grade for` 95
#EVAL `letter grade for` 75
#EVAL `letter grade for` 50
-- Complex Conditions
GIVEN age IS A NUMBER
`has valid ID` IS A BOOLEAN
`is member` IS A BOOLEAN
`can enter venue` age `has valid ID` `is member` MEANS
IF (age >= 21 AND `has valid ID`) OR `is member`
THEN "Welcome"
ELSE "Access denied"
#EVAL `can enter venue` 25 TRUE FALSE
#EVAL `can enter venue` 18 TRUE FALSE
#EVAL `can enter venue` 18 FALSE TRUE
-- DECIDE ... IF Form (Boolean Functions)
-- Shorthand for boolean-returning functions
GIVEN `person age` IS A NUMBER
DECIDE `is adult` IF `person age` >= 18
-- Equivalent long form:
GIVEN `person age 2` IS A NUMBER
DECIDE `is adult 2` IS IF `person age 2` >= 18 THEN TRUE ELSE FALSE
-- Both work the same way (using #CHECK since they use ASSUME-like params)
#CHECK `is adult` WITH `person age` IS 21
#CHECK `is adult 2` WITH `person age 2` IS 15
-- Returning Different Types
-- Returning numbers
GIVEN a IS A NUMBER, b IS A NUMBER
`maximum number` a b MEANS IF a > b THEN a ELSE b
-- Returning lists
GIVEN condition IS A BOOLEAN
`list if true otherwise empty` condition MEANS
IF condition
THEN LIST 1, 2, 3
ELSE EMPTY
#EVAL `maximum number` 10 25
#EVAL `list if true otherwise empty` TRUE
#EVAL `list if true otherwise empty` FALSE
-- If in Complex Expressions
GIVEN n IS A NUMBER
GIVETH A NUMBER
`fibonacci number` n MEANS
IF n <= 1
THEN n
ELSE `fibonacci number` (n MINUS 1) PLUS `fibonacci number` (n MINUS 2)
#EVAL `fibonacci number` 10
Basic If-Then-Else
IF TRUE THEN "yes" ELSE "no"
GIVEN age IS A NUMBER
DECIDE status age IS
IF age >= 18 THEN "adult" ELSE "minor"
Nested Conditions
GIVEN score IS A NUMBER
DECIDE grade score IS
IF score >= 90 THEN "A"
ELSE IF score >= 80 THEN "B"
ELSE IF score >= 70 THEN "C"
ELSE IF score >= 60 THEN "D"
ELSE "F"
Multi-line Format
GIVEN x IS A NUMBER
DECIDE absolute x IS
IF x >= 0
THEN x
ELSE 0 MINUS x
Boolean Expressions in Condition
GIVEN age IS A NUMBER
GIVEN hasLicense IS A BOOLEAN
DECIDE canDrive IS
IF age >= 16 AND hasLicense
THEN TRUE
ELSE FALSE
DECIDE ... IF Form
For boolean-returning functions, use DECIDE ... IF:
GIVEN age IS A NUMBER
DECIDE isEligible IF age >= 18
This is shorthand for:
GIVEN age IS A NUMBER
DECIDE isEligible IS IF age >= 18 THEN TRUE ELSE FALSE
Related Keywords
- AND - Logical conjunction in conditions
- OR - Logical disjunction in conditions
- CONSIDER - Pattern matching alternative
- CONTROL-FLOW - All control flow keywords
Note: THEN and ELSE are part of the IF syntax, not separate keyword pages.
See Also
- Logical Operators - Combining conditions