A Variable is a named container that stores a value in memory. Variables must be created with the New command before they can be used, and each variable has a specific Data Type that determines what kind of data it can hold.
Variable names in ProtoLang must follow these rules:
Variables are created using the New command. You must specify the data type and initial value when creating a variable.
NEW STR name = "Alice"
NEW INT age = 25
NEW FLOAT height = 5.6
NEW BOOL isStudent = TRUEOnce created, variables can be used anywhere a Literal would be used. The variable's name represents its stored value.
NEW INT x = 42
PRINTLN x // Displays "42"NEW INT a = 10
NEW INT b = 5
NEW INT sum = a + b
PRINTLN sum // Displays "15"NEW INT x = 100
NEW INT y = x
PRINTLN y // Displays "100"Variables can be modified using the Set command. The variable must already exist before it can be modified.
NEW INT counter = 0
PRINTLN counter // Displays "0"
SET counter = 10
PRINTLN counter // Displays "10"NEW INT x = 5
SET x = x + 1
PRINTLN x // Displays "6"By default, all variables are in global scope and can be accessed from anywhere in the program after they are created. For information about local scope, see Functions.
NEW INT x = 10
PRINTLN x // Displays "10"
SET x = 20
PRINTLN x // Displays "20"Variables can be removed from memory using the Delete command or all variables can be cleared with Clear.
NEW INT x = 10
NEW INT y = 20
DELETE x // Only x is deleted
PRINTLN y // Displays "20"NEW INT x = 10
NEW INT y = 20
CLEAR // Both x and y are deletedPRINTLN x // Runtime error! Variable "x" does not exist!
NEW INT x = 5 // This line is not executed because the program crashed!NEW INT x = 5
NEW INT x = 10 // Runtime error! Variable "x" already exists!NEW INT x = 5
SET x = 5.0 // Runtime error! Cannot assign Float value to Integer variable!NEW INT myVariable = 5
PRINTLN MyVariable // Runtime error! Variable "MyVariable" does not exist!
PRINTLN myVariable // Correct! This will display "5"