Programming in BASIC
Reading Time: 7 minutes
A program is a set of instructions for the computer. These instructions tell the computer precisely what to do, and the computer always follows these instructions.
Just like people, computers can only follow instructions if specified in a language they can understand. These are called programming languages and BASIC is one such programming language.
Components of a BASIC Program
Every BASIC program comprises of one or more statements. Statements are similar to sentences we use everyday, but with a clear and precise meaning.
Each statement builds upon two things: information and instruction. We give information to the computer along with an instruction on what to do with that information.
Using these simple concepts of information and instructions, we can build any system on Earth, from calculator to a Robot!
Information
Before we can go any further, however, we need to classify each type of information, so that computer can correctly interpret the thing we actually meant. Type of an information is very important in computer science.
In BASIC, we classify information into three types: text, number and boolean. BASIC must be precisely instructed whether we meant a text, a number or a boolean information:
- Text is always quoted in
"". E.g.,"Indian","one","42"are treated as texts. - Number is given as it is. E.g.,
42,132,429are treated as numbers. - Boolean is a special case that can store
TRUEorFALSEonly.
- Always put text within quotes.
"Hi! Human"is valid, butHi! Humanis not. - Always put numbers without quotes.
42is number , but"42"is text.
But how do we store these information in computer? In BASIC, we usually store these information using variables. Variables are nothing but placeholders for actual information. It is actually the same thing we use in Algebra.
For example, in the below BASIC program, we instruct computer to store three numbers in computer memory and name it A, B, C. In future, when we need to use these numbers, we directly use those variables instead of the numbers itself.
A = 2
B = 3
C = 5
S = A + B + C
In the last line, we tell the computer to sum the values 2, 3, 5 using the variables A, B, C that are stored in computer memory. Computer, in turn, will replace all the variables with the actual numbers; add them; and finally store the sum in S.
This is exactly similar to how we do math using variables in algebra.
REMEMBER:
Information is also produced when an expression gets evaluated by BASIC.
- Math expressions produce a number. E.g.,
2 + 5, will produce7 - Relational expressions produce boolean. E.g.,
2 > 5will produceFALSE. - Text expressions will produce a new text. E.g.,
"Vedic " + "Science"produces"Vedic Science".
Instructions
Information along is not enough. We need to instruct computer on what to do with those information. BASIC provides many types of instructions to operate on a given information. The most important are:
- Math Instructions
- Input/Output Instructions
- Branching Instructions
- Looping Instructions
Math Instructions
BASIC supports almost all the mathematical operations that Algebra I supports, including some new operators:
- We use
=operator to store information in RAM. E.g.,A = 42, will store 42 in RAM. - We use
+,-,*,/,MODto do arithmetic operations. E.g.,A = 40 + 2, will store 42 in RAM. - We use
<,>,=,<>,AND,ORto do relational operations. E.g.,A = 42 > 5, will storeTRUEin RAM.
Below program shows an example of some of the mathematical instructions.
A = 2 'Store 2 in memory as A.
B = 3 'Store 3 in memory as B.
C = 4 'Store 4 in memory as C.
D = 5 'Store 5 in memory as D.
N = A + B 'Store 5 in memory as N. Since, 2 + 3 is 5.
O = D - 4 'Store 1 in memory as O. Since, 5 - 4 is 1.
P = B * 3 'Store 9 in memory as P. Since, 3 * 3 is 9.
Q = D / A 'Store 2 in memory as Q. Since, 5 / 2 has quotient 2.
R = D MOD A 'Store 1 in memory as R. Since, 5 / 2 has remainder 1.
S = A < C 'Store TRUE in memory as S. Since, 2 < 4 is TRUE.
T = N >= D 'Store TRUE in memory as T. Since, 5 ≥ 5 is TRUE.
U = N <> D 'Store FALSE in memory as U. Since, 5 ≠ 5 is FALSE.
Input/Output Instructions
BASIC provides two instructions for input/output information to users.
INPUT Instruction
BASIC can take input from user using the INPUT instruction. It takes two information with it:
- Firstly, it takes a text message to be shown to the user, when they enter the information.
- Secondly, it takes a list of variables, where information given by the user will be stored.
INPUT "Enter name and height: ", N, H
For example, in the above program, INPUT instruction shows a message Enter name and height: to the user. After that, when user enters the values, the information will get stored in N and H variables.
PRINT Instruction
BASIC can print information on the screen using the PRINT instruction. It takes a list of information, to be printed, separated by a comma. The information could be given directly or using variables.
N = "Abhimanyu"
H = 70
PRINT "Name, Age, Height: ", N, 16, H
For example, in the above program, PRINT instruction will print Name, Age, Height: Abhimanyu 16 70 on the screen.
- The 1st and 3rd information,
"Name, Age, Height: "and16is provided directly toPRINTinstruction. - The 2nd and 4th information,
NandHis provided toPRINTinstruction using variables.
- As
"Enter name and height: "is a text, it must be enclosed in quotes, otherwise BASIC won’t understand it. - It, however, does not include the quotes when printing text, since we can understand it without quotes.
Combining INPUT and PRINT
In real-life programs, INPUT and PRINT work hand-in-hand. INPUT instruction takes information from user, then some operation is performed on the information and then the final result is shown to the user.
INPUT "Enter your name: ", NAME
INPUT "Enter your age: ", AGE
PRINT "Your name is: ", NAME
PRINT "Your age is: ", AGE
In the above program, BASIC takes name and age from user and prints it back. Probably, the shortest program one could ever write!
Branching Instructions
In general, BASIC takes a series of instructions and executes them one-by-one sequentially. In many situations, we may need to execute selected statements based on a condition.
Branching statements help us to skip a bunch of statements based on YES and NO conditions.
INPUT "Enter age:", A
IF A < 18
PRINT "The person is minor."
PRINT "You can watch cartoons!"
ELSE
PRINT "The person is adult."
PRINT "You can watch TV serials!"
END IF
PRINT "Happy Watching!"
For example, in the above program,
IFa user enters any value less than18,THENthe program will print:The person is a minor.You can watch cartoons!
ELSEthe program will printThe person is an adult.You can watch TV serials!
Finally, the program will print Happy Watching!. Instead of printing all the five PRINT instructions, BASIC selectively prints the information based on IF condition.
As like all other instructions, IF also takes an information with it. The information, however, must evaluate to TRUE or FALSE value instead of a number or text value. That is, IF can only take relational expressions as information with it.
Looping Instructions
Coming soon...