Chapter 1: Introduction to BASIC
Programming
Have you
ever, when seeing a game or another program, asked yourself : How did they do
that?
Every
program, every game you run on your computer, has been written by someone using
a special software development tool called a programming language.
By
definition: A program is
a set of instructions that makes the computer work. The
instructions constitute of keywords, sometimes called reserved words, that have
special functions such as:
- Clearing the screen
- Writing data to the screen
- Getting user input
- Executing loops
- Changing colours
- Playing sounds
- Opening files
- Etc.
There are
different programming languages. Some are difficult to use, understand, other
are limited to special purposes such as scientific calculations, drawing or
database management.
Some
programming languages out there:
The Qbasic IDE
The Qbasic IDE
You should type your instructions in the untitled window.
To run your program, click on Run -> Start or Press F5
To get out of Qbasic, click on File -> Exit.
We'll see more about the Qbasic IDE later.
Two BASIC reserved words
It is not necessary for you to know all the BASIC reserved words to get started. You could write a lot of programs using about 10 keywords. To get started we'll be using only two of them:
CLS
|
This statement is used to clear the screen.
|
PRINT
|
The PRINT statement is used to display data on the screen.
|
Your First BASIC program
To begin we will write a simple program that will output a text on the screen.Type the following in the untitled window:
PRINT "Hello!"
PRINT "This is my first BASIC program." |
Click on Run � Start or Press the function key F5 |
Hello!
This is my first program. Press any key to continue |
If you run your program many times, you will notice that the same text will be displayed several times on the screen as shown below:
Hello!
This is my first program. Hello! This is my first program. Hello! This is my first program. Hello! This is my first program. Press any key to continue |
CLS
PRINT "Hello!" PRINT "This is my first BASIC program." |
Mathematics symbols
Some mathematical operators and their BASIC equivalent:
Operation
|
Mathematics
|
BASIC equivalent
|
Plus
|
+
|
+
|
Minus
|
-
|
-
|
Multiply
|
x
|
*
|
Divide
|
�
|
/
|
Exponential
|
a2
|
a^2
|
Storing data in the computer memory
Data types: Constants and
Variables
You may need to store data in the computer when writing your program. Data is classified into 2 types:
- Numbers
- Character Strings
10 +5
0.32 -1.234
|
"Hello! how are you?" "John
Smith" "14 Royal Road"
"$123"
|
To store data in the memory you will need to put them first in memory variables or constants.
A memory variable will store values that vary, change.
A memory constant will store values that remain constant (i.e. they don't change).
The Computer Memory
The memory of the human being contains millions of cells that store the different information that you have acquired in your life. Similarly the computer memory could be represented as storage cells. Each cell can be given a name or an address as follows:
Value
|
10
|
+5
|
0.32
|
-1.234
|
name of cell
|
a
|
b
|
c
|
d
|
Value
|
Hello! How are you
|
John Smith
|
14 Royal Road
|
$123
|
name of cell
|
a$
|
b$
|
c$
|
d$
|
Declaring variables
BASIC has received many critics because of its ability of creating & declaring variables on the fly. Qbasic allow you to declare variables.The DIM statement is used to specify a data type for a variable. Variables declared should not end with a suffix.
The syntax is as follows:
DIM variable AS type
Example:
DIM userrname AS STRING
DIM age AS INTEGER
username
= "John Smith"
age
= 23
|
username$
= "John Smith"
age%
= 23
|
Personally, though you might encounter both ways in the tutorial, I prefer to use the first one. It's a good habit to declare every variable you use.
No comments:
Post a Comment