In Lua 5.3.0, I run "true and print("Hi")":
> true and print("Hi")
Hi
nil
Why does program output nil?
The print function returns nil:
> print("Hi") == nil
Hi
true
The expression true and nil returns nil (see Logical operators in Lua):
> true and nil
nil
That's why your original expression returns nil.
print function returns nothing, but when used with a boolean operator the number of return values is adjust to one value (which is nil in this case). (print("Hi")) has the same effect for the same reason, plain print("Hi") will not write the nil to the console.nil parameter is evident. Thank you for your clarification.This is a side effect of a new feature of the Lua interpreter in version 5.3.
In previous versions, this code was invalid:
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> true and print("Hi")
stdin:1: unexpected symbol near 'true'
Similarly:
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> 1+1
stdin:1: unexpected symbol near '1'
The interpreter only accepted statements, not expressions. You could prefix an expression with return or the shortcut = though:
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> =1+1
2
> =true and print("Hi")
Hi
nil
In Lua 5.3 a new feature has been introduced to make it simpler to use the interpreter like a calculator: if the input is invalid, then the interpreter tries to prefix it with return. This is why you get this result.