Categories
Class 5 Class 6 Class 7

BASIC Programming Notes

Introduction to BASIC

BASIC (Beginner’s All-Purpose Symbolic Instruction Code) is a high-level programming language.

It was developed in 1964 by John Kemeny and Thomas Kurtz at Dartmouth College, New Hampshire, United States.

BASIC is an interpreted language, which means that the instructions are executed line-by-line using a BASIC Interpreter.

Data Types

In BASIC, we work with two kinds of data:

  1. String
  2. Numeric

Any data which is not a number, can be treated as strings. String values (also known as string constants) are always enclosed in double quotes. Your name for instance, is an example of a string value.

Any piece of data that can be treated as numbers, are of numeric data type. Your age, your score in Math, your height, all are examples of numeric values (also known as numeric constants).

Variables

Variables are named storage locations that lets us temporarily store data (string or numeric constants) in the computer’s memory.

While naming the variables in our program, we must follow certain rules.

Here are those rules:

  1. A variable name must begin with an alphabet.
  2. A variable name can only consist of alphabets, digits and underscores.
  3. Variable names are not case-sensitive.
  4. We cannot use the BASIC keywords as our variable names.
  5. The string variables are always suffixed with a dollar ($) sign.
  6. We must limit the length of a variable name to a maximum of eight characters.

BASIC Commands

The PRINT Command

The PRINT command is used to display a message or the result of a program on the BASIC screen.

Syntax:

PRINT <message>

Here is an example to print a string constant:

PRINT "Hello world!"

To print the contents of a string variable S$:

PRINT S$

And here is another example to print a numeric constant:

PRINT 75

To print the contents of a numeric variable N:

PRINT N

PRINT with comma (,) and semicolon (;)

A BASIC screen is divided into 25 rows and 80 columns.

When we print data, separating them with commas, the BASIC screen is divided into five zones. Each zone therefore has 16 columns. And thus each piece of data is printed on a separate zone, left-aligned.

Below is an example of printing data using commas:

PRINT A, B, C, D, E

In the above example, each variable’s content will be displayed on a separate zone.

When we print with semicolon, the string values are printed side-by-side, without leaving any blank spaces in between.

However, when we print numeric values and separating them with semicolons, two blank spaces are left in between the values.

One blank space is reserved for the decimal point of the number, and the other blank space is reserved for the sign of the number.

Below is an example that prints data using semicolon:

PRINT A; B; C; D; E

The LET Command

The LET command is used assign a value (string or numeric) to a variable. We also use the = sign for this operation.

Syntax:

LET <variable> = <value or expression>

Below are few examples:

LET N$ = "Harry Potter"

LET MATH = 75

We can also evaluate a BASIC expression, and then use LET to store the result of that expression. Here are some examples:

LET RESULT = 75 + 5 / 2

LET ANS = A + B / 2

The INPUT Statement

The INPUT statement / command is used to receive data (string or numeric) from the user, while the program is executing.

We can even include proper message for the user that indicates what kind of data is being expected from the user.

We use a semicolon (;) to separate messages with the variables while doing so.

Syntax:

INPUT <optional message>; <variable>

Here are few examples:

INPUT N$

INPUT AGE

INPUT "Enter your name"; N$

INPUT "Enter your age"; AGE

The CLS Command

The CLS command is used to clear the BASIC screen. It is mainly used at the beginning of the program to clean the screen before showing any output.

Here is an example:

CLS

The NEW Command

The NEW command is used to clear the previous BASIC program from the computer’s memory so that we can begin fresh with a new program.

Here is an example:

NEW

The END Command

The END command is used to terminate the BASIC program. It is mainly given at the end of every BASIC program.

Here is an example:

END

The SYSTEM Command

The SYSTEM command is used to exit from the BASIC interpreter. We enter this command once we are done with programming in BASIC.

Here is an example:

SYSTEM

The LIST Command

The LIST Command is used to display the BASIC program on the BASIC screen, that is currently stored in the computer’s memory.

Here is an example:

LIST

The PRINT TAB Command

The PRINT TAB command is used to print something in a specified column, in the current row.

Syntax of PRINT TAB Command:

PRINT TAB(<column>); <Message>

Example:

PRINT TAB(5); "Hello"

The LOCATE Command

The LOCATE command is used to move the cursor in BASIC screen to a particular row and column.

Syntax of LOCATE Command:

LOCATE <row>, <column>

Example:

LOCATE 5, 12

We can also print something after using LOCATE. Here is an example:

LOCATE 4, 10: "Welcome"

Arithmetic Operators

There are seven arithmetic operators in BASIC, which are used for various arithmetic calculations:

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)
  5. Remainder (MOD)
  6. Exponent (^)
  7. Parentheses ()

