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

Resources
Web Sites
More Tutorials
Vendors
Books
Magazines
NewsLetters
NewsGroups
Forums
User Groups
Talk Shows
Blogs

GBIC >> QBasic >> Tutorial >> Flow Control

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

Flow Control
QBasic contains several mechanisms to repeat blocks of code (looping) or to execute a block of code depending on the results of an expression. It also provides methods of jumping to a labeled block of code.

    • Conditional Execution
     if..then, select case, on..gosub, on..goto 
    • Conditional Looping
     for..next, while..wend, do..loop 
    • Program Redirection
     gosub..return, goto 
    • Loop Redirection
     exit, return 
    • Program Termination
     end, system, stop 

Expressions
An expression is simply QBasic code which resolves to a value. Expressions are typically contained within parentheses. Parentheses are also used to contain lists, where each member of the list is a separate expression.

     (5)              # parentheses (list) with a single, simple expression
     (5,"dog",2.5)    # parentheses with 3 simple expressions
     (a$+2)           # list with a single expression
     (sin($b)+3, 7)   # list with two expressions
     x$=3             # successful QBasic statements return a value of 0

Conditional Execution
Conditional execution statements direct QBasic to complete a block of code depending on the true/false value of an expression.

Note that a block of code is executed only once when these statements are used.

         General Form                        Sample
     ==========================         =========================
     if (expression) then               if (var$="no") then
         ..statements..                     print "one"
     elseif (expression) then           elseif (var$="yes") then
         ..statements..                     print "two"
     else                               else
         ..statements..                     print "three"
     end if                             end if

     select case (expression)                      selection case var%
        case (expression list)                        case 1
           ..statements..                                print "one"
        case (expression) To (expression)             case 2-12
           ..statements..                                print "two"
        case relational-operator (expression)         case > 12
           ..statements..                                print "three"
     end select                                    end select

Both the elseif and else lines are optional. When used, any number of elseif statements may be used.

Conditional Looping
QBasic also provides a means of repeating a block of statements a present number of times or based on the true/false value of an expression.

     for var$ = startval to endval     for i% = 1 TO 5
        ..statements..                       print %i
     next var$                          next i%


     while (expression)                 while (i% < 5)
         ..statements..                     i% = i% + 1
     wend                               wend


     do                                 do
        ..statements..                      print$ x$
     loop                               loop


     do                                 do
        ..statements..                      i% = i% + 1
     loop until (expression)            loop while i% > 5


     do                                 do
        ..statements..                      i% = i% + 1
     loop until (expression)            loop until i% > 5

Program Redirection
QBasic also provides the capability to redirect program flow without condition.

         General Form                        Sample
     ==========================         =========================
     gosub ... return                   gosub 24
                                        gosub NewPlace

     goto line                          goto 24        # line number
                                        goto NewPlace  # line label
In the gosub example, execution is redirected to a line labeled "24" or "NewPlace". When a return statement is encountered, program execution returns to the line following the gosub statement.

In the goto example, execution is premanently redirected to the new line (24 or NewPlace) and does not return.

Loop Redirection
QBasic provides methods for changing the order of executing statements within a loop - exit and return.

  • exit - jumps out of DO/FOR loop, a Function/Sub procedure, or a DEF FN function

  • return - branches to and returns from a subroutine

Program Termination

  • end - terminates the QBasic program, closing all files
  • stop - halts a program (can be restarted within QBasic IDE
  • system - closes all files and returns control to operating system

If you have any questions or corrections, please let me know.