pifty
/
tutes-dump
Archived
1
1
Fork 1
This repository has been archived on 2020-07-15. You can view files and clone it, but cannot push or open issues or pull requests.
tutes-dump/site-tutorials/basic-language.html

206 lines
8.2 KiB
HTML

<h2>BASIC</h2>
<h4>The basics of BASIC</h4>
<p>
BASIC, which stands
for <b>B</b>eginners <b>A</b>ll-Purpose <b>S</b>ymbolic <b>I</b>nstruction <b>C</b>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!
</p>
<p>
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.
</p>
<p>
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!
</p>
<pre>
10 PRINT "HELLO"
20 GOTO 10
</pre>
<p>
If this is run through a BASIC interpreter, it will have the
following output:
</p>
<pre>
HELLO
HELLO
HELLO
HELLO
.... and so on off into infinity
</pre>
<p>
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:
</p>
<pre>
######## ## ## ## ## ### ######## ######## ########
## ## ## ## ## ## ## ## ## ## ## ## ##
## ## #### ## ## ## ## ## ## ## ## ##
######## ## ## ## ## ## ## ## ###### ########
## ## ## ## ## ## ######### ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ##
######## ## ### ### ## ## ## ######## ## ##
######## ### ###### #### ######
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ##
######## ## ## ###### ## ##
## ## ######### ## ## ##
## ## ## ## ## ## ## ## ##
######## ## ## ###### #### ######
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:
</pre>
<p>To exit bwbasic, just type "quit" and press enter.</p>
<h4>Structure of BASIC Lines of Code</h4>
<p>
Ok, so let's unpack what we just did a little bit. The basic layout
of a BASIC statement works like this:
<pre>LINE_NUMBER COMMAND PARAMETERS</pre>
Each of these parts serves a specific purpose.
<ul>
<li>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!)
<li>COMMAND - This is what you want your program to do. There are
lots of these, we'll discuss them in detail later.
<li>PARAMETERS - Most commands require some additional
information. That's what this one is about.
</ul>
</p>
<p>
So now with that in mind, let's take a look at our happy little
program! The first line <pre>10 PRINT "HELLO"</pre> 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?
</p>
<p>
Now, what about the next line? <pre>20 GOTO 10</pre> 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!
</p>
<h4>The BASIC Line Editor</h4>
<p>
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.
</p>
<p>
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:
<pre>
15 PRINT "GOODBYE"
</pre>
Now, we can take a look at what the computer thinks the program is
by using the LIST command:
<pre>
LIST
10 PRINT "HELLO"
15 PRINT "GOODBYE"
20 GOTO 10
</pre>
You can also use the line number to change the lines:
<pre>
15 PRINT ":-D"
</pre>
Or you can delete lines all together
<pre>
15
</pre>
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.
</p>
<h4>Available BASIC Interpreters at SDF</h4>
<li>bwbasic
<li>yabasic
<li>brandy
<li>..
<p>
<i>This is exactly how to use BASIC at SDF with a plethora of fun and interesting programming examples just for you!</i>
<p>
$Id: basic-language.html,v 1.4 2016/08/11 02:11:08 pngwen Exp $