Getting Started
Introduction
Sample Programs
IDEs
History
Advice
Mini-Tutorial
Tutorials
Code Snippets

Resources
Web Sites
More Tutorials
Forums
Vendors/Tools
Books
Magazines
Newsletters
NewsGroups
User Groups
Talk Shows
Blogs

Controls
Overview
Button
Check3State
Checkbox
ComboBox
Frame
Graphic
Image
ImageX
ImgButton
ImgButtonX
Label
Line
ListBox
ListView
Option
Progress Bar
Scrollbar
StatusBar
TAB
TextBox
Toolbar
TreeView

GBIC >> PowerBASIC >> Tutorials >> Flow Control

PowerBASIC Information Center Tutorials
These tutorials were written to help you get a quick, but thorough, understanding of PowerBASIC - the scope of the language as well as it's specific capabilities.

Introduction       Projects       Language           Messages       Functions           Advanced
  • Overview
  • Examples
  • IDE
  • Compilation
  • Distribution
  • Project Files
  • DDT Windows
  • Controls
  • Menus
  • Dialogs  
  • Help Files
  • Resources  
  • Templates  
  • Project Shell  
  • Syntax
  • Operators
  • Data Types
  • Variables
  • Scope
  • Declarations  
  • Procedures
  • Flow Control
  • Windows
  • Messages
  • Callbacks
  • Mouse
  • Keyboard
  • Dialogs
  • Controls
  • Subclassing
  • Arithmetic
  • Trig  
  • Strings
  • Arrays
  • Date/Time
  • Printing
  • Files
  • Folders
  • Keyboard
  • Mouse
  • Sound
  • System
  • Error Traps
  • Debugging
  • Objects
  • Graphics
  • Databases
  • API
  • DLLs
  • ASM
  • Threads
  • Flow Control
    Like all programming languages, PowerBASIC contains several structures to determine which blocks of code to execute and how often to repeat them. The structures can be summarized into the following categories.

    • Conditional Execution - execute once
    • Conditional Looping - execute repeatedly
    • Enumerated Looping - loop a specified number of times
    • Unconditional Branching - jump to labelled line of code

    Here is a summary of the specific PowerBASIC structures involved. These will be discussed in more detail further down this page.

      • Conditional Execution
       If Then, Select Case, IIF, Switch 
       Choose, On Goto, On GoSub
      
      • Conditional Looping
       Do/Loop, While/Wend 
      • Enumerated Looping
       For/Next
      • Unconditional Branching  
       GoTo, GoSub 
      • In-Loop Redirection
       Exit, Iterate 

    Conditional Execution
    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/END IF
      Statements of the first TRUE (non-zero) condition are executed and the IF/END IF structure is then terminated - only one block of code will be executed.
          IF (expression) THEN
              <statements>
          ELSEIF (expression) THEN
              <statements>  
          ELSEIF (expression) THEN        'multiple ELSEIF allowed
              <statements>
          ELSE
              <statements>
          END IF
      

    • SELECT/END SELECT
      The SELECT/END SELECT structure is the only one of the flow control structures which allows multiple comparisons of the expression.

           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
      

    • IIF, SWITCH, CHOOSE
      PowerBASIC offers three functions which return a value from a list, depending on the value of one or more test expression. Note that the values themselves may be expressions. If expressions are used to provide values, only the chosen expression is evaluated. Unselected expressions are not evaluated.

          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&.

    • ON..GOTO, ON..GOSUB
      These functions jump to the code at label or line number, based on the value of a numeric expression. The ..GOTO does not return to the point where the call was made. The ..GOSUB will return to the calling point once it finds a RETURN statement. Both functions can only call code within the current scope.

           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"
      

    Conditional Looping
    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.

    • IF/END IF
    • DO/LOOP
    • WHILE/WEND
    • SELECT/END SELECT

    Here are examples of each.

    • DO/LOOP
      There are 4 variations on a DO/LOOP:
          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/WEND
      The WHILE/WEND structure has the constraint that the expression must evaluate to an integer. The loop continues until the expression evaluates to FALSE (zero).

          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).

    Enumerated Looping
    With enumerated looping a program can specify the number of times that a block of statements can be executed. The FOR/NEXT structure is the only enumerated looping structure provided by PowerBASIC.

         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.

    Unconditional Branching
    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
    

    In-Loop Redirection
    PowerBASIC provides two methods for changing the order of executing statements within a loop - exit and iterate.

    • exit - leaves the current flow control structure permanently
      applies to for|if|do|loop|select|try
    • iterate - immediately starts a new iteration of a loop structure
      applies to do|loop|for

    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.