Here is a summary of the specific PowerBASIC structures involved. These will be discussed in more detail further down this page.
If Then, Select Case, IIF, Switch Choose, On Goto, On GoSub
Like all languages, PowerBASIC supports several flow-control structures. These allow a programmer to use the value of an expression to determine which PowerBASIC statements to execute.
IF (expression) THEN <statements> ELSEIF (expression) THEN <statements> ELSEIF (expression) THEN 'multiple ELSEIF allowed <statements> ELSE <statements> END IF
SELECT CASE (expression) CASE 5 'single value <statements> CASE 23,35,42 'list of values <statements> CASE 65 TO 75 'range of values <statements> CASE > 100 'relational value <statements> CASE ELSE <statements> END SELECT
Returns value based whether expression is TRUE or FALSE: IIF (expr, TRUEvalue, FALSEvalue) IIF& (expr, TRUEvalue, FALSEvalue) IIF$ (expr, TRUEvalue, FALSEvalue) Returns value paired with the first TRUE expression. SWITCH (expr1, value1, expr2, value2, ...) 'any numeric value SWITCH& (expr1, value1, expr2, value2, ...) 'long values SWITCH$ (expr1, value1, expr2, value2, ...) 'string values Returns value matching the value of the index: CHOOSE (index&, value1, value2, ...) 'any numeric value CHOOSE& (index&, value1, value2, ...) 'long values CHOOSE$ (index&, value1, value2, ...) 'string values
PowerBASIC recognizes IIF%/CHOOSE%/SWITCH% as for IIF&/CHOOSE&/SWITCH&.
val = 3 On val GOTO (100,150,200,400,600) 'jumps to line 200 On val GOSUB ("one","two","three","four") 'jumps to label "three"
Here are examples of each. The PowerBASIC FOR/NEXT structure also provides a means of repeating a block of statements a specific number of times, as in the following example.
Here are examples of each.
DO <statements> LOOP UNTIL x = 10 DO <statements> LOOP WHILE x < 10 DO WHILE x = 10 <statements> LOOP DO UNTIL x > 10 <statements> LOOP
WHILE (expression) <statements> WEND
The WHILE/WEND loop statements are executed as long as the expression is TRUE (non-zero). The loop stops when the expression is FALSE (zero).
FOR i = 1 TO 100 <statements> 'executed 100 times NEXT i FOR i = 10 TO 100 STEP 5 <statements> 'executed 19 times NEXT i
In the first example, the block of statements will execute 100 times. The value of i starts at 10 and is incremented by a value of 1 for each iteration of the loop. When i reaches the value of 101, the loop is exited.
The second example is similar, except that the value of i is incremented by a value of 5 for each iteration of the loop.
These functions jump to the code at label or line number - without checking the value of an expression as was the case with On..GoTo and On..GoSub..
The GOTO does not return to the point where the call was made, whereas the GOSUB will return to the calling point once it reaches a RETURN statement. Both functions can only call code within the current scope.
Here is an example of each.
GoTo 100 'jumps immediately to line 100 a$ = 5 'skipped by the GoTo 100 a$ = 6 'a$ is now 6 GoSub 100 'jumps immediately to line 100 99 a$ = 5 'skipped by the GoTo, but executed by Return Dialog End 100 a$ = 6 'a$ is now 6 Return 'returns to line following GoSub
PowerBASIC provides two methods for changing the order of executing statements within a loop - exit and iterate.
Here are examples of each.
IF x>2 THEN <statements> EXIT IF <statements> 'will not be executed END FOR i = 2 TO 200 IF i > 12 and i < 20 THEN ITERATE FOR END IF <statements> 'skipped for values of 13 to 19 NEXT i
If you have any questions or corrections, please let me know.