Compound Assignments
When writing programs, it is generally necessary to increment a variable. The problem with Lua and many other high-level scripting languages is that they don’t provide an efficient way to do such a this:
Although this method is simple and straightforward, it becomes inefficient when dealing with large-scale programs and longer variable names. Additionally, the VM needs to reference the value of a
, add 13
to it, and update the original value of a
. An operator for actions like this would decrease execution time and improve developer productivity. The following code block shows an example using the +=
compound assignment.
There is a compound assignment operator for each arithmetic and string operation in Lua, such as (+=
, -=
, /=
, *=
, ^=
, %=
, and ..=
). Each operation assigns a variable the value of itself with the operation performed on the original value.
Last updated