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 >> Syntax

QBasic Information Center Tutorials - Syntax
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.

Syntax
Like any language, QBasic has rules on how the code must be written for the interpreter to be able to read and execute the code. These rules are called syntax.

The basics of QBasic syntax are demonstrated in the following short program. This example is simply various lines of code selected to demonstrate syntax concepts and are numbered solely for the purpose of reference in this tutorial. With QBasic, line numbers are optional. Lines may also be given a label (text name) rather than a number.

     1   Dim a$, b(5), c as Long # string variable and numeric array declared
     2   a$ = "Hello";           # no type declaration, end with semicolon
     3   a$ = _                  # underscore used to continue code on next line
     4        "Goodbye"          # 2nd line of a 2-line code
     5   a$ = 2 : b(4) = 3       # two statements on same line, separated by colon
     6   print (a$)  'comment    # parentheses (optional) around function arguments   
     7   if (a$+1) then          # expression
     8      print "True"         # single quotes, no parenthesis
     9   end if                  # ends the if statement
    10   REM what now?           # comment line
    11   call Print_Hello(1,2)   # calls subroutine Print_Hello
    12   sub Print_Hello (a$, c) # subroutine definition, no arguments
    13        print n$           # this line only executes when n$ is printed
    14   end sub                 # end the subroutine
    15   if (a$+1) then print "true"  # one line version of lines 7-8

     

Lines of Code
QBasic scripts consist of lines of case-insensitive code. A source code statement simply ends at the end of the line, although statements can be continued over multiple lines by using the underscore as a continuation character (line 2).

Multiple statements can be entered on a single line by separating them with a colon (line 5).

Comments
Any text to the right of an apostrophe character is treated as comments and is ignored during execution (line 6). Also, any line starting with "REM" is treated as a comment (line 10).

Variables
QBasic variable names begin with a-z and can contain 0-9 or a period (line 1). Variables do not have to be declared to be used. To create a variable, simply use it in a line of code (line 13).

Data Types
QBasic functions require data of specific types (integer, long, single, double, string). QBasic also support user-define data type. Unless otherwise declared, a variable data type is single.

Declaration of a variable can include the data type of the variable (line 1).

The data type of a variable can also be defined by following the name with a type identified (%-integer, &-long, $-string, !-single, #-double) (line 1).

Also, QBasic also lets you define the data type of variables beginning with specific letters (i.e. DEFINT A-D or DEFSNG R-T).

Parentheses
Parentheses () are used in several ways by QBasic. They can contain an expression (line 7), or a list of expressions (line 11/12). An expression is simply a code statement which resolves to a value.

Procedure arguments are enclosed in parentheses only if the Call statement is used to call the procedure.

Procedures
QBasic supports two types of procedures - subroutines and functions. A procedure is a block of code which can be executed by calling out the name of the procedure. Values (parameters) can be passed to procedures. Functions return a value, whereas subroutines do not.

If the procedure is called before it is declared in code, the "Call" function must be used.

If you have suggestions or corrections to this page, please let me know.