tutes-dump/dump/basic-language.html.docuwiki

118 lines
7.4 KiB
Plaintext

===== BASIC =====
=== The basics of BASIC ===
BASIC, which stands for **B**eginners **A**ll-Purpose **S**ymbolic **I**nstruction **C**ode is a programming language which was created at Dartmouth University by John Kemeny and Thomas Kurtz in the early 1960's. BASIC's authors intended to create a programming language and environment which would be useful to people who's primary interest was something other than computer programming. It was intended to be used by average users, scientists, and programming students alike. BASIC was instantly popular among the students and faculty of Dartmouth, and it saw rapid adoption on other systems. When the microcomputer revolution started to take hold in the 1970's and 1980's, BASIC was the default user environment of choice. As a result, many of today's programmers cut their teeth on BASIC. This tutorial will help you get started with this wonderful little language. BASIC remains a very easy to learn language, and it is great for writing small programs. Best of all, a lot of the concepts in BASIC transfer to other languages, so this can serve as a great jumping off point for your programming career!
One of the difficulties in learning BASIC lies in the wide array of BASIC compilers and interpreters that are currently in circulation. Each one of these programs comes with their own nuances and extensions to the BASIC programming language. The good news is that there is a lot of commonality between the various versions of BASIC, and once you've gained familiarity with one you can move to another pretty quickly. Best of all, there is a common thread which runs through all the various BASICs, and that is where we will begin. We'll start with a minimal subset of the language, one which will get you up and running with all the interpreters available on SDF.
Let's start with a simple working example. A classic one which children the world over have typed into computers since the dawn of the 80's!
10 PRINT "HELLO"
20 GOTO 10
If this is run through a BASIC interpreter, it will have the following output:
HELLO
HELLO
HELLO
HELLO
.... and so on off into infinity
In fact, why don't we try this out in one of our several BASIC interpreters! Go ahead and fire up bwbasic. To do this, just type "bwbasic" at the sdf prompt. Type in the program listed above, and then type RUN and press enter. Once you've been greeted enough times, press Ctrl-C to halt the program. The hole session should look like this:
######## ## ## ## ## ### ######## ######## ########
## ## ## ## ## ## ## ## ## ## ## ## ##
## ## #### ## ## ## ## ## ## ## ## ##
######## ## ## ## ## ## ## ## ###### ########
## ## ## ## ## ## ######### ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ##
######## ## ### ### ## ## ## ######## ## ##
######## ### ###### #### ######
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ##
######## ## ## ###### ## ##
## ## ######### ## ## ##
## ## ## ## ## ## ## ## ##
######## ## ## ###### #### ######
Bywater BASIC Interpreter, version 3.00
Copyright (c) 1993, Ted A. Campbell
Copyright (c) 1995-1997, Jon B. Volkoff
Copyright (c) 2014-2015, Howard Wulf, AF5NE
bwBASIC: 10 PRINT "HELLO"
bwBASIC: 20 GOTO 10
bwBASIC: RUN
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
Program interrupted at line 10
bwBASIC:
To exit bwbasic, just type "quit" and press enter.
=== Structure of BASIC Lines of Code ===
Ok, so let's unpack what we just did a little bit. The basic layout of a BASIC statement works like this:
LINE_NUMBER COMMAND PARAMETERS
Each of these parts serves a specific purpose.
* LINE_NUMBER - Present in most BASICS. This serves as a label for the line so you can refer to it later, and in some BASICs it also serves as a method of editing lines. (more on this later!)
* COMMAND - This is what you want your program to do. There are lots of these, we'll discuss them in detail later.
* PARAMETERS - Most commands require some additional information. That's what this one is about.
So now with that in mind, let's take a look at our happy little program! The first line
10 PRINT "HELLO"
is line number 10. The command that we are using is "PRINT", and the parameter is "HELLO". PRINT puts information on the screen, and "HELLO" is that something we are putting on the screen. Pretty simple, right?
Now, what about the next line?
20 GOTO 10
GOTO is a command which tells them computer what line to execute next. Normally, the computer will just step through the lines of the program in order, but when it hits a GOTO, that's your chance to send it somewhere else. Here, we are sending it back to line 10. This causes it to print out "HELLO" again, then it hits line 20 again, and then it goes back to 10. This will go on and on forever, or until someone stops the program. Computers are very patient, so you can run this to your heart's content!
=== The BASIC Line Editor ===
Before we go any further with the language, we should explore what's going on with those line numbers. The answer lies in BASIC's origins. Originally, BASIC was run on a GE-235 running Dartmouth's Time Sharing System. The primary mode of interface was a teletype printer (this was the 1960's after all!) so everything was constantly being typed out to paper. This meant that modern text editing just wasn't available to BASIC programmers. To combat this, Kurtz and Kemeny implemented a line based editing scheme. The line numbers were the key to this.
First and foremost, these line numbers provide a guide for sequencing the program. The computer sorts them into increasing order. They need not be consecutive, and traditionally BASIC programmers would count by 10's or by 100's. The reason for this is that you could go back and insert new lines of code in between lines of code. Imagine, for instance, that you had entered our little program, but then decided you wanted a second line of output that said "GOODBYE". You could type the whole thing over again, or you could just type the following:
15 PRINT "GOODBYE"
Now, we can take a look at what the computer thinks the program is by using the LIST command:
LIST
10 PRINT "HELLO"
15 PRINT "GOODBYE"
20 GOTO 10
You can also use the line number to change the lines:
15 PRINT ":-D"
Or you can delete lines all together
15
So in short, these line numbers are both your labels in your program, and they are also a means to edit your program. Of course, I should mention that not all BASIC interpreters support this sort of direct editing. In those cases, you'll want to edit your program in a standard text editor (like ed!) [ok, or vi or emacs]. But for that old-school feel, you'll want to use line number editing.
=== Available BASIC Interpreters at SDF ===
* bwbasic
* yabasic
* brandy
* .. //This is exactly how to use BASIC at SDF with a plethora of fun and interesting programming examples just for you!//$Id: basic-language.html,v 1.4 2016/08/11 02:11:08 pngwen Exp $