This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MudkipTheEpic's profile picture

Code Blocks (in Lua) And Local Variables

Started by MudkipTheEpic, 24 October 2013 - 07:29 PM
MudkipTheEpic #1
Posted 24 October 2013 - 09:29 PM
I saw a bunch of scattered definitions of code blocks and local variables over the tutorials section, so I decided to put my own here.

SpoilerThe definition from the PIL is as follows:
Programming In Lua said:
A block is the body of a control structure, the body of a function, or a chunk (the file or string with the code where the variable is declared).
In Lua, all blocks (excluding repeat) end with an "end", as opposed to many other languages who use curly braces or indentation to build blocks.
SpoilerBlocks in Lua can start with consist of:
  • do
  • if
  • repeat
Functions are also considered blocks.

Note that I never stated while or for, simply because do is the main word to start the block. for or while by themselves cannot start a block, but do can.
SpoilerLocal variables are variables that are only in scope in their own block.
Being in scope means the variable can be read and written to.

Some examples are as follows:
(coming soon)

Another note from the PIL is:
Programming In Lua said:
It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones.

This is all I had time to write tonight, make sure to leave (constructive) criticism on the tutorial! MUCH more content and details will be coming soon1.

Sources: http://www.lua.org/pil/4.2.html, Personal Experience

1 Soon is not guaranteed to be any particular time…
theoriginalbit #2
Posted 24 October 2013 - 10:58 PM
Needs a lot of work but I think it has potential. You've also missed quite a lot of code blocks. Also think about fixing and/or defining your high-level terms such as scope.
MudkipTheEpic #3
Posted 24 October 2013 - 11:56 PM
Needs a lot of work but I think it has potential. You've also missed quite a lot of code blocks. Also think about fixing and/or defining your high-level terms such as scope.

Thanks. I had limited time to post it, so that's the reason it lacks most content.

What are these other blocks you speak of?

I'll work on it A LOT when I have time.
theoriginalbit #4
Posted 25 October 2013 - 02:36 AM
Honestly I think you should have typed up the tutorial in a text editor and only posted it when you had more time to finish it off and make it better.

Any control statement is a block. When you leave the control statement any local variables defined within are deallocated.

Code blocks
function [name]([arguments]) [statements] end // function
if [condition] then [statements] end // if statement
while [condition] do [statements] end // while
for [declaration], [limit], [increment] do [statements] end // numeric for
for [declaration] in [iterable] do [statements] end // generic for
do [statements] end // do block
repeat [statements] until [condition] // repeat
surferpup #5
Posted 21 January 2014 - 02:12 PM
The only thing I note is that technically a doend is not technically a control structure. Control structures can redirect the sequential nature of the program counter inherently. Even a simple ifthenend control structure can cause the program to jump to the end of the control structure, skipping any code blocks contained within. All of the looping control structures (while, repeat and for) have dramatic impact on the program counter. doend specifically identifies a code block, in fact it is the only way to explicitly declare a code block without the use of a control structure.

I think I cover this pretty thoroughly in Lua Basics -- Variable Scope, Code Blocks and Control Structures.
Edited on 21 January 2014 - 01:13 PM