Some stuff

by qbguy (no login)

This should help you write some simple programs. You can read some existing code and use the QBASIC help file to figure out what new commands do.

Text:

PRINT "Hello World"

This text will display "Hello World"

and this command:

PRINT 2+2

will let print 4 because 4 is 2+2. Multiplication is *, Division is /, and exponentiation is ^. Integer division (divide and ignore remainder) is \ (backslash) and remainder is MOD (short for modulo). The order of operations is basically the normal order of operations for mathematics, which you can look up on the Internet but it is also in the QBASIC help file.

Variables:

There are four basic variable types:

INTEGER: a whole number between -32768 and 32767
LONG: a whole number between −2,147,483,648 and 2,147,483,647
SINGLE and DOUBLE: decimal / real numbers with different precisions

in QB64, there is also a 64 bit integer type which allows an even bigger range of whole numbers as well as a quad precision type. QB64 might also have a COMPLEX type for complex numbers.

A variable can be thought of as being like a little match box with a gummed label on it. Inside the matchbox is a small slip of paper capable of containing a single numerical quantity, such as: 27, or 3.1416, or -10000, etc.

Variable names can contain only letters, numbers, and periods (in QB64 they can contain underscores too). Variable names must begin with a letter. Variable names are case-insensitive, so iterator and Iterator are the same variable. Variable names must be less than 40 letters long (in QB64 they can be up to 255 letters long).

To set a variable as a data type, you can either put a symbol at the end:

% = integer
& = long
! = single
# = double
## = quad-precision (qb64 only)
&& = 64-bit integer (qb64 only)

or DIM it:

DIM variable AS INTEGER

To assign a variable, simply do this:

x = 2

Note that the statement x = 2 * y does NOT mean that when y changes, x also changes so that it is twice y. Also, the statement x = x + 1 increments x by 1. If it helps, you can put LET in front of the assignment statement:

LET x = 2

GOTO:

GOTO linenumber

GOTO linelabel

Line labels are ended with a colon.

It is strongly advised that you avoid indiscriminately using "Goto" statements in your program unless it is absolutely necessary. The misuse of excessive "Goto" statements can lead to what is sometimes referred to as "spaghetti code".

If you use too many gotos, Edser W Dijkstra will come back from the grave and murder you with a machete.

Logical operators:

< (less than)
<= (less than or equal to)
= (equal to)
<> (not equal to)
> (greater than)
>= (greater than or equal to)
NOT (bitwise complement)
AND (bitwise and)
OR (bitwise or)
XOR (bitwise XOR)

true is -1, false is 0.

If statement:

IF (expression) THEN dostuff

IF (expression) THEN dostuff ELSE dootherstuff

IF (expression) THEN
dostuff
END IF

IF (expression) THEN
dostuff
ELSE
dootherstuff
END IF

IF (expression) THEN
dostuff
ELSEIF (other expression) THEN
dodifferentstuff
ELSE
dootherstuff
END IF

If statements can be nested inside each other. Also, IF foo THEN GOTO 2 can be abbreviated as IF foo THEN 2 or as IF foo GOTO 2.

Loops:

The way to do a loop using goto is

index = startvalue
label: (stuff do to)
Set index = index + incrementbythismuch
If (index <= maximumvalue) THEN goto label

The better way to do this is:

FOR {variable} = start TO max STEP stepvalue
(do stuff)
NEXT

You can break out of a FOR loop with EXIT FOR.

Other loops:

WHILE (condition)
...
WEND

DO WHILE (condition)
...
LOOP

DO
...
LOOP WHILE (condition)

"Random" numbers:

Randomize TIMER -- make the "random" numbers more "random"
RND(1) -- random real number between 0 and 1

Random number between two numbers:

INT(RND(1)*(max-min+1))+min

INT rounds down to an integer.

Comments are done with apostrophe ('). Lines can be continued onto the next line with the underscore (_) character.

Functions:

DEF FN name(parameters)=expression

example:

DEF FNLOG10(X)=LOG(X)/LOG(10)

better type of function:

FUNCTION LOG10(X)
LOG10 = LOG(X)/LOG(10)
END FUNCTION

This type of function allows you to put code like loops and stuff inside the function. Function arguments are pass by reference, which means that if you modify a parameter, it gets modified in the main program.

To use a function after you have written it, just do this

X = LOG10(10)

sets x to 1, or if you used the DEF FN version,

X = FNLOG10(10)

Subroutines are like functions except that they don't return a value.

You call subroutines using a CALL statement with the arguments in parenthesis.

PI is equal to 3.14159265358979323846264338327950288419716939937510 which is 4*ATN(1).








Posted on May 16, 2008, 10:27 AM
from IP address 76.195.134.210

Respond to this message   

Goto Forum Home