BASIC PROGRAMMING J2
CLASS: JSS Two
TOPIC: BASIC Programming Language
Origin and Features of BASIC
BASIC stands for Beginner’s All-purpose Symbolic Instruction Code. It was developed in 1964 by John Kemeny and Thomas Kurtz to teach students at Dartmouth College. Over the years, BASIC has undergone many changes and historical developments, resulting in various forms of the language.
Today, modern versions like VB.NET (Visual Basic.Net) exist. Most BASIC languages use special programs called interpreters to help the computer understand and follow the instructions (statements) in a BASIC program.
Examples of such interpreters include:
- BASICA
- GwBASIC
- Turbo BASIC
- Quick BASIC
BASIC Character Set
These are the different symbols that can be used in the BASIC programming language. The characters used in BASIC include:
- Alphabetic Characters: These are the letters from A to Z (both uppercase and lowercase, though BASIC often converts to uppercase).
- Numeric Characters: These are the numbers from 0 to 9.
- Special Characters: These are symbols that are not letters or numbers. They include punctuation marks, mathematical symbols, and other special signs. Examples: `+`, `-`, `*`, `/`, `^`, `=`, `(`, `)`, `%`, `#`, `$`, etc.
BASIC Arithmetic Expressions
An expression is a combination of operands and operators. * Operands are the data items or values involved in a calculation (e.g., numbers, variables). * Operators are symbols that tell the computer what action to perform on the operands (e.g., addition, subtraction). For instance, in the expression `X + Y`, "X" and "Y" are the operands, while "+" is the operator.
BASIC uses specific operators to perform arithmetic operations:
Operation | Operator |
---|---|
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Exponentiation (Power) | ^ |
Rules for BASIC Arithmetic Expressions
Every arithmetic expression in BASIC must be written on a single line. Unlike in algebra, there is no way to write numbers as superscripts (like $X^2$) in BASIC; you must use the `^` operator for powers (e.g., `X^2`).
Algebraic Expressions vs. BASIC Expressions
It's important to know how to write algebraic expressions correctly in BASIC.
BASIC Variables
Variables are names used in a program to represent a storage location in the computer's memory. This location holds data that can change as the program runs.
Types of Variables
BASIC generally allows two main types of variables:
-
Numeric Variables: These variables are used to store numbers, like whole numbers (e.g., 23, 98) or decimal numbers (e.g., 1.44, 5.0). Their names usually consist of letters and numbers.
Examples of numeric variables: `N`, `Y`, `P`, `SUM`, `AVERAGE`. -
String Variables: These variables are used to store text (which can include letters, numbers, and special characters). The last character in a string variable's name must always be a dollar sign (`$`).
Examples of string variables: `NAME$`, `COLOR$`, `X$`.
Constants
Constants are fixed values that do not change during the execution of a program.
Types of Constants in BASIC
BASIC allows two main types of constants:
- Numeric Constant: This is any number, positive or negative, that is directly used in a program (e.g., `10`, `-5`, `3.14`).
-
Alpha-Numeric or String Constant: This consists of a combination of letters, digits, and other symbols that are treated as text. String constants must always be enclosed within double quotation marks (`""`).
Example: `"Hello World"`, `"My Name Is John"`, `"123 Main Street"`.
BASIC Key Statements
BASIC key statements are special words that have specific meanings and instructions for the computer. They are essential for writing BASIC programs.
CLS Statement
The CLS (Clear Screen) statement is used to erase any text or graphics that have been displayed on the computer screen. It's useful when you want to start with a fresh, clean screen to show new information.
LET Statement
The LET statement is used to assign a value to a variable. This value can be a number (for numeric variables) or text (for string variables). A LET statement must include a variable, an equals sign (`=`), and the value or expression to be assigned.
Examples:
LET X = 12
LET B$ = "Deborah"
INPUT Statement
The INPUT statement is used to get data from the user while the program is running (executing). When the program reaches an INPUT statement, it will usually pause and wait for the user to type something and press Enter.
Examples:
INPUT A, B, C
INPUT N$, M$, Factor
READ-DATA Statements
The READ and DATA statements are two BASIC keywords that are always used together. The `DATA` statement is used to store values directly within a line of the program, while the `READ` statement is used to read those values from the `DATA` statement and assign them to variables.
Example:
10 READ A, B, C
20 DATA 5, 6, 7
30 LET SUM = A + B + C
40 PRINT "The sum is"; SUM
50 END
REM (Remark) Statement
The REM (Remark) statement is used to add comments or notes inside a BASIC program. These remarks help programmers understand what the code does. The computer completely ignores `REM` statements when running the program, so they don't affect how the program works.
Example:
REM This program calculates the total sales
10 REM program to add six numbers (Example remark)
PRINT Statement
The PRINT statement is used to display output (like text, numbers, or the values of variables) from the computer's memory onto the screen.
Examples:
PRINT 45
PRINT A, B, C
PRINT "cmpnote blog"
Program Terminators (END and STOP)
Both END and STOP statements are used to stop the execution of a program. * The STOP statement can be used multiple times and anywhere in a program to temporarily halt execution, often for debugging. * The END statement indicates the very last instruction of a program and can only appear once, at the very end.
Example using END statement:
10 REM Example of END statement
20 PRINT "Good morning"
30 END
GO TO Statement
The GO TO statement is used to transfer the program's control from its current line number to a specific line number mentioned after `GO TO`. This means the program will "jump" to that new line and continue execution from there, skipping any lines in between.
Example:
10 REM GO TO statement example
20 GOTO 40
30 PRINT "Good morning" ' This line will be skipped
40 END
In the example above, when the program reaches line 20, it immediately jumps to line 40. This means line 30, which says `PRINT "Good morning"`, will never be executed. The program will then end at line 40.
Some Simple BASIC Programs
Example 1: Find the average of six numbers
Write a program in BASIC to find the average of six numbers.
Solution:
10 REM Program to find the average of six numbers
20 REM Numbers will be input by the user
30 INPUT "Type in the first number: "; A
40 INPUT "Type in the second number: "; B
50 INPUT "Type in the third number: "; C
60 INPUT "Type in the fourth number: "; D
70 INPUT "Type in the fifth number: "; E
80 INPUT "Type in the sixth number: "; F
90 LET AVERAGE = (A + B + C + D + E + F) / 6
100 PRINT "The average is"; AVERAGE
110 END
Example 2: Calculate Perimeter and Area of a Rectangle
Write a program to calculate the perimeter and area of a rectangle.
Solution:
10 REM Program to calculate the perimeter and Area of a Rectangle
20 INPUT "Input the length: "; L
30 INPUT "Input the breadth: "; B
40 LET PERIMETER = 2 * (L + B)
50 LET AREA = L * B
60 PRINT "The perimeter is"; PERIMETER
70 PRINT "The area is"; AREA
80 END
Kudos to you bro. Can you please work on Data Processing?
ReplyDeleteGood job guys
ReplyDeleteBasic programming comprises of weeks 2 and 3. Week 2 says basic language, meaning of BASIC, BASIC character set,key BASIC statements and simple BASIC statement, while week 3 says simple BASIC statements, examples of simple BASIC statements and simple BASIC program.please how do I divide this notes into the two different lessons. Help a sister please. Thanks for the goog job. God bless
ReplyDeleteI don't know the class you are referring to but for SSS 1 use this link thank you https://cmpnote.blogspot.com/p/basic-programming-language.html?m=1
DeleteBravooooooo
ReplyDeleteDon't you guys think BASIC is getting kinda outdated and we should be teaching student more demanded and more powerful languages like Python and maybe JavaScript even?
ReplyDeleteYou're right but the current Nigeria curriculum requires it to be taught in secondary schools
DeleteWow seems interesting
ReplyDelete