The BASIC Programming Language
Introduction
The BASIC Programming language is a high-level language that is fairly easy to learn. It is mainly meant for the beginners in programming.
BASIC is an acronym for Beginners' All Purpose Symbolic Instruction Code. This language was created by John Kemeny and Thomas Kurtz at Dartmouth College in the year 1964.
Download PC-BASIC
Since GW-BASIC is not supported in newer operating systems, you can download and install PC-BASIC, which has a similar interface. The video below demonstrates how you can download and install PC-BASIC on a Windows computer.
Types of Data in BASIC
In BASIC, there are two types of data:
1. String: Any data which is not a number can be considered as a string data type.
2. Numeric: Any data which is a number is considered as a numeric data type.
1. String: Any data which is not a number can be considered as a string data type.
2. Numeric: Any data which is a number is considered as a numeric data type.
Constants in BASIC
The data that doesn't change during program execution is a constant. The string constants are always enclosed in double quotes. The numeric constants are never enclosed in quotes.
Examples of a String constant: "Hello World", "125", "A"
Examples of a Numeric constant: 25, 3.45
Variables in BASIC
Variables are named storage locations that can have changes in their values during program execution. The string variables are suffixed with a $ sign. The numeric variables are not suffixed with any such symbol.
Example of a numeric variable: AGE
Example of a string variable: N$
The PRINT Command
The PRINT command is used to display a message on the BASIC screen.
Syntax:
Syntax:
PRINT <message>
Examples:
Examples:
PRINT "Hello World"
PRINT 25
PRINT with Comma
When we print multiple values separating them with commas, each value is printed in a separate zone on the BASIC screen. The BASIC screen is divided into 5 zones.
Example:
Example:
PRINT "A", "B", "C"
PRINT with Semicolon
When we print multiple values separating them with semicolons, the values are printed side by side without gaps in case of string values, and in case of numeric values, little space is left in between due to reserved spaces for decimal point and the sign of the number.
Example:
PRINT "A"; "B"; "C"
The CLS Command
The CLS command is used to clear the BASIC screen.
The NEW Command
The NEW Command is used to erase the BASIC program currently stored in the computer's memory so that a new program can be written.
The SYSTEM Command
The SYSTEM command is used to exit from BASIC.
Arithmetic Operators in BASIC
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
^ for Exponent
() for Parentheses (for changing the order of evaluation)
- for Subtraction
* for Multiplication
/ for Division
^ for Exponent
() for Parentheses (for changing the order of evaluation)
The LET Command
The LET command is used to assign a value to a variable.
Syntax:
Syntax:
LET <variable> = <value>
Examples:
Examples:
LET N = 25
LET N$ = "Thomas Kurtz"
The INPUT Command
The INPUT command is used to receive data from the user while the program is executing.
Syntax:
Syntax:
INPUT <message>; <variable>
Examples:
Examples:
INPUT "Enter your name"; N$
INPUT "Enter your roll number"; R
Note that the message is optional while using the INPUT command.
The LIST Command
The LIST command is used to display the program on the BASIC screen that is currently stored in the computer's memory.
The AUTO Command
The AUTO command is used to provide the line numbers automatically when the ENTER key is pressed. Once all the lines are entered, it can be stopped by pressing Ctrl + C at the end of the last line.
The SAVE Command
The SAVE command is used save a BASIC program in the hard disk with a file name. The file name has .BAS file extension.
Syntax:
Syntax:
SAVE "<filename>"
Example:
SAVE "SOLVE.BAS"
SAVE "SOLVE.BAS"
The LOAD Command
The LOAD Command is used to load the saved BASIC program from the hard disk to the computer's memory (RAM).
Syntax:
LOAD "<filename>"
LOAD "<filename>"
Example:
LOAD "SOLVE.BAS"
Note that the BASIC files are saved with .BAS file extension.
The TAB Command
The TAB command is used with PRINT to display a message at a particular column on the BASIC screen. There are 80 columns in a BASIC screen, so the column number ranges from 1 to 80.
Syntax:
PRINT TAB(<column number>); <message>
PRINT TAB(<column number>); <message>
Example:
PRINT TAB(12); "Welcome"
The LOCATE Command
The LOCATE command is used to display a message on the BASIC screen at a particular row and column. There are 25 rows in the BASIC screen, out of which the last row is reserved, so the row number ranges from 1 to 24. There are 80 columns on the BASIC screen, so the column number ranges from 1 to 80.
Syntax:
LOCATE <row number>, <column number>: <message>
Example:
LOCATE 12, 24: PRINT "Hello"
Relational Operators in BASIC
< for Less than
> for Greater than
<= for Less than or equal to
>= for Greater than or equal to
= for Equal to
<> for Not equal to
The IF THEN ELSE Statement
The IF THEN ELSE statement is used to take decisions in our program using the various relational operators. Note that the ELSE statement is optional.
Syntax:
IF <condition> THEN <statement 1> ELSE <statement 2>
Example:
IF A > B THEN PRINT A ELSE PRINT B
The FOR NEXT Statement
The FOR NEXT statement is used to execute a set of statements repeatedly for a specified number of times.
Syntax:
FOR <variable> = <initial value> TO <final value> STEP <value>
<statements>
NEXT <variable>
FOR <variable> = <initial value> TO <final value> STEP <value>
<statements>
NEXT <variable>
Example:
10 FOR I = 1 TO 10 STEP 1
20 PRINT "Hello"
30 NEXT I
The SQR() Function
The SQR() function is used to find the square root of a number.
Syntax:
SQR(<number>)
Examples:
LET S = SQR(49)
PRINT SQR(81)
The SGN() Function
The SGN() function is used to check whether a given number is positive, or negative, or zero. If the number is positive, it returns 1. If the number is negative, it returns -1. And if the number is 0, it returns 0.
Syntax:
SGN(<number>)
Example:
IF SGN(N) = 1 THEN PRINT "Positive"
The INT() Function
The INT() function is used to convert a given number to an integer. If the number is positive, it truncates the fractional part and returns the whole number part. If the number is negative, then it returns the nearest integer that is smaller than or equal to the given number.
Syntax:
INT(<number>)
Examples:
PRINT INT(2.45)
LET N = INT(-3.5)
The MOD Operator
The MOD operator is used to find the remainder by dividing two numbers. The sign of the result depends on the numerator's sign.
Syntax:
<numerator> MOD <denominator>
Examples:
PRINT 5 MOD 2
PRINT 5 MOD 2
LET R = 5 MOD 2
The ABS() Function
The ABS() function is used to find the absolute value of a given number which means that it will always returns the number as positive.
Syntax:
ABS(<number>)
Examples:
PRINT ABS(-23)
LET N = ABS(-2.5)
The LEN() Function
The LEN() function is used to find the length (total number of characters) of a given string.
Syntax:
Syntax:
LEN(<string value>)
Examples:
PRINT LEN("HELLO")
PRINT LEN("HELLO")
LET L = LEN("WELCOME")
The LEFT$() Function
The LEFT$() function is used to extract a substring from the left side of the given string.
Syntax:
LEFT$(<string value>, <number of characters>)
Syntax:
LEFT$(<string value>, <number of characters>)
Examples:
PRINT LEFT$("HELLO", 2)
LET S$ = LEFT$("TEACHER", 5)
The RIGHT$() Function
The RIGHT$() function is used to extract a substring from the right side of the given string.
Syntax:
RIGHT$(<string value>, <number of characters>)
Examples:
PRINT RIGHT$("FOLLOW", 3)
LET S$ = RIGHT$("SMARTPHONE", 5)
The MID$() Function
The MID$() function is used to extract a substring from anywhere in middle of the given string.
Syntax:
MID$(<string value>, <starting position>, <number of characters>)
Examples:
PRINT MID$("WELCOME", 4, 3)
LET S$ = MID$("FRIENDSHIP", 7, 4)
Comments
Post a Comment