29

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 ?

prapin
  • 6,395
  • 5
  • 26
  • 44

8 Answers8

39

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

So it's equivalent to:

a = 3;
b = 5;
c = a
Esailija
  • 138,174
  • 23
  • 272
  • 326
17

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

Luatic
  • 8,513
  • 2
  • 13
  • 34
Foggzie
  • 9,691
  • 1
  • 31
  • 48
  • True, but this doesn't explain why the program compiles and runs. – prapin Dec 28 '12 at 08:14
  • It compiles and runs because LUA does not mind missing semicolons at the end of lines. It will just print out 3 5 3. – annih Nov 21 '14 at 14:16
  • 2
    @annih It does make sense why this code compiles, as said on this answer -- starts a comment, so essentialy your code is: `a = 3; b = 5; c = a;` Note that ; is necessary on top of my head if you do it all in one line, like I do – engineercoding Nov 23 '14 at 01:32
11

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

mnille
  • 1,328
  • 4
  • 16
  • 20
Seniru Pasan
  • 753
  • 1
  • 7
  • 13
4

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

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

This will give

3 5 3

because the 3rd line will be evaluated as c = a.

Why? Because in Lua, comments starts with --. Therefore, c = a-- * b++; // some computation is evaluated as two parts:

  1. expression: c = a
  2. comment: * b++; //// some computation
NetherGranite
  • 1,940
  • 1
  • 14
  • 42
0

There are 2 problems in your Lua code:

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

One, Lua does not currently support incrementation. A way to do this is:

c = a - 1 * b + 1
print(a, b, c)

Two, -- in Lua is a comment, so using a-- just translates to a, and the comment is * b++; // some computation.

Three, // does not work in Lua, use -- for comments.

Also it's optional to use ; at the end of every line.

TERIHAX
  • 237
  • 2
  • 14
-2

EDIT: Using SharpLua in C# incrementing/decrementing in lua can be done in shorthand like so:

a+=1 --increment by some value
a-=1 --decrement by some value

In addition, multiplication/division can be done like so:

a*=2 --multiply by some value
a/=2 --divide by some value

The same method can be used if adding, subtracting, multiplying or dividing one variable by another, like so:

a+=b
a-=b
a/=b
a*=b

This is much simpler and tidier and I think a lot less complicated, but not everybody will share my view.

Hope this helps!

  • 1
    Those operators like `+=` do not exist in Lua, even in the latest work version Lua 5.4! – prapin May 31 '20 at 07:43
  • @prapin I've been using said operators for about 2 months now, tested and working... so, either they're not documented or my lua scripts are 'magically' interpreting said operators and doing exactly what I expect them to do. I suggest you do some of your own testing before dismissing my solution right off the bat. – Werner Bessinger May 31 '20 at 09:40
  • I was surprised, so I did test on a raw Lua 5.3 interpreter, and I also checked in the documentation in case it would be a new feature in 5.4 that I didn't know about. I suspect that you are using a _modified_ version of the Lua interpreter (which one?) – prapin May 31 '20 at 09:58
  • @prapin I'm using SharpLua as parser within C# which probably then has support for the shorthand operators built in, so technically then it wouldn't work with the vanilla interpreter. Would be great if it could be made a standard feature - would certainly tidy things up a bit! So yeah, I suppose technically it doesn't work 'out of the box'. – Werner Bessinger May 31 '20 at 10:13
-2

You can do the following:

local default = 0
local max = 100

while default < max do
     default = default + 1
     print(default)
end
lemon
  • 14,875
  • 6
  • 18
  • 38
xo1337
  • 1