Most programming languages allow you to add notes to your
programs that are ignored by the computer. This lets you
explain what you've done to someone else who might read
your program later. In QBASIC we use the apostrophe (') to
begin a comment. Here's an example:
' A program to draw boxes all over the screen ' This is a comment, QBASIC will ignore it SCREEN 12 CLS ' Draw 50 boxes FOR I = 1 TO 50 ' Pick the location of the box X1 = INT(RND * 640) Y1 = INT(RND * 480) X2 = INT(RND * 640) Y2 = INT(RND * 480) ' Pick the color for the box Color1 = INT(RND * 16) ' Draw the box LINE (X1, Y1) - (X2, Y2), Color1, BF NEXT I
The computer will ignore all those comment lines,
but us humans can read them and remember how a program
works. Good programmers use comments to help others
understand what they have done. Comments can also help
us remember what we did
when we come back to a program after
working on something else for a while.
Constants
Another way to make your programs easier to understand
is to use constants. Constants look and act like variables,
but they cannot be changed. Here's a useful program:
CONST Pi = 3.141593 INPUT "Enter the radius of a circle: ", Radius PRINT "The circumference is:"; 2 * Pi * Radius PRINT "The area is:"; Pi * Radius * Radius
If we didn't use the constant Pi, we would have to copy
the number 3.141593 two places in the above program. Using
a constant makes the program easier to read and understand.
It also keeps us from making mistakes when copying.
Source: http://jpsor.ucoz.com |