Relational Operators

The relational operators are used to compare different values in BASIC. There are six relational operators:

  1. Greater than (>)
  2. Less than (<)
  3. Equal to (=)
  4. Not equal to (<>)
  5. Greater than or equal to (>=)
  6. Less than or equal to (<=)

Logical Operators

The logical operators are used to combine different conditions in BASIC to make complex decisions. There are three logical operators:

  1. AND (used when both the conditions should be true to execute a statement)
    Syntax:
    IF <condition1> AND <condition2> THEN <statement1> ELSE <statement2>
  2. OR (used when any one condition should be true to execute a statement)
    Syntax:
    IF <condition1> OR <condition2> THEN <statement1> ELSE <statement2>
  3. NOT (negates the result of a condition, meaning that it converts a true condition to false and vice-versa)
    Syntax:
    IF NOT <condition> THEN <statement1> ELSE <statement2>

The IF THEN Statement

The IF THEN statement is a conditional statement that lets us execute a statement only if a certain condition is satisfied.

Syntax of IF THEN:

IF <condition> THEN <statement>

Example:

IF A > B THEN PRINT A; " is greater!"

IF M >= 90 AND M <= 100 THEN PRINT "Excellent"

IF D = 0 OR D <= 1 THEN PRINT "Binary"

IF NOT (M >= 40) THEN PRINT "Fail"

The IF THEN ELSE Statement

The IF THEN ELSE Statement is a conditional statement that lets us execute a statement if the given condition is true, and execute another statement if the condition is false.

Syntax of IF THEN ELSE:

IF <condition> THEN <statement 1> ELSE <statement 2>

Example:

IF A > B THEN PRINT A; " is greater!" ELSE PRINT B; " is greater!"

The FOR NEXT Loop

The FOR NEXT loop is an iterative statement that lets us repeatedly execute a set of statements for a specified number of times.

Syntax of FOR NEXT:

FOR <variable> = <start> TO <end> STEP <value>
<loop_body>
NEXT <variable>

Example:

10 FOR I = 1 TO 10 STEP 1
20 PRINT I
30 NEXT I
40 END

The STEP keyword is optional. If not specified, by default the increment variable increments by 1.

The NEXT keyword is used to continue with the next value of the increment variable.

Math Functions

There are several Math Functions in BASIC that lets us solve complex calculations easily.

Below are some of these functions:

The SQR Function

The SQR() functions allows us to find the square root of a positive number.

Syntax:

SQR(<number>)

Example:

LET N = SQR(49)

The SGN Function

The SGN() function is used to check if a given number is positive, negative or zero.

For positive numbers, it returns 1. For negative numbers, it returns -1. And for zero, it returns 0.

Syntax:

SGN(<number>)

Example:

10 LET S = SGN(45)
20 IF S = 1 THEN PRINT "Positive"
30 END

The INT Function

The INT() function is used to convert a given number to an integer, by removing the fractional part of the number.

Syntax:

INT(<number>)

Example:

10 LET N = INT(4.56)
20 PRINT N
30 END

The ABS Function

The ABS() function is used to find the absolute value of a given number. It converts the negative number to positive, and keeps positive number as it is.

Syntax:

ABS(<number>)

Example:

10 LET N = ABS(-5)
20 PRINT N
30 END

String Functions

BASIC has a set of string functions as well that lets us work with strings in various ways.

The LEN Function

The LEN() function is used to find the number of characters in a given string.

Syntax:

LEN(<string>)

Example:

10 LET S$ = "Hello"
20 LET L = LEN(S$)
30 PRINT L
40 END

The LEFT$ Function

This function allows us to extract a specified number of characters from the left side of the string.

Syntax:

LEFT$(<string>, <number of characters>)

Example:

10 LET S$ = "Hello"
20 LET T$ = LEFT$(S$, 4)
30 PRINT T$
40 END

The RIGHT$ Function

This function allows us to extract a specified number of characters from the right side of the string.

Syntax:

RIGHT$(<string>, <number of characters>)

Example:

10 LET S$ = "Welcome"
20 LET T$ = RIGHT$(S$, 4)
30 PRINT T$
40 END

The MID$ Function

This function allows us to extract a specified number of characters from anywhere in the middle of the string.

Syntax:

MID$(<string>, <starting point>, <number of characters>)

Example:

10 LET S$ = "Application"
20 LET T$ = MID$(S$, 6, 3)
30 PRINT T$
40 END

String Concatenation

We can join (concatenate) two string values using the “+” operator.

Syntax:

<string 1> + <string 2>

Example:

10 LET S$ = "Hello" + " world"
20 PRINT S$
30 END

1 reply on “BASIC Programming Notes”

Leave a Reply