Lua - Conditional Assignment

Published 2014/03/03 by Admin in LUA
Tags: , , , , ,

We are able to take a bit of the long-windedness of the "if, else" statement away by using conditional assignment.

a=1

b=(a==1) and "one" or "not one"

The code above is the same as the code below.

a=1

if a==1 then

b = "one"

else 

b = "not one"

end

As you can see conditional assignment removes a lot of unnecessary code.


The following words are all reserved for internal functions in Lua.

These words cannot be used for variable names, but Lua is case sensitive, so 'and' is reserved and can't be a variable, but 'aND, aNd, AND, anD, AnD, And, ANd' are not reserved and can be used as variables.


my coding

This is a collection of all the coding gems I have found and would like to share with the world!