Postfix and Prefix Operators

Another addition to the Lua++ language is prefix and postfix operators. These can be used to increment and decrement a specific variable within your program. These types of operators are frequently found in C-based programming languages like C++, C#, or Java. The following example demonstrates how to use a postfix increment operator to print numbers from 0 to 9.

local v: number = 1

while v < 10 do
    print(v)
    v++        -- increment value of v by 1
end

The ++ operator will first return the value of that variable and then increment it by 1. If you want to increment the value prior to returning it, you can prefix your variable reference with the ++ operator.

local v: number = 1
 
print(v++)  -- prints 1
print(++v)  -- prints 2

To decrement variables the --- operator is used which functions exactly like the ++ operator. Lua uses -- to indicate the beginning of a comment, which conflicts with the conventional -- decrement operator of many other programming languages. Therefore, Lua++ provides a workaround that disables standard Lua comments using a macro which can be found here: Comment.

Last updated