I know that the BASIC language has fallen out of favor but I still find it entertaining. In this reference I will attempt to provide a somewhat comprehensive (eventually) guide to the BASIC language, particularly as it appears in later DOS-based Microsoft BASIC releases (e.g., QBasic, QuickBasic, PDS, and VBDOS). My initial attempt will focus primarily on QuickBasic 4.5.
Best Practices
Place parentheses around expressions to make visibly clear the beginning and end of each expression: IF (A = B) AND (B = Z) THEN ...
Place parentheses around mathematical expressions involving multiple operators: result = (A + B) * (C - D)
Control-Flow Structures
In BASIC, an expression is true if it returns -1 and an expression is false if it returns 0 (see Microsoft QuickBasic Programming in BASIC, Section 1.2 Boolean Expressions for details): PRINT 10 < 5 ' Returns 0 for false PRINT 5 < 10 ' Returns -1 for true
Available Relational Operators
Operator
Purpose
=
Equal
<
Less Than
>
Greater Than
<=
Less Than or Equal To
>=
Greater Than or Equal To
<>
Not Equal To
Available Logical Operators
Operator
Purpose
AND
Return true only when all expressions are true
OR
Return true when either expression is true
NOT
Return true only if the expression is false
XOR
IMP
EQV
IF…THEN…ELSE..ELSE IF
OriginalValue = 10
NewValue = 20
IF OriginalValue < NewValue THEN
PRINT "The New Value is greater than the Original Value."
ELSE IF OriginalValue > NewValue THEN
PRINT "The New Value is greater than the Original Value."
ELSE
PRINT "The New Value is equal to the Original Value."
SELECT CASE
InputValue = 20
SELECT CASE InputValue
CASE 1 TO 5:
PRINT "You are too young."
CASE 6:
PRINT "Have you finished kindergarten?"
CASE 7, 8, 9:
PRINT "I should say something smart here."
CASE IS > 9:
PRINT "I wish I was your age."
CASE ELSE:
PRINT "Ahh, have you even be born yet?"
Important: Unlike some other programming languages, in QuickBasic once one hits a valid CASE the code for that CASE is executed and then execution skips the rest of the CASES. In some languages one has to explicitly tell the SELECT CASE statement that it should terminate after executing a case, otherwise it will continue to execute all matching cases.
FOR…NEXT
FOR i = 1 TO 5:
PRINT "Hello World!"
IF i = 4 THEN EXIT FOR
NEXT i
WHILE…WEND
DO…LOOP
SUBs and FUNCTIONS
Bibliography
Microsoft(R) QuickBASIC: Programming in Basic. Microsoft, 1987/1988.