Blocks

atharvai

Senior Member
I'm wondering how do u use Blocks and define them... Am i right in saying that a block is basically a bit of code like If...End if?

Let me know how to use them cos i'm writing a few programs which are very very long
 

hippy

Technical Support
Staff member
Block Structured Programming is a mechanism to avoid using GOTO statements to make the logic of a program clearer, more understandable and easier to maintain and debug. It does not necessarily make programs any shorter, either in lines of code, nor code generated, can actually increase both.

You don't have to define 'blocks', simply use them, although you will have to enabled the Enhanced Compilers - View->Options->Editor.

The block structures supported by the PICAXE are ...<code><pre><font size=2 face='Courier'>FOR var = varOrNum TO varOrNum { STEP varOrNum }
statements
NEXT

IF var op varOrNum [ AND|OR var op varOrNum ] THEN
statements
END IF

IF var op varOrNum [ AND|OR var op varOrNum ] THEN
statements
ELSE
statements
END IF

IF var op varOrNum [ AND|OR var op varOrNum ] THEN
statements
ELSIF var op varOrNum [ AND|OR var op varOrNum ] THEN
statements
ELSE
statements
END IF

SELECT CASE var
CASE varOrNum [ , varOrNum ]
statements
END SELECT

SELECT CASE var
CASE varOrNum [ , varOrNum ]
statements
ELSE
statements
END SELECT

DO
statements
LOOP UNTIL|WHILE var op varOrNum [ AND|OR var op varOrNum ]

DO UNTIL|WHILE var op varOrNum [ AND|OR var op varOrNum ]
statements
LOOP </font></pre></code>

In terms of editing 'blocks', these are simply sections of program which are prefixed by a '{' and end with '}'. When put into a program, the editor can hide and expand the parts in { ... } to alow some parts of the code to be hidden to make the rest more readable, for example ...<code><pre><font size=2 face='Courier'> FOR b0 = 1 TO 10
{
IF b0 = 1 THEN
{
GOSUB Multiply
}
ELSE
{
GOSUB Add
}
END IF
}
NEXT </font></pre></code> As can be seen, block-folding in the editing is quite closely tied to block-structures to be really effective. Also, correct indentation will help a lot. There is a whole eternity of debate we can have over whether the { and } should align with the outer block or statements within the inner block. My preference is as I've shown above, aligned with outer blocks.
 
Top