BASIC Programming Language

BASIC Programming Language

TOPIC: BASIC programming Language

CLASS: SSS One

Origin of and Features of BASIC

BASIC stands for Beginner’s All-purpose Symbolic Instruction Code. It was developed in 1960 by John Kemeny and Thomas Kurtz to teach students at Dartmouth College. It has undergone a series of historical development, which has resulted in several forms of the language.
BASIC is now in form of VB.NET (Visual Basic.Net). The majority of BASIC languages use program translators called interpreters to allow the computer to understand and obey the BASIC statements in the computer program. Examples of such interpreters are:
BASICA
GwBASIC
Turbo BASIC
Quick BASIC

BASIC Character Set

The characters used in BASIC language include:
i. Alphabetic Characters: BASIC alphabetic characters consist of letters (A to Z)
ii. Numeric Characters: Numbers from 0 to 9 are used in BASIC language
iii. Special Characters: Special characters are characters that are not letters or numbers. They include punctuation marks, accent marks, ASCII control characters, formatting characters. Examples + % ^ # = ( ) etc

BASIC variable

A variable is a quantity that changes during the execution of a program. It can also be defined as a name that is used to represent some storage location.

Types of Variables

1. Numeric Variables: These are used to store numeric values such as 23, 98, 1.44 etc. Examples of numeric variables are; N, Y, P, SUM, AVERAGE, etc

2. String Variables: These are used to store alphabetic and alpha-numeric values. A string variable name is always written with a dollar sign ($) as the last character. E.g. Name$, AVG$, X$, etc

Rules for coding variable
i. In BASIC combining alphabets, numbers and the decimal point (a maximum length of 40 characters) may form a variable.
ii. No reserve word can be used as a variable name.
iii. Special characters cannot be used for naming a variable. iv. A string variable corresponds to string data whereas a numeric variable corresponds to numeric data,
v. In a program, each variable is referred to throughout the program by its name.

Constants
A constant is data that remains the same as the program runs (executes). Constants are values stored or assigned to variables.
Types of Constants in BASIC
BASIC allows two constants which are;
Numeric constant: Numeric constant in BASIC is any signed or unsigned number.
Alpha-Numeric or string constant: It consists of the combination of letters, digits, and other symbols that are treated in a manner completely analogous to a numeric constant. They are enclosed within inverted commas.

Rules for numeric constants
i. A number can have a maximum of 8 digits
ii. No comma is allowed
iii. A decimal point can appear anywhere
iv. If the value is quite larger it is expressed in exponent form
v. No blank space, special characters or any other letter is allowed in the number.

BASIC Expressions and Operators

In programming, an expression can be defined as the combination of operands and operators. Operands are the data items involved in an expression. Operators determine the action to be carried out on the operand in the expression. For instance in the statement: LET C = A + B, A and B are the operands while “+” is the operator.
There are three major types of expression in BASIC. They are:
Arithmetic expression
Relational Expression
Logical expression

Arithmetic Expression
BASIC arithmetic expression is used to represent mathematical formulae in BASIC programming. Below is a list of BASIC arithmetic operators:
Arithmetic Operators

Symbol Name Function
^ Upper caret Exponentiation
/ Slash Division
* Asterisk Multiplication
+ Plus Addition
- Minus Substraction

Arithmetic Expression
Every arithmetic expression must appear on a single line. There is no superscript in BASIC as we find in algebra.

Relational Expression
Relational Expression is used for the comparison of two or more data items. BASIC relational operators are listed below:
Symbol Name
< Less than
> Greater than
= Equal to
<> Not Equal to
<= Less than or equal to
>= Greater than or equal to

Logical Expression
Logical expression involve is an expression involving two or more relational repression joined by a logical expression. BASIC logical operators are:
AND
NOT
OR

Evaluation or Arithmetic Expression

To evaluate an arithmetic expression, the following order is followed:

Priority Operator
1st Parenthesis i.e ( and )
2nd Exponentiation
2nd Exponentiation
3rd Multiplication and Division
4th MOD and INTER Division
5th Addition and Subtraction

Example: evaluate 4*A*B^2+ (A^2*B+C)/(A+B) if A=2; B=4 and c=2
Solution
Step 1 Substituting we have ---- 4*2*4^2+ (2^2*4+2)/ (2+4)
Step 2 evaluate terms in the parenthesis ---- 4*2*4^2+18/6
Step 3 evaluate 4^2 --------- 4*2*16+18/6
Step 4 evaluate 4*2*18 ------- 128+18/6
Step 5 evaluate 18/6 ------ 128 + 3
Step 6 evaluate 128+3 ----- 131

BASIC Statements

LET Statement
The let statement is used to assign a numeric or string value to a variable.
Syntax
LET [variable] = [constant] for numeric value
LET [variable]$ = [“value”] for string value
Example
LET X = 12
LET B$ = “Clementina”
LET AREA = L*B

INPUT Statement
The INPUT statement is used to enter data into the computer with a user prompt or a group of variables during program execution.
Syntax for numeric value
INPUT “[prompt]”; [variable]
Syntax for string value
INPUT “[prompt]”; [variable$]
Example
INPUT “type in the number”; A
INPUT “Type in your name”; N$

READ-DATA statement
READ and Data are two statements concerned with each other which are used to put data in a line of the program and to read the data when it is needed.
Example
READ A, B, C
DATA 5, 6, 7
LET SUM = A+B+C
PRINT SUM
END

REM (Remark) Statement
The REM statement is used to insert comments or remarks into a BASIC program. The use of remark statements improves the readability of the program. REM is a non-executable statement.
Syntax
REM [remark]
Example
REM program to add six numbers

PRINT statement
This statement is used to transmit data from the computer memory to the output device.
Examples
PRINT A
PRINT “I Like Writing Program”

Program Terminators (END and STOP Statements)
The STOP statement is used to terminate the execution of a program at any point in the program. The END statement indicates the actual end of a program. The STOP statement may appear many times and anywhere, whereas an END statement can only appear at the end of a program and only once.
Example
REM END statement
PRINT “Good morning”
END

FOR – NEXT
Looping is used to have the computer do repetitive tasks in a fraction of the time that would otherwise be required. The most common type of loop used in QBASIC programming is the FOR...NEXT and WHILE WEND loop that repeats a series of instructions a specified number of times.
Syntax
FOR variable=x TO y [STEP z]
.
.
.
NEXT [variable][,variable...]
x,y, and z are numeric expressions.
STEP z specifies the counter increment for each loop.
Example 1: Write a program using FOR-NEXT state to print any statement five times Solution

FOR I = 1 TO 5
PRINT “the dullest pencil is better than the sharpest memory”
NEXT I
END

EXAMPLE 2: Write programming using FOR-NEXT statement to display odd numbers from 1 to 20
Solution
REM program to print odd numbers from 1 to 20
PRINT “odd numbers from 1 to 20 are”
FOR ODD =1 TO 20 STEP 2
PRINT ODD
NEXT ODD
END

Simple Basic Programs

Example 1: Program to find the sum and difference between two number
10 REM this program accepts two numbers and finds their sum and difference
20 INPUT “Type the first number and press ENTER”; NUM1
30 INPUT “Type the second number and press ENTER”; NUM2
40 LET SUM = NUM1 + NUM2
50 LET DIFF = NUM1 – NUM2
60 PRINT “first number is “; NUM1
70 PRINT “second number is “; NUM2
80 PRINT “================”
90 PRINT NUM1; “+”; NUM1 “=” ; SUM
100 PRINT NUM1; “-“ ; NUM2 “=” DIFF
110 END


Example 2: a program to calculate the area and perimeter of a rectangle
10 REM program to find the area and perimeter of a rectangle
20 INPUT “Type the length of the rectangle”; L
30 INPUT “Type the in the breadth of the rectangle”; B
40 LET AREA = L*B
50 LET PERI = 2 * (L + B)
60 PRINT “The area of the rectangle is “ ; AREA
70 PRINT “The perimeter of the rectangle is” ; PERI
80 END

Comments

  1. Thanks this really helped me out

    ReplyDelete
  2. Thank you so much. This really helped. I am so grateful

    ReplyDelete
  3. This is AMAZING!

    ReplyDelete
  4. Pls what is the meaning of the 20, 50,80

    ReplyDelete
    Replies
    1. Just the line number. For reference purposes. The line number can also be omitted if you wish

      Delete
  5. I love this book please write more on computer for some people who are not able to go to school but have a phone it will so exciting

    ReplyDelete

Post a Comment

Popular posts from this blog

90 Objective Examination Questions in Major Subjects

Complete Computer Studies/ICT Curriculum for JSS 1 to SSS 3

JSS One Objective Questions in Computer Studies/Information Technology