Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementation in Lua

Tags:

lua

I am playing a little bit with Lua.

I came across the following code snippet that have an unexpected behavior:

a = 3;
b = 5;
c = a-- * b++; // some computation
print(a, b, c);

Lua runs the program without any error but does not print 2 6 15 as expected. Why ?

like image 420
prapin Avatar asked Dec 18 '12 22:12

prapin


3 Answers

-- starts a single line comment, like # or // in other languages.

So it's equivalent to:

a = 3;
b = 5;
c = a
like image 124
Esailija Avatar answered Nov 08 '22 14:11

Esailija


LUA doesn't increment and decrement with ++ and --. -- will instead start a comment.

like image 28
Foggzie Avatar answered Nov 08 '22 14:11

Foggzie


There isn't and -- and ++ in lua. so you have to use a = a + 1 or a = a -1 or something like that

like image 25
Seniru Pasan Avatar answered Nov 08 '22 14:11

Seniru Pasan