Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

What exactly is the "do" keyword for?

Asked by
LuaQuest 450 Moderation Voter
8 years ago

I've just been observing other people's code, and from time to time i see this "do" statement completely alone. Does this statement have any additional meaning to it? What exactly is it used for?

Example:

do
    print("Code")
end

1 answer

Log in to vote
0
Answered by 8 years ago

The "do" keyword can be used to create new scopes in code. Basically used for making a new environment for holding local variables, incase you don't want to make a new one.

Here's an example:

do
    local test = "hello"
    print(test) -- hello
end

print(test) -- nil
0
You could also explain do while to him. drahsid5 250 — 8y
0
Not needed. It's the do statement that creates a new scope for "while" and completes it. The "while" loop would be an entirely different topic. CodingEvolution 490 — 8y
0
Lua doesn't have `do while`, it has `repeat until`. This is also to *hide* variables so that you can keep things private in an implementation (modulescripts obviate this though) BlueTaslem 18071 — 8y
0
I think he was referring to the while loop, just ordered it wrong. CodingEvolution 490 — 8y
Ad

Answer this question