LOOPS & CODE BLOCKS Updated: Jan 6, 2012 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 [> | < | >= | <= | <>] {value_1} {statements} CASE {value_2} 'implicitly, the omitted operator is "=" {statements} [CASE {value_n} {statements}] [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 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. '''''' + Registered version USE {link} AS {type} WITH {pointer} {statements using link} END USE {link} where {link} is a user symbol to access a {type} by {pointer}, {type} is a qualified type which may be a native HotBasic Object (e.g., STRING, FONT, BITMAP) or a UDT/Custom Object, and {pointer} is a numeric expression for a pointer to a dimensioned instance of the Object. Example: DIM A as LIST: Aptr = OBJPTR(A) USE xLIST as LIST with Aptr 'access A by pointer xLIST.LoadFromFile "MyData.lst" print xLIST END USE xLIST USE ... END USE code blocks provide access to Objects by pointer anywhere in your project. For example, in $APPTYPE OBJ, the .obj module code may have a pointer to an Object but not its dimensioned name. You may wish to pass an Object pointer to a SUB/FUNCTION to process data by pointer. Notice the same link (xLIST above) may be reused for all LIST Objects in your project. That is, the xLIST link binds xLIST to the LIST Object while the pointer to a specific instance may be changed in different USE blocks. USE blocks may be nested so that a code section may access any arbitrary number of Objects by pointer. EXIT SUB, EXIT FUNCTION and RETURN should not be written in USE blocks. + Penthouse (registered) version Copyright 2003-2012 James J Keene PhD Original Publication: Oct 8, 2003