BASIC Programming Language
TOPIC: BASIC Programming Language
CLASS: SSS One
Origin and Features of BASIC
BASIC stands for Beginner’s All-purpose Symbolic Instruction Code. It was developed in the 1960s by John Kemeny and Thomas Kurtz to teach programming to students at Dartmouth College. Over the years, BASIC has undergone several developments, resulting in various forms of the language.
BASIC is now widely known in forms like VB.NET (Visual Basic.Net). Most BASIC languages use program translators called interpreters to allow the computer to understand and execute BASIC statements. Examples of such interpreters include:
- BASICA
- GwBASIC
- Turbo BASIC
- Quick BASIC
BASIC Character Set
The characters that can be used in BASIC language include:
- Alphabetic Characters: These consist of all uppercase and lowercase letters (A to Z).
- Numeric Characters: These are numbers from 0 to 9.
- Special Characters: These are characters that are not letters or numbers. They include punctuation marks, symbols, and operators. Examples: `+`, `-`, `*`, `/`, `^`, `=`, `<`, `>`, `(`, `)`, `%`, `$`, `#`, `!`, etc.
BASIC Variable
A variable is a named storage location in a computer's memory that can hold a value. This value can change during the execution of a program.
Types of Variables
-
Numeric Variables: These are used to store numerical values, such as whole numbers (integers) or numbers with decimal points.
Examples: `N`, `Y`, `P`, `SUM`, `AVERAGE`, `TOTAL` -
String Variables: These are used to store text (alphabetic and alphanumeric) values. A string variable name is always written with a dollar sign (`$`) as the last character.
Examples: `Name$`, `Address$`, `City$`, `Response$`
Rules for Naming Variables
- Variable names can combine alphabets and numbers. In many BASIC dialects, the length of the variable name might be limited (e.g., to 40 characters for QBasic).
- No reserved word (a command or keyword used by BASIC) can be used as a variable name (e.g., you cannot name a variable `PRINT` or `INPUT`).
- Most special characters (like `+`, `-`, `*`, `/`) cannot be part of a variable name, except for the type-declaration characters like `$` for strings.
- A string variable must correspond to string data, and a numeric variable must correspond to numeric data.
- In a program, each variable is referred to throughout the program by its specific name.
Constants
A constant is a fixed value that does not change during the execution of a program. Constants are literal values directly used in your code.
Types of Constants in BASIC
-
Numeric Constant: Any signed or unsigned number (whole numbers or decimals).
Examples: `23`, `98`, `1.44`, `-50`, `3.14159` -
Alpha-Numeric or String Constant: A combination of letters, digits, and other symbols that are treated as text. String constants are always enclosed within inverted commas (double quotes).
Examples: `"Hello World"`, `"My Name Is Joseph"`, `"123 Main Street"`
Rules for Numeric Constants
- A number can typically have a reasonable number of digits, depending on the BASIC dialect's precision (e.g., single or double precision).
- No comma is allowed within a number (e.g., `1,000` should be `1000`).
- A decimal point can appear anywhere in the number (e.g., `123.45` or `.5`).
- If the value is very large or very small, it can be expressed in exponent form (scientific notation), e.g., `1.23E+05` for `123000`.
- No blank spaces or other special characters (except `+` or `-` for sign, and `.` for decimal) are allowed within the number itself.
BASIC Expressions and Operators
In programming, an expression is a combination of operands and operators that evaluates to a single value. Operands are the data items (variables or constants) involved in an expression, while operators determine the action to be carried out on the operands.
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 expressions in BASIC:
- Arithmetic expression
- Relational expression
- Logical expression
Arithmetic Expression
A BASIC arithmetic expression is used to represent mathematical formulae in BASIC programming. All arithmetic expressions must appear on a single line. There is no superscript or special formatting as we find in algebra.
Arithmetic Operators
Symbol | Name | Function |
---|---|---|
^ |
Caret / Exponentiation | Raises a number to a power (e.g., `X^2` for x squared) |
/ |
Slash | Division (e.g., `A/B`) |
* |
Asterisk | Multiplication (e.g., `L*B`) |
+ |
Plus | Addition (e.g., `X+Y`) |
- |
Minus | Subtraction (e.g., `P-Q`) |
\ |
Backslash | Integer Division (divides and returns only the integer part, discarding remainder) |
MOD |
Modulo | Returns the remainder of a division (e.g., `10 MOD 3` returns `1`) |
Algebraic Expression VS BASIC Arithmetic Expression
Relational Expression
A relational expression is used for the comparison of two or more data items. These expressions evaluate to either true or false.
BASIC Relational Operators:
Symbol | Name |
---|---|
< |
Less than |
> |
Greater than |
= |
Equal to |
<> |
Not Equal to |
<= |
Less than or equal to |
>= |
Greater than or equal to |
Logical Expression
A logical expression combines two or more relational expressions using logical operators to evaluate a compound condition as either true or false.
BASIC Logical Operators:
- `AND`
- `NOT`
- `OR`
Evaluation of Arithmetic Expression (Order of Operations)
To evaluate an arithmetic expression, BASIC follows a specific order of operations. This ensures that expressions are calculated consistently. The order is:
Priority | Operator |
---|---|
1st | Parentheses `()` |
2nd | Exponentiation `^` |
3rd | Multiplication `*` and Division `/` (evaluated from left to right) |
4th | Integer Division `\` and Modulo `MOD` (evaluated from left to right) |
5th | Addition `+` and Subtraction `-` (evaluated from left to right) |
Example: Evaluate `4*A*B^2 + (A^2*B + C) / (A + B)` if `A=2`, `B=4`, and `C=2`
Solution:
Original Expression: `4 * A * B^2 + (A^2 * B + C) / (A + B)`
- Substitute values: `4 * 2 * 4^2 + (2^2 * 4 + 2) / (2 + 4)`
-
Evaluate terms in parentheses (1st Priority):
* ` (2^2 * 4 + 2) ` becomes ` (4 * 4 + 2) ` which is ` (16 + 2) ` = `18`
* ` (2 + 4) ` becomes `6`
Expression is now: `4 * 2 * 4^2 + 18 / 6` -
Evaluate Exponentiation (2nd Priority):
* `4^2` becomes `16`
Expression is now: `4 * 2 * 16 + 18 / 6` -
Evaluate Multiplication and Division (3rd Priority - from left to right):
* `4 * 2 * 16` becomes `8 * 16` = `128`
* `18 / 6` becomes `3`
Expression is now: `128 + 3` - Evaluate Addition (5th Priority): * `128 + 3` becomes `131`
The final evaluated value is 131.
BASIC Statements
LET Statement
The LET statement is used to assign a numeric or string value to a variable. It explicitly sets the value of a variable.
Syntax:
LET [variable] = [numeric_constant_or_expression] ' for numeric value
LET [variable]$ = "[string_constant]" ' for string value
Examples:
LET X = 12
LET B$ = "Clementina"
LET AREA = L * B
INPUT Statement
The INPUT statement is used to get data from the user during program execution. It can display a prompt message to the user.
Syntax for numeric value:
INPUT "[prompt]"; [variable]
Syntax for string value:
INPUT "[prompt]"; [variable$]
Examples:
INPUT "Type in the number: "; A
INPUT "Type in your name: "; N$
READ-DATA statement
The READ and DATA statements work together. `DATA` statements store values directly within the program, and `READ` statements retrieve these values sequentially when needed during execution.
Syntax for READ-DATA statement
READ variable1, variable2, ...
DATA value1, value2, ...
Example:
10 READ A, B, C
20 DATA 5, 6, 7
30 LET SUM = A + B + C
40 PRINT SUM
50 END
[run] Output:
18
REM (Remark) Statement
The REM statement is used to insert comments or remarks into a BASIC program. These remarks are ignored by the computer and are not executed. They are very useful for improving the readability of your program by explaining what different parts of the code do.
Syntax:
REM [remark text]
' [remark text] (A single apostrophe also works as REM)
Example:
REM This program is written to add six numbers
' This is another way to add comments in BASIC
PRINT statement
The PRINT statement is used to display data (text, numbers, or results of expressions) from the computer's memory to the output device (usually the screen).
Examples:
PRINT A
PRINT 100
PRINT "I Like Writing Program"
PRINT "The sum is "; SUM
Program Terminators (END and STOP Statements)
Both `END` and `STOP` statements are used to stop the execution of a BASIC program, but they have different uses:
- The STOP statement can be used to temporarily halt the execution of a program at any point. It's often used during debugging to pause and inspect variable values. A "Break in line XX" message might be displayed.
- The END statement indicates the actual, final end of a program. It should typically be the last line of your main program logic.
The `STOP` statement may appear many times and anywhere within a program, whereas an `END` statement can only appear at the logical end of a program and typically only once.
Example:
10 REM Demonstrating END statement
20 PRINT "Good morning"
30 END
[run] Output:
Good morning
FOR – NEXT Loop
A FOR-NEXT loop is used to repeat a series of instructions a specified number of times. It's ideal when you know exactly how many times you want a loop to run.
Syntax:
FOR variable = x TO y [STEP z]
.
. (Statements to be repeated)
.
NEXT [variable]
- `variable`: A numeric variable that acts as a counter.
- `x`: The starting value for the counter.
- `y`: The ending value for the counter.
- `STEP z`: (Optional) Specifies the increment or decrement for the counter. If omitted, `STEP 1` is assumed.
Example 1: Write a program using FOR-NEXT statement to print a statement five times
Solution
10 FOR I = 1 TO 5
20 PRINT "The dullest pencil is better than the sharpest memory"
30 NEXT I
40 END
[run] Output:
The dullest pencil is better than the sharpest memory
The dullest pencil is better than the sharpest memory
The dullest pencil is better than the sharpest memory
The dullest pencil is better than the sharpest memory
The dullest pencil is better than the sharpest memory
Example 2: Write a program using FOR-NEXT statement to display odd numbers from 1 to 20
Solution
10 REM program to print odd numbers from 1 to 20
20 PRINT "Odd numbers from 1 to 20 are:"
30 FOR ODD = 1 TO 20 STEP 2
40 PRINT ODD
50 NEXT ODD
60 END
[run] Output:
Odd numbers from 1 to 20 are:
1
3
5
7
9
11
13
15
17
19
Simple BASIC Programs
Example 1: Program to find the sum and difference between two numbers
Solution
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; " + "; NUM2; " = "; SUM
100 PRINT NUM1; " - "; NUM2; " = "; DIFF
110 END
[run] Example Interaction:
Type the first number and press ENTER: ? 15
Type the second number and press ENTER: ? 7
First number is: 15
Second number is: 7
================
15 + 7 = 22
15 - 7 = 8
Example 2: A program to calculate the area and perimeter of a rectangle
Solution
10 REM Program to find the area and perimeter of a rectangle
20 INPUT "Type the length of the rectangle: "; L
30 INPUT "Type 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
[run] Example Interaction:
Type the length of the rectangle: ? 10
Type in the breadth of the rectangle: ? 5
The area of the rectangle is 50
The perimeter of the rectangle is 30
Thanks this really helped me out
ReplyDeleteYou are welcome
DeleteThank you so much. This really helped. I am so grateful
ReplyDeleteYou are welcome
DeleteThis is AMAZING!
ReplyDeleteThanks a lot.
ReplyDeletePls what is the meaning of the 20, 50,80
ReplyDeleteJust the line number. For reference purposes. The line number can also be omitted if you wish
DeleteI 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
ReplyDeleteWaoooo, I really like this coding
ReplyDeleteIt's very detailed
ReplyDeletePls what of python?
ReplyDelete