BASIC
From IT Jalakam - Unofficial IT syllabus for grade 10 students
BASIC (an acronym for Beginner's All-purpose Symbolic Instruction Code) is a family of high-level programming languages. The original BASIC was designed in 1964 by John George Kemeny and Thomas Eugene Kurtz at Dartmouth in New Hampshire, USA to provide computer access to non-science students. At the time, nearly all use of computers required writing custom software, which was something only scientists and mathematicians tended to be able to do. The language and its variants became widespread on microcomputers in the late 1970s and 1980s. BASIC remains popular to this day in a handful of highly modified dialects and new languages influenced by BASIC such as Microsoft Visual Basic. As of 2006, 59% of developers for the .NET platform used Visual Basic as their only language. Although the classical BASIC is now considered to be largely antiquated, however it is still widely used as as educational tool for beginner programmers due to its ease and simplicity to use for small programs. The BASIC programming language however has always been considered inadequete and too cumbersome for larger programs.
Contents |
Introduction
Let's kick start into BASIC programming by using the ever famous "Hello World" Program.
PROGRAM
10 CLS 20 PRINT "HELLO WORLD" 30 END
Now run this program into the BASIC Interpreter and see the results.
OUTPUT
HELLO WORLD
Let me explain. A BASIC Program is a simple text file with some textual data in form of BASIC instructions written in a simple text editor. Every statement is BASIC is preceded by line numbers (in few interpreters, the interpreter adds them automatically when programmer doesn't; however in BASIC it's considered a practice to use line numbers). You can use line numbers in any range, but proper order of course. It's recommended to put line numbers in multiples of 10 so that you can you can later add lines beetween the statements. First Let us analyze the first statement.
10 CLS
This statement will clear the screen. It's Optional.
The next statement is
20 PRINT "HELLO WORLD"
This statement will print a line "HELLO WORLD" into the console. As you would have guessed the PRINT statement would print any text content on the console. You can say PRINT "INDIA", PRINT "LINUX", PRINT "ANYTHING", etc. whatever you wish. I supposed you have realised this goes the first step in interaction in the user in form of output.
And finally
30 END
This will terminate the program. Kindly note tha usage of END at the end of the text source is optional as at that time, the program will terminate anyway. END can be used to terminate program at any time as we will see later.
Variables
Before learning how to get input first, We have to see what a variable is. A variable is a symbolic name associated with a value and whose associated value may be changed anytime during program execution. We use the LET statement to create a variable.
<Line number> LET variablename = <value>
For instance we want to store value 10 in a variable call tenvar.
10 LET tenvar = 10
And thus the value 10 will be stored into the variable tenvar.
Often the LET statement is redundant and you can declare variable without this statement.
10 tenvar = 10
Numbers and Strings
In BASIC, there are basically two types of data types, Numbers and Strings. Numbers are umm... obviously numbers like 123, 456, 3.14, etc. Numbers can be declared simply with LET statement. They need no specific mention as such that they are numbers.
Strings on the other hand is an alphanumeric array of characters such as abc, xyz, pqr, a12, etc. Please note in order to specify strings, they must be enclosed in double quotes(") like "abc", "zyz", etc. Please note "123" is a string not a number due to the fact that 123 is enclosed by double quotes.
In order to declare string, we can use the LET statement, however, the variable has to be suceeded i.e. followed by a $ character. For example:
10 LET thisstr$ = "Hello World"
Please note that without the dollar the variable cannot be assigned to a string value.
10 LET thisstr = "Hello World"
This statement will issue an error.
Printing Variables
The PRINT statement can simply be used in order to output the value of the variables onto the console. For example:
10 LET var = 10 20 PRINT var
This will simply output the value of the variable var to the console.
Strings can also be printed using this method.
10 LET str$ = "Hello World" 20 PRINT str$
Remember the Hello World Program? This program will do the same. I guess you have realised by now that by now, in that example that we were basically printing a string in that case as well, difference being, in that case that string wasn't stored.
Printing More than one variable at a time
You can use Comma(,) or Semicolon(;) within variables in order to print more than one variable at a time. For Example:
10 LET abc = 15 20 LET xyz = 45 30 LET qv$ = "Hello World" 40 PRINT abc, xyz, qv$ 50 PRINT abc;xyz;qv$
Kindly see the output to find out the difference between the Comma(,) and Semicolon(;).
This trick can also be used to give verbosity to the program. For instance, instead of just printing the value of the variable -> 35, you would like to give the output -> The value is 35. You can just do.
10 LET var = 35 20 PRINT "THE VALUE IS", var
Getting Input
We use the INPUT statement to get input from the keyboard. When we give INPUT command, the program prompts the user for input, such that the value user inputs into the keyboard is stored into the variable.
Getting input is fairly easy you just have to follow this syntex:
<Line number> INPUT "Text to display on Screen";variablename
For Example :
If you write this program :
10 INPUT "Please Enter a Number :";no 20 PRINT "You entered ";no
The Output Will Be
Please Enter a Number :
If you enter 25 then it will display
You Entered 25
That is the program will prompt the user for input, and as the user will input 25, the BASIC interpreter will store the value 25 into the variable no and thus variable no will have value of 25.
Similarly we can input Strings as well.
10 INPUT "Please Enter a String :";str1$ 20 PRINT "You entered ";str1$
Keep in note the difference between the Comma(,) and the Semicolon(;) applies in the INPUT statement just as in PRINT.
Example Program
Let use write a program that will accept Name of the person, number of matches he/she has played, and number of goals scored. Using the data the program will calculate goal/game ratio and will print the name and the goal/game ratio.
REM PROGRAM TO DEMONSTRATE INPUT AND OTHER STUFF 10 CLS 20 INPUT "ENTER NAME: ", name$ 30 INPUT "ENTER MATCHES PLAYED: ", nummatches 40 INPUT "ENTER GOALS SCORED: ", numgoals 50 LET ratio= numgoals/nummatches 60 PRINT 'PRINT A BLANK LINE 70 PRINT "NAME: "; name$ 80 PRINT "MATCHES: "; nummatches 90 PRINT "GOALS: "; numgoals 100 PRINT "GOAL PER GAME RATIO: "; ratio 110 END
OUTPUT
ENTER NAME: ABC ENTER MATCHES PLAYED: 22 ENTER GOALS SCORED: 11 NAME: ABC MATCHES: 22 GOALS: 11 GOAL PER GAME RATIO: 0.5
Comments
Notice the REM statement in the beginning and the statement 60 PRINT 'PRINT A BLANK LINE. Anything followed by REM or by Single Quote(') is a comment or in other words text that the user uses to describe the program. For example in 60 PRINT 'PRINT A BLANK LINE, after ('), we describe what a simple print statement does. Use of Comments is always encouraged in the code. You may be smart now to know what code is doing now, but not someone else who may look into your code or you few weeks later. This is particularly true in larger programs.
Operators and Arithmetic
Basic was obviously created for us to have fun, play games, draw nice graphics and even make sounds. But one of the which are part and parcel of programming language is arithmetic using operators. Don't worry if you don't like Maths, BASIC would do that for you, you just to tell it when.
Basic Usage
Consider the program
10 LET NUM1 = 10 20 LET NUM2 = 20 30 LET NUMR = NUM1 + NUM2
Thus the + operator is used for performing addition. Similarly we can subtract, multiply, divide, etc.
Common Operators
| Operator | What it does | Example | Result |
|---|---|---|---|
| + | Add | 7 + 2 | 9 |
| - | Subtract | 7 – 2 | 5 |
| * | Multiply | 7 * 2 | 14 |
| / | Divide | 7 / 2 | 3.5 |
Few Advanced Operators
| Operator | What it does | Example | Result |
|---|---|---|---|
| MOD | Remainder | 7 MOD 2 | 1 |
| \ | Integer Division | 7 \ 2 | 3 |
| ^ | To the Power of | 7 ^ 2 | 49 |
| SQR | Square Root | SQR(9) | 3 |
