Increment to Lua

I play a little with Lua.

I came across the following piece of code that has unexpected behavior:

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

Lua launches the program without any errors, but does not print 2 6 15 as expected. Why?

+7
source share
4 answers

-- launches one line comment , for example # or // in other languages.

So this is equivalent to:

 a = 3; b = 5; c = a 
+18
source

LUA does not increase or decrease with ++ and -- . -- will start a comment instead.

+6
source

If you want 2 6 15 as output, try this code:

 a = 3 b = 5 c = a * b a = a - 1 b = b + 1 print(a, b, c) 
+2
source

It will give

3 5 3

because the 3rd row will be evaluated as c = a

why? because in lua comments start with -. Therefore c = a-- * b ++; // calculation

estimated as two parts:

  • expression: c = a
  • comment: * b ++; //// some calculations
+1
source

All Articles