LOOPS & CODE BLOCKS
Updated: June 20, 2005

Loops & Conditional Code Blocks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Each type of code block may be nested up to 12 levels.

''''''
  IF {condition} THEN {statements} [ELSE {statements}]

or

  IF {condition} THEN
    {statements}
  [ELSEIF {condition} THEN
    {statements}]
  [ELSE
    {statements}]
  END IF

''''''
  SELECT CASE {string or number value}
    CASE [> | < | >= | <= | <>] {value1}
      {statements}
    CASE {value2}  'implicitly, the omitted operator is "="
      {statements}
    CASE {value??}
      {statements}
    'etc
    [CASE ELSE
      {statements}]
  END SELECT

SELECT CASE code is illustrated in hottest.bas

GOTO statements can jump to a LABEL anywhere in a SELECT CASE block, so that,
in effect, procedures can be written that funnel from multiple entry points
into common code anywhere in the SELECT code.

''''''
  FOR MyVar = param1 TO param2 [STEP [-]param3]
  'code
  NEXT [MyVar]

For general basic compatibility, MyVar and the param1 - 3 can be any numeric
type.  However, 4-byte non-float values - DWORD, LONG, INTEGER - are faster.

param3 must be a positive value and a "-" prefix is used to show sign.

If MyVar is altered inside the FOR NEXT loop, STEP must be used to properly
alter number of loop iterations -- STEP forces a compare of index variable
MyVar with the limit param2.

IF 4-byte non-floating variables are used in FOR NEXT loops, faster code
will be compiled.

If a loop STEP value is not defined and therefore assumed to be one, speed
is the fastest possible.  

  DEFINT i
  FOR i = 1 TO 1000000: NEXT i

is almost twice as fast as

  DEFINT i
  FOR i = 1 TO 1000000 STEP 1: NEXT i

although logically the loops are identical if STEP = 1.  Absence of the
STEP keyword is the cue to minimalize application code for speed.

In the first example above, loop time is the theoretical mimimum meeting
FOR NEXT specifications that can be achieved in any computer language,
including assembler language.  Only three opcodes are used: inc Index,
dec LoopCount and jg LoopStart (jg = jump if greater than zero).

Even in assembler, the first two opcodes could be faster only if CPU
registers were dedicated to the Index and LoopCount values which is not
feasible when, as usual, data is processed in the loop and loops might be
nested.

In the FOR NEXT loop, if you change the index i above, use STEP to ensure
the loop count will reflect the new index value.


''''''
  DO
  {statements}  'code
  IF {condition} THEN Goto Do_Done 'implements Exit Do
  {statements}  'code
  LOOP [UNTIL {condition}]
  Do_Done:

''''''
  WHILE {condition}
    {statements}
    IF {condition} THEN Goto While_Done 'implements Exit While
    {statements}
  END WHILE  'or WEND
  While_Done:

EXIT IF, EXIT SELECT, EXIT FOR, EXIT DO and EXIT WHILE may be used to exit
code blocks.

If you want more than 12 levels of code block nesting:
(1) see your family physician.  If (1) fails, (2) contact us.


+ Penthouse (registered) version

Copyright 2003-2005 James J Keene PhD
Original Publication: Oct 8, 2003
