In programming languages such as C you can create an anonymous code block to limit the scope of variables to inside the block can the same be done with Lua?
If so what would be the Lua equivalent of the following C code?
void function()
{
{
int i = 0;
i = i + 1;
}
{
int i = 10;
i = i + 1;
}
}
A comment starts with a double hyphen ( -- ) anywhere outside a string. They run until the end of the line. You can comment out a full block of code by surrounding it with --[[ and --]] . To uncomment the same block, simply add another hyphen to the first enclosure, as in ---[[ .
JavaScript Code Blocks JavaScript statements can be grouped together in code blocks, inside curly brackets {...}. The purpose of code blocks is to define statements to be executed together.
You want to use do...end
. From the manual:
A block can be explicitly delimited to produce a single statement:
stat ::= do block end
Explicit blocks are useful to control the scope of variable declarations. Explicit blocks are also sometimes used to add a
return
orbreak
statement in the middle of another block
function fn()
do
local i = 0
i = i + 1
end
do
local i = 10
i = i + 1
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With