ProtoLang.net

Reference: Variable

☰ Hide Sidebar
TOKEN None
ALIAS None

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

Variable names in ProtoLang must follow these rules:

  • Names can contain letters (both uppercase and lowercase), making them case-sensitive.
  • Variable names must be unique within their scope.
  • Names like x and X are considered different variables.
  • Names should be descriptive and meaningful to make code easier to understand.

Creating Variables

Variables are created using the New command. You must specify the data type and initial value when creating a variable.

Example: Creating Variables of Different Types

NEW STR name = "Alice"
NEW INT age = 25
NEW FLOAT height = 5.6
NEW BOOL isStudent = TRUE

Using Variables

Once created, variables can be used anywhere a Literal would be used. The variable's name represents its stored value.

Example: Using Variables in Print Statements

NEW INT x = 42
PRINTLN x // Displays "42"

Example: Using Variables in Expressions

NEW INT a = 10
NEW INT b = 5
NEW INT sum = a + b
PRINTLN sum // Displays "15"

Example: Copying Variable Values

NEW INT x = 100
NEW INT y = x
PRINTLN y // Displays "100"

Modifying Variables

Variables can be modified using the Set command. The variable must already exist before it can be modified.

Example: Changing a Variable's Value

NEW INT counter = 0
PRINTLN counter // Displays "0"
SET counter = 10
PRINTLN counter // Displays "10"

Example: Using a Variable in its Own Update

NEW INT x = 5
SET x = x + 1
PRINTLN x // Displays "6"

Variable Scope

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.

Example: Global Scope Variables

NEW INT x = 10
PRINTLN x // Displays "10"
SET x = 20
PRINTLN x // Displays "20"

Deleting Variables

Variables can be removed from memory using the Delete command or all variables can be cleared with Clear.

Example: Deleting a Specific Variable

NEW INT x = 10
NEW INT y = 20
DELETE x // Only x is deleted
PRINTLN y // Displays "20"

Example: Clearing All Variables

NEW INT x = 10
NEW INT y = 20
CLEAR // Both x and y are deleted

Important Notes

  • Variables must be created before they can be used.
  • A variable's data type cannot be changed after creation.
  • Variable names are case-sensitive (x and X are different).
  • Each variable can only hold one value at a time.
  • Variables must be unique within their scope.

Common Errors

Error: Using a Variable Before Creating It

PRINTLN x // Runtime error! Variable "x" does not exist!
NEW INT x = 5 // This line is not executed because the program crashed!

Error: Creating a Variable with the Same Name Twice

NEW INT x = 5
NEW INT x = 10 // Runtime error! Variable "x" already exists!

Error: Assigning Wrong Data Type to Variable

NEW INT x = 5
SET x = 5.0 // Runtime error! Cannot assign Float value to Integer variable!

Error: Using Case Incorrectly

NEW INT myVariable = 5
PRINTLN MyVariable // Runtime error! Variable "MyVariable" does not exist!
PRINTLN myVariable // Correct! This will display "5"