Programming in C too complicated? New to the world of programming? If you've never programmed a computer before, BASIC (Beginner's All-Purpose Symbolic Instruction Code) is an excellent language to start with. It has a very simple form which will make using it easy for beginners, but it can do enough cool stuff to make it interesting.
The PRINT command:
Simply prints stuff out to the screen. The syntax ("syntax" just means the form that the command takes) is:
The whatever is the text you want to print.
To print variables (which is simply a piece of data that can change, or be varied), do this:
Where varname is the name of the variable. Simple, isn't it! You can even mix the two. Just put straight text strings inside quotation marks to mark it as verbose text, and variables outside quotation marks. So, if you had a variable called age which contained Bill Gates' age (just for example), you could use the following command:
PRINT "Bill Gates is "; age; " years old!"
Just remember to have those semicolons (;) to separate the printed text from the variable name, as you can see above.
The INPUT command:
Takes input from the keyboard. The two fundamental data streams of a computer program are INPUT and OUTPUT. The PRINT command takes care of output, giving the computer user stuff to look at. The INPUT command (appropriately enough) handles... You gussed it: Input! See? You're catching on already! :)
Syntax for INPUT is also simple:
INPUT "Some sort of prompt", varname
This will show the prompt specified between the quotation marks, and then put their input into a variable called varname. You can then manipulate the variable with other commands. For example, suppose you had this INPUT command:
INPUT "Enter your age > ", age
This would prompt the user for their age, then record their age as age. You could then do something like this:
PRINT "You're "; age; " years old!"
By the way, this is a good time to think about spacing. Notice that there is a space between the e in "You're" and the quote mark that follows it. The reason for this you may be able to guess: It's to put a space between the word "You're" and the actual age displayed on the screen. The same goes for "years". There's a space between the opening quote mark and the letter y. The actual variable age would not contain spaces (unless the user typed spaces at the beginning and end of their age when they typed it, which would be unlikely), so without putting those spaces in, it wouldn't be spaced properly.
The REM Command:The REM (which stands for "remark") command simply puts comments into the program code. They serve no purpose whatsoever to the running of the program and will be ignored. They are just there to put comments in the program so you can say specific things to anyone who's looking through the program listing. For example, at the very beginnning of a program, you might add a REM line (or several of them) to say the name of the program, the author of it (in this case, you), and the program's purpose. Something like this, possibly:
REM Space Blasters, by Jeff Wooster
REM This extremely useful program helps
REM blow up lots of little alien spaceships.
Math in BASIC can be done as follows:
varname = equation
Here's a full-program example:
REM Program to add together two input numbers INPUT "Enter a nice number for me! > ", n% INPUT "Enter another nice number! > ", m% o% = n% + m% PRINT "Those two numbers added equal "; o%; "!"
Just study this for a while, and it should become pretty clear.
This is a good time to mention variable TYPES. Notice that the variables used in the above program all end in a percent sign (%). In BASIC, variables are supposed to end in some sort of symbol which signifies what kind of variable it is, exactly. (In previous examples on this Web page, no such symbols were included, but that was for simplicity. Now that you're a more advanced BASIC programmer, you can learn about this.)
There are several different types of variables, but for now, it's enough to know just these two:
$ text string
In the above program, we created n% and m%, which were number variables. What might have happened if, upon running this program, we tried to enter something that wasn't a number (some sort of text, for instance)? Well, what would have happened is you would've gotten a message saying Redo from start. That means the input you just supplied was not accepted by the computer, because it was of the wrong type.
So why bother with % variables at all? Why not just make everything a $, which will accept virtually any input you give it? Because if we modified the program so that n% became n$, m% became m$, and o% became o$, the program would, instead of mathematically adding the numbers you give it, simply put them together! To illustrate just what I'm talking about, suppose you entered 2 and 3. That equals 5, of course, when added. The program works fine with all the variables as % types, but when you make them $ types, you get 23 instead!
This leads me to adding text. Suppose you used INPUT to get someone's first name, and then their last name, and you wanted to make their full name. You might make a program something like this:
INPUT "Please type your first name > ", firstname$ INPUT "Please type your last name > ", lastname$ fullname$ = firstname$ + " " + lastname$ PRINT "Your full name is "; fullname$
(The two quote marks in the middle of the third line have a space between them. As you might guess, they serve to add a space between the first and the last name.)
Easy, isn't it? And yet, I'm sure you can already think of stuff to do with what you've learned so far.
The GOTO, GOSUB and RETURN commands
GOTO is a command used to move to a particular part of the BASIC program. It can go to either a line number or a label.
What's a line number? Well, sit by the fire and let me tell you a story about the ol' BASIC programmers from the days when you were jus' a li'l one. In the olden days (before computers ran on electricity), all BASIC programs needed a number at the beginning of each line. Every line had a number, and every number needed to be different from every other one.
It was typical to go by tens when numbering your lines. For example, a program would look like this:
10 CLS 20 PRINT "Hello, there." 30 END
This was so you could add lines in between without having to re-number the lines to the whole program. For example, if you wanted to make the above program print a second line, you could number that line 25 without having the re-number the last one (the END one).
No modern BASIC compiler needs line numbers, but any should still accept them if you put them in. The format for GOTO goes like this:
destination is wherever you want to go. So for example, you could make a program like this:
10 CLS 20 PRINT "Hello, there." 30 GOTO 20
This will print "Hello, there." forever. Note that this program never stops. It will keep scrolling the words until you abort the program. (In QBASIC, press CTRL-BREAK to do this. The BREAK key should be right next to SCROLL LOCK, in the upper-right.)
What's a label? Well, here's a program to demonstrate:
CLS routine: PRINT "Hello, there." GOTO routine
This program does exactly the same thing as the last one. However, it does it with a label instead of a line number. I'm sure you can see how it works: You define the label by simply putting its name alone on a line, followed by a colon. Then you can GOTO it.
And what's GOSUB? It's a variation on GOTO. Here's an example:
CLS GOSUB routine END routine: PRINT "Hello, there." RETURN
Again, you can see just how it works: GOSUB goes to either a line number or label just like GOTO, but you can use the RETURN command to go BACK to where you did the GOSUB. This is useful for sub-routines which you want to call without abandoning the rest of the program.
Note that the RETURN is not optional. Every GOSUB must have a companion RETURN.
So far, this has only been the basics of BASIC. If you want to get started writing your own programs right now, try typing QBASIC at your computer's command prompt and see what happens. MS-DOS comes with QBASIC, which is a good program for writing in BASIC. Unfortunately, it doesn't come with Windows 95. Microsoft apparently thought it wasn't that useful. If you upgraded to Windows 95 from MS-DOS, you should still have QBASIC, but if 95 is your first operating system (i.e. your computer came with it instead of having you UPGRADING to it), you'll have to buy a third-party BASIC interpreter program somewhere.
Sample BASIC Programs
I was looking at this site the other day and was struck by how it needed more example programs. So, here are some. They might still need a little formatting but overall they seem to work.
Flash monitor pixels:
REM Flash monitor pixels. CLS SCREEN 12 TOP: PSET (320, 240), m PSET (639, 479), m PSET (0, 479), m PSET (639, 0), m PSET (0, 0), m SLEEP 1 m = m XOR 15 GOTO TOP
Draw a green square on the screen. A good example of how to use FOR/NEXT for repititive tasks:
REM Draw a green square. SCREEN 9 FOR A = 1 TO 100 FOR B = 1 TO 100 PSET (A, B), 10 NEXT NEXT SYSTEM
Demonstrate how GET/PUT work:
REM First draw the green square on the screen, then capture it to AR REM using GET. DIM AR(1313) SCREEN 9 FOR A = 1 TO 100 FOR B = 1 TO 100 PSET (A, B), 10 NEXT NEXT GET (0, 0)-(100, 100), AR CLS PRINT "The green square is in AR. Now we'll re-draw it using PUT." SHELL "pause" PUT (0, 0), AR SYSTEM
Demonstrate how DIM works:
DIM S(7) REM That makes an array called S, with 7 subscripts in it. S(1) = 10 REM That sets the first subscript of S to 10. PRINT S(1) REM That prints the first subscript of S.
Further demonstrate how DIM works:
DIM S(7): REM That makes an array called S, with 7 subscripts in it. S(1) = 10: REM That sets the first subscript of S to 10. S(2) = 20 PRINT S(1): REM That prints the first subscript of S. PRINT S(2)
Even more demonstration of how DIM works:
V = 1 W = 2 DIM S(7): REM That makes an array called S, with 7 subscripts in it. S(V) = 10: REM That sets the first subscript of S to 10 (V is 1). S(W) = 20 PRINT S(1): REM That prints the first subscript of S. PRINT S(2)
Draw a sine wave: SINEWAVE.BAS:
DIM AR(101) SCREEN 12 FOR A = 1 TO 10 FOR B = 1 TO 10 PSET (A, B), 10 NEXT NEXT REM The above lines draw a small green box in the upper-left corner. GET (0, 0)-(10, 10), AR CLS REM The above two lines capture the green box into an array called AR REM and clear the screen. REM Below is the fun part: The actual drawing of the sine wave using REM the green box in AR. FOR i = 620 TO 0 STEP -1 REM The first number is the right end of the line. REM The second number is the left end of the line. REM This STEP number controls the spacing between the plots of the REM square. REM If you make it -20, you'll be able to see spaces between the REM squares REM and it won't be a solid line anymore. PUT (i, 240 - (SIN(i / 100) * 225)), AR REM The 240 is the vertical position on the screen where the left end REM of the sine wave begins. REM The 100 is the horizontal "tightness" of the sine wave. REM The 225 is the height of the sine wave. NEXT SYSTEM
Spaceship Simulator. Use arrow keys to move around, press q to quit. This demonstrates use of the arrow keys in BASIC:
x = 13 y = 39 position: CLS LOCATE x, y: PRINT "è" getkey: a$ = INKEY$: IF a$ = "" GOTO getkey IF a$ = "q" THEN SYSTEM IF a$ = CHR$(0) + "H" THEN x = x - 1: IF x <= 1 THEN x = 1: REM This is the up arrow key! IF a$ = CHR$(0) + "P" THEN x = x + 1: IF x > 24 THEN x = 24: REM This is the down arrow key! IF a$ = CHR$(0) + "K" THEN y = y - 1: IF y <= 1 THEN y = 1: REM This is the left arrow key! IF a$ = CHR$(0) + "M" THEN y = y + 1: IF y > 80 THEN y = 80: REM This is the right arrow key! GOTO position
Parody Of The "Zork" Games. This game Takes Place In Zurk. Quite Simple, but humorous:
CLS PRINT "The light of the sun slowly filters through your window. Your" PRINT "alarm clock FRRRRRTTTZZZTZAATTS its alarm, and you" PRINT "wake up." PRINT PRINT "Another beautiful day in Zurk." PRINT PRINT "Gosh, it's COLD in here! And no wonder! The furnace is out PRINT "of gas!" PRINT "Shoot. What do you want to do now?" PRINT PRINT "1. Call Petro Zurkada and order more." PRINT PRINT "2. Bash your furnace with a sledgehammer until it starts again PRINT "for no reason." PRINT PRINT "3. Feed it some pizza with quintiple anchovies." PRINT PRINT "What's your choice?" PRINT 10 A$ = INKEY$: IF A$ = "" GOTO 10 IF A$ = "1" GOTO one IF A$ = "2" GOTO two IF A$ = "3" GOTO three GOTO 10 one: PRINT "There's no such thing as Petro Zurkada. Sorry, you lose!" GOTO ender two: PRINT "POW! POW! POW! You beat your furnace silly!" PRINT "Press any key to continue..." PRINT SHELL "pause" PRINT "You break your furnace. The end." GOTO ender three: PRINT "Your furnace very quickly gets A LOT of gas. It heats up your" PRINT "home rapidly, and you and your furnace live happily ever after." PRINT "The end." GOTO ender ender: SYSTEM
CLS : SCREEN 12 RANDOMIZE TIMER DrawLoop1: x% = (RND * 750) + 1 y% = (RND * 500) + 1 RESTORE FOR j% = 1 TO 7 READ k% c% = (RND * 15) + 1 CIRCLE (x%, y%), k%, c% FOR t = 1 TO 250: NEXT NEXT FOR t = 1 TO 750: NEXT Z = Z + 1: IF Z = 35 THEN Z = 0: CLS REM In the previous line, the number 35 specifies how many circle REM patterns should be drawn before the screen clears and starts over. REM Change the number if you feel the screen clears too soon or not REM soon enough. A$ = INKEY$ IF A$ = "" GOTO DrawLoop1 CLS : SYSTEM DATA 1, 5, 10, 15, 20, 25 DATA 30
Another Screen Saver:
CLS : SCREEN 9 RANDOMIZE TIMER DrawLoop1: x1% = RND * 639 y1% = RND * 349 x2% = RND * 639 y2% = RND * 349 c% = RND * 15 LINE (x1%, y1%)-(x2%, y2%), c% Counter = Counter + 1 IF Counter = 1000 THEN CLS (0): Counter = 0 REM In the previous line, the number 1000 specifies how many lines REM should be drawn before the screen clears and starts over. Change REM the number if you feel the screen clears too soon or not soon REM enough. a$ = INKEY$ IF a$ = "" GOTO DrawLoop1 SYSTEM
Pythagorean Theorem program
REM Pythagorean Theorem program REM Calculates the length of the third side of a right triangle when REM two sides are already known. Useful for some basic trigonometry. START: CLS PRINT "Do you know the hypotenuse? Y/N" 10 A$ = INKEY$: IF A$ = "" THEN GOTO 10 IF UCASE$(A$) = "Y" THEN GOTO 20 IF UCASE$(A$) <> "N" THEN GOTO 10 PRINT PRINT "Enter the lengths of the two known" INPUT "sides, separated by a comma> "; X, Y Z = SQR(X * X + Y * Y) PRINT PRINT "The hypotenuse is "; Z: GOTO ASKAGAIN 20 PRINT INPUT "Enter the hypotenuse length> "; Z INPUT "Enter the length of the other known side> "; X Y = SQR(Z * Z - X * X) PRINT PRINT "The length of the third side is "; Y ASKAGAIN: PRINT PRINT "Do you want to do another calculation? Y/N" 30 A$ = INKEY$: IF A$ = "" THEN GOTO 30 IF UCASE$(A$) = "N" THEN SYSTEM IF UCASE$(A$) <> "Y" THEN GOTO 30 GOTO START
Shows exactly how FOR and NEXT work:
CLS FOR a% = 1 TO 10 PRINT a% NEXT a%
Shows how STEP works with FOR:
CLS FOR a% = 7 TO -6 STEP -3 PRINT a% NEXT a%
REM Counts from 1 onward, until overload occurs at 32767 CLS j% = 0 again: a% = j% + 1 b% = a% + 1 c% = b% + 1 d% = c% + 1 e% = d% + 1 f% = e% + 1 g% = f% + 1 h% = g% + 1 i% = h% + 1 j% = i% + 1 PRINT a%; " "; b%; " "; c%; " "; d%; " "; e%; " "; f%; " "; g%; " "; h%; " "; i%; " "; j% GOTO again
Counts powers of 2 until overflow occurs. This happens pretty fast even though it uses a long integer (that's why it pauses for keypress after each number):
CLS A& = 1 mainloop: A& = A& * 2 PRINT A& 10 A$ = INKEY$: IF A$ = "" GOTO 10 GOTO mainloop
Makes a text file with all the numbers from 1 to 1000:
OPEN "1000.TXT" FOR OUTPUT AS #1 FOR A% = 1 TO 1000 WRITE #1, A% NEXT A% CLOSE #1
Back to the main page