ProtoLang.net

Reference: Equal To

☰ Hide Sidebar
TOKEN EQUAL TO
ALIAS =

The Equal To General Operator is used to assign a value to a variable. It appears in two contexts: during variable creation with the New command to set the initial value, and with the Set command to change an existing variable's value. The value on the right side of Equal To is stored in the variable on the left side.

Syntax

The short syntax structure uses the = symbol:

NEW DATA TYPE VARIABLE = VALUE
SET VARIABLE = VALUE

The long syntax structure uses the words EQUAL TO:

NEW DATA TYPE VARIABLE EQUAL TO VALUE
SET VARIABLE EQUAL TO VALUE

Using Equal To with New

When creating a variable with New, Equal To assigns the initial value. The value must match the declared data type.

Example: Assigning Initial Values

NEW INT x = 10 // Assigns 10 to variable x
NEW STR name = "Alice" // Assigns "Alice" to variable name
NEW BOOL isActive = TRUE // Assigns TRUE to variable isActive
NEW FLOAT price = 19.99 // Assigns 19.99 to variable price

Example: Assigning Variable to New Variable

NEW INT x = 5
NEW INT y = x // Assigns the value of x (5) to y
PRINTLN y // Displays "5"

Example: Assigning Expression Results

NEW INT sum = 5 + 3 // Assigns result of expression (8) to sum
NEW BOOL isValid = 10 > 5 // Assigns result of comparison (TRUE) to isValid
NEW STR message = "Hello" .. " World" // Assigns concatenated string to message
PRINTLN sum // Displays "8"
PRINTLN isValid // Displays "TRUE"
PRINTLN message // Displays "Hello World"

Using Equal To with Set

When modifying a variable with Set, Equal To assigns a new value. The variable must already exist and the value must match its data type.

Example: Changing Variable Values

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

Example: Using Variable in Its Own Assignment

NEW INT x = 5
SET x = x + 1 // Assigns x + 1 (6) back to x
PRINTLN x // Displays "6"
SET x = x * 2 // Assigns x * 2 (12) back to x
PRINTLN x // Displays "12"

Example: Incrementing and Decrementing

NEW INT counter = 0
SET counter = counter + 1 // Increment by 1
PRINTLN counter // Displays "1"
SET counter = counter - 1 // Decrement by 1
PRINTLN counter // Displays "0"

Equal To vs Is Equal To

It's important to distinguish between the assignment operator (Equal To / =) and the comparison operator (Is Equal To / ==).

Example: Assignment vs Comparison

NEW INT x = 5 // EQUAL TO (=) assigns 5 to x
NEW BOOL y = x == 5 // IS EQUAL TO (==) compares x to 5, result is TRUE
PRINTLN x // Displays "5"
PRINTLN y // Displays "TRUE"

Complex Assignments

Example: Assigning Complex Expressions

NEW INT a = 5
NEW INT b = 3
NEW INT result = ( a + b ) * 2 // Assigns ( 5 + 3 ) * 2 = 16
PRINTLN result // Displays "16"

Example: String Concatenation Assignment

NEW STR firstName = "John"
NEW STR lastName = "Doe"
NEW STR fullName = firstName .. " " .. lastName
PRINTLN fullName // Displays "John Doe"

Example: Assignment in Loops

NEW INT i = 1
NEW INT sum = 0
WHILE i <= 5
    SET sum = sum + i // Add i to sum each iteration
    SET i = i + 1 // Increment i
END WHILE
PRINTLN sum // Displays "15"

Important Notes

  • Equal To (=) is for assignment; Is Equal To (==) is for comparison.
  • The value assigned must match the variable's data type.
  • The right side is evaluated first, then assigned to the left side.
  • A variable can use its own current value in the assignment expression.

Common Errors

Error: Wrong Data Type Assignment

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

Error: Using Comparison Operator for Assignment

NEW INT x = 5
SET x == 10 // Syntax error! Use = for assignment, not ==
SET x = 10 // Correct! EQUAL TO (=) assigns value

Error: Assigning to Non-Existent Variable

SET x = 10 // Runtime error! Variable x doesn't exist
NEW INT x = 10 // Correct! Create variable first

Error: Missing Equal To in New

NEW INT x // Syntax error! Must assign initial value
NEW INT x = 0 // Correct! Initial value assigned with EQUAL TO