The Set Command is used to change the value of an existing variable in the current scope's memory (Read about scope in Functions). The Set command can only modify variables that have already been created with the New command. The new value must match the variable's original data type, which cannot be changed after creation.
The short syntax structure uses the word SET:
SET VARIABLE = VALUEThe long syntax structure uses the word SET:
SET VARIABLE EQUAL TO VALUEThe Set command replaces the current value of a variable with a new value.
NEW INT counter = 0
PRINTLN counter // Displays "0"
SET counter = 5
PRINTLN counter // Displays "5"
SET counter = 10
PRINTLN counter // Displays "10"NEW FLOAT temperature = 72.5
PRINTLN temperature // Displays "72.5"
SET temperature = 75.0
PRINTLN temperature // Displays "75.0"NEW STR status = "pending"
PRINTLN status // Displays "pending"
SET status = "active"
PRINTLN status // Displays "active"
SET status = "complete"
PRINTLN status // Displays "complete"NEW BOOL isActive = FALSE
PRINTLN isActive // Displays "FALSE"
SET isActive = TRUE
PRINTLN isActive // Displays "TRUE"Like the New command, Set can assign Literals, Variables, or Expressions.
NEW INT x = 0
SET x = 42 // Integer literal
PRINTLN x // Displays "42"
NEW STR name = ""
SET name = "Alice" // String literal
PRINTLN name // Displays "Alice"NEW INT source = 100
NEW INT destination = 0
SET destination = source
PRINTLN destination // Displays "100"
NEW STR original = "Hello"
NEW STR copy = ""
SET copy = original
PRINTLN copy // Displays "Hello"NEW INT x = 10
SET x = x + 5 // Arithmetic expression
PRINTLN x // Displays "15"
NEW BOOL result = TRUE
SET result = TRUE && FALSE // Logical expression
PRINTLN result // Displays "FALSE"
NEW STR message = "Score: "
SET message = message .. "100" // Concatenate expression
PRINTLN message // Displays "Score: 100"A powerful feature of Set is the ability to use a variable's current value to calculate its new value.
NEW INT counter = 0
SET counter = counter + 1
PRINTLN counter // Displays "1"
SET counter = counter + 1
PRINTLN counter // Displays "2"
SET counter = counter + 1
PRINTLN counter // Displays "3"NEW INT lives = 3
SET lives = lives - 1
PRINTLN lives // Displays "2"
SET lives = lives - 1
PRINTLN lives // Displays "1"NEW INT value = 5
SET value = value * 2
PRINTLN value // Displays "10"
SET value = value * 3
PRINTLN value // Displays "30"NEW FLOAT total = 0.0
SET total = total + 10.5
SET total = total + 20.3
SET total = total + 15.7
PRINTLN total // Displays "46.5"NEW STR output = "Result: "
SET output = output .. "Step 1, "
SET output = output .. "Step 2, "
SET output = output .. "Complete"
PRINTLN output // Displays "Result: Step 1, Step 2, Complete"The Set command is commonly used inside If statements to update variables based on conditions.
NEW INT score = 75
NEW STR grade = ""
IF score >= 90
SET grade = "A"
ELIF score >= 80
SET grade = "B"
ELIF score >= 70
SET grade = "C"
ELSE
SET grade = "F"
END IF
PRINTLN grade // Displays "C"NEW BOOL isOn = FALSE
PRINTLN "Status: " .. isOn // Displays "Status: FALSE"
IF isOn == FALSE
SET isOn = TRUE
ELSE
SET isOn = FALSE
END IF
PRINTLN "Status: " .. isOn // Displays "Status: TRUE"NEW INT points = 100
NEW BOOL hasBonus = TRUE
IF hasBonus == TRUE
SET points = points + 50
END IF
PRINTLN "Total points: " .. points // Displays "Total points: 150"NEW FLOAT temperature = 105.0
NEW STR alert = "Normal"
IF temperature > 100.0
SET alert = "High Temperature Warning"
ELIF temperature < 32.0
SET alert = "Low Temperature Warning"
END IF
PRINTLN alert // Displays "High Temperature Warning"The Set command is essential for updating loop counters and accumulating values in Loops.
NEW INT i = 1
WHILE i <= 5
PRINTLN "Iteration: " .. i
SET i = i + 1
END WHILE
// Displays: Iteration: 1, Iteration: 2, Iteration: 3, Iteration: 4, Iteration: 5NEW INT countdown = 5
WHILE countdown > 0
PRINTLN "T-minus " .. countdown
SET countdown = countdown - 1
END WHILE
PRINTLN "Liftoff!"NEW INT sum = 0
NEW INT i = 1
WHILE i <= 10
SET sum = sum + i
SET i = i + 1
END WHILE
PRINTLN "Sum: " .. sum // Displays "Sum: 55"NEW STR result = ""
NEW INT counter = 1
WHILE counter <= 3
SET result = result .. counter .. " "
SET counter = counter + 1
END WHILE
PRINTLN result // Displays "1 2 3 "NEW INT current = 1
NEW INT previous = 0
NEW INT temp = 0
NEW INT i = 1
WHILE i <= 5
PRINTLN current
SET temp = current
SET current = current + previous
SET previous = temp
SET i = i + 1
END WHILE
// Displays Fibonacci sequence: 1, 1, 2, 3, 5NEW INT playerScore = 0
PRINTLN "Score: " .. playerScore
// Player collects items
SET playerScore = playerScore + 10
PRINTLN "Score: " .. playerScore // Displays "Score: 10"
SET playerScore = playerScore + 25
PRINTLN "Score: " .. playerScore // Displays "Score: 35"
SET playerScore = playerScore + 50
PRINTLN "Score: " .. playerScore // Displays "Score: 85"NEW FLOAT celsius = 25.0
NEW FLOAT fahrenheit = 0.0
SET fahrenheit = ( celsius * 9.0 / 5.0 ) + 32.0
PRINTLN celsius .. "C = " .. fahrenheit .. "F"
// Displays "25.0C = 77.0F"NEW INT stock = 100
PRINTLN "Initial stock: " .. stock
// Customer purchases
SET stock = stock - 15
PRINTLN "After sale: " .. stock // Displays "After sale: 85"
// Restock
SET stock = stock + 50
PRINTLN "After restock: " .. stock // Displays "After restock: 135"NEW STR state = "idle"
NEW STR action = "start"
PRINTLN "Current state: " .. state
IF action == "start"
SET state = "running"
ELIF action == "stop"
SET state = "stopped"
END IF
PRINTLN "New state: " .. state // Displays "New state: running"NEW INT health = 100
NEW INT maxHealth = 100
PRINTLN "Health: " .. health
// Take damage
SET health = health - 30
PRINTLN "Health: " .. health // Displays "Health: 70"
// Heal
SET health = health + 20
IF health > maxHealth
SET health = maxHealth
END IF
PRINTLN "Health: " .. health // Displays "Health: 90"The Set command enforces type consistency - you can only assign values that match the variable's declared type.
NEW INT count = 10
SET count = 20 // Valid: Integer literal
SET count = 5 + 5 // Valid: Expression result is Integer
SET count = count * 2 // Valid: Expression result is Integer
PRINTLN count // Displays "20"NEW FLOAT price = 10.0
SET price = 15.99 // Valid: Float literal
SET price = price * 1.5 // Valid: Expression result is Float
PRINTLN price // Displays "23.985"NEW STR message = "Hello"
SET message = "Goodbye" // Valid: String literal
SET message = message .. "!" // Valid: Expression result is String
PRINTLN message // Displays "Goodbye!"NEW BOOL flag = TRUE
SET flag = FALSE // Valid: Boolean literal
SET flag = 10 > 5 // Valid: Expression result is Boolean
PRINTLN flag // Displays "TRUE"NEW FLOAT total = 100.0
NEW FLOAT taxRate = 0.08
NEW FLOAT discount = 0.10
// Apply discount
SET total = total - ( total * discount )
PRINTLN "After discount: " .. total // Displays "After discount: 90.0"
// Add tax
SET total = total + ( total * taxRate )
PRINTLN "After tax: " .. total // Displays "After tax: 97.2"NEW INT score = 0
NEW INT i = 1
WHILE i <= 10
IF i % 2 == 0
SET score = score + i
END IF
SET i = i + 1
END WHILE
PRINTLN "Even sum: " .. score // Displays "Even sum: 30"NEW INT a = 5
NEW INT b = 10
NEW INT temp = 0
PRINTLN "Before: a=" .. a .. ", b=" .. b
SET temp = a
SET a = b
SET b = temp
PRINTLN "After: a=" .. a .. ", b=" .. b
// Displays "Before: a=5, b=10"
// Displays "After: a=10, b=5"NEW INT value = 10
PRINTLN "Start: " .. value
SET value = value + 5
PRINTLN "After add: " .. value // Displays "After add: 15"
SET value = value * 2
PRINTLN "After multiply: " .. value // Displays "After multiply: 30"
SET value = value - 10
PRINTLN "After subtract: " .. value // Displays "After subtract: 20"
SET value = value / 4
PRINTLN "Final: " .. value // Displays "Final: 5"It's important to understand the difference between Set and New.
NEW INT x = 10 // New creates the variable
PRINTLN x // Displays "10"
SET x = 20 // Set modifies the existing variable
PRINTLN x // Displays "20"
SET x = 30 // Set can be used multiple times
PRINTLN x // Displays "30"NEW INT age = 25 // New and requires data type
SET age = 26 // Set only needs the variable name and new value
SET age = 27 // Type is already known from creation
PRINTLN age // Displays "27"SET x = 10 // Runtime error! Variable "x" does not exist!
// Correct: Create variable first with New
NEW INT x = 10
SET x = 20 // Now validNEW INT x = 5
SET x = 5.0 // Runtime error! Cannot assign Float to Integer variable
SET x = 6 // Correct: Integer value matches Integer typeNEW INT x = 5
SET INT x = 10 // Syntax error! Set does not SET use x = 10 // Correct: Set only needs variable name and valueNEW INT count = 10
SET count = "twenty" // Runtime error! Cannot assign String to Integer
SET count = 20 // Correct: Integer matches Integer typeNEW BOOL flag = TRUE
SET flag = 1 // Runtime error! Cannot assign Integer to Boolean
SET flag = FALSE // Correct: Boolean literal matches Boolean typeNEW INT x = 10
NEW FLOAT y = 2.5
SET x = x + y // Type error! Cannot mix Integer and Float
// Correct: Keep types consistent
NEW INT a = 10
NEW INT b = 2
SET a = a + b // Valid: Both are IntegersNEW INT myVar = 5
SET MyVar = 10 // Runtime error! Variable "MyVar" does not exist
SET myVar = 10 // Correct: Case must match exactlyNEW STR message = "Hello"
SET message = Goodbye // Syntax error! Strings must be quoted
SET message = "Goodbye" // Correct: String is properly quotedNEW FLOAT price = 10.0
SET price = 15 // Runtime error! 15 is Integer, not Float
SET price = 15.0 // Correct: Float literal with decimal pointNEW INT value = 10
SET value = 10.5 // Runtime error! Cannot change type from Integer to Float
// Correct: Create new variable if different type needed
NEW INT value = 10
NEW FLOAT floatValue = 10.5