Wednesday, 12 February 2014

Data types and input/output statements in BASIC

BASIC Programmiang



BASIC is an easy and simple high level programming language. It was developed in 1964 t Dartmouth college by Professors John G. Kemeny and Thomas E. Kurtz. Because of its simplicity and conversational nature, BASIC quickly became one of the most popular programming language.

10.1 BASIC Programming

BASIC is the first high level language to be used on micro computers. First interpreter of BASIC language was written in 1975 by Microsoft. Most popular versions are GW BASIC and QBASIC.
BAIC has the following features:
Ø  It is easy language
Ø  It is a general purpose language, suitable for scientific and commercial programming.
Ø  It has simple syntax rules and easy to test and debug.
Ø  It is well suited for computers having limited memory.

10.2 character set in BASIC

Character set in BASIC consists of the followings:-
Ø  The Alphabetic Characters In BASIC are capital letter A, B, C………, Z and a, b, c, ……….z.
Ø  The numeric characters are digits 0, 1,2,……9.
Ø  Special characters such as $, #, %, @, \ blank spaces and punctuations etc.
Ø  Arithmetic and relational operators such as +, -, *, /, ^, (, ), =, >, < etc.

10.3 reserved words

Reserved words have special meaning in BASIC. They cannot be used as variables. There are 160 reserved words in BASIC.

10.4 types of entries in BASIC

There are 3 types of entries in BASIC, which are commands, program statements, and data. 
Commands are executable instructions such as save, list etc. some BASIC commands are described below:-
Auto
It generates an automatic line number each time <enter> key is pressed.
Auto 80, 20         first number specifies the beginning of the line number and second number specifies the increment in the line number.
Auto   , 20            if the number is not specified before comma then the first line number assumes 0.
Auto  .,                 if period is entered before comma, it indicates the current line, if the increment is not specified then the last increment specified is assumed.
Clear                     
It clears value assigned to numeric and string variable. Resuming all numeric variables to zero, all string variables to null and close all open files.
Cls / home          Executing Cls or Home clears the screen and the cursor returns on the top left corner.
Delete
It deletes program lines or line range.
Delete 50             it deletes the specified line.
Delete 30-50      it deletes all the lines within range.
Delete -100         it deletes all lines up to the end line number specified.
Delete  70-          it deletes all lines from the line number specified up to the end of the program.
Edit
It edits a specified line number
Edit 50                   it displays specified line to be edited.
Files
To display names of all files from the specified drive.
Files                       displays all the filenames in the current directory.
Files “BAS”          displays all files having BAS extension.
Files “A”               displays all files on A drive.
Kill
To delete a file from disk.
Kill <filename>  it deletes the specified file.
List
To display the program or part of the program currently in memory.
List 20-50             lists all programs between line numbers specified.
List 40-                  lists all programs from line number specified.
List -50                  lists all programs up to the end line number specified.
Load
To load a file from diskette to memory.
Load “filename” it loads the specified file into memory.
New
To delete the currently in memory and clears all variables.
New                      it erases the old program from memory and clears the screen.
Renum
To renumber program lines without changing lines order.
Renum 50, 35, 10              the first number specifies new number for the first line, the second number specifies old number of the line and the third number specifies increment.
Run
It begins program execution currently in memory.
Run 40                  if the number is specified then execution begins from that line.
Save
It is used to save a program file o a drive.
Save “A:try”       it saves the try file on the disk in drive A.
System
It is used to exit BASIC.

10.5 Statements

Statements are part of computer program. They have specific purposes in a program. Commands usually perform some type of program maintenance such as editing running or saving programs. On the other hand statements are part of programs and run in compiler mode.
A program in BASIC has two parts; a line number and one or more instructions. For example
10  let A=10

10.6 Data type

BASIC has two data types.
Numeric Data
BASIC deals with data having numbers that are used in calculations. For example 25 and 30.67 etc.
String Data
BASIC also deals with texts called strings. A string consists of all the characters, word or numbers enclosed by quotation marks such as “BRC Turbat”.
Constants
A constant in BASIC is something whose value does not change during execution of program.
Variables
Variables are names used to represent certain quantities in a BASIC program. Variable values can change during execution. There are two types of variables, which are:-
a)      Numeric variables
A numeric variable has a numeric value either supplied by the user or calculated by the computer during execution. A numeric variable name may consist of all alphabets and numeric constants but must begin with an alphabet such as Age.
b)      String variables
A string variable represents a string that is a sequence of characters i.e letters, numbers and special characters such as +,-,*, $, =,? Etc. a string name must be followed by a dollar sign such as A$.

10.7 operators

Operators perform mathematical or logical operations on values. In BASIC the operators are divided into four categories.
a)      Arithmetic operators
Numeric expression contains operators that perform arithmetic operations called arithmetic operators.
Arithmetic operators used in BASIC
Operation
operator
Addition
+
Subtraction
-
Multiplication
*
Division
/
Exponentiation
^
Negation
-

10.8 The hierarchy of arithmetic operators

The order in which various operations are performed is as follows:-
First:              perform all operation inside parenthesis.
Second:        evaluate all exponentiation from right to left.
Third:            perform all multiplication and divisions from left to right.
Fourth:         perform all additions and subtractions from left to right.
b)      Relational operators
Relational operators are used to compare two quantities. The result of comparison may be either yes or no or true or false, which decide the flow of the program.
Relational operators
Operators
relation
=
equal
<> 
Not equal
> 
Greater than
< 
Less than
>=
Greater than or equal to
<=
Less than or equal to
c)       Logical operators
Logical operators such as AND, OR, NOT, NAND, NOR and XOR perform logical operations. The result of the logical operation would be either true or false and decide the flow of a program.
d)      Functional operators
The built-in functions such ABS, INT, FIX, left$  etc perform arithmetic and string operations.

10.9 modes of operations

a) Direct mode
In the direct mode, program statements are entered without a line number. The computer will execute it as soon as the <return> or <enter> key is pressed after typing, output is displayed on the screen

10.10 Print command

The print command allows the computer to communicate with you. Print command is used to display any combination of letters, numbers and symbols or to evaluate numerical expressions. For example
Print 25+35
Print “BRC Turbat”
b)            indirect mode
the program mode is also called the indirect mode. A program in BASIC consists of line number followed by an instruction or statement. The statement with smaller number is executed first.

10.11 let statement

Let is an assignment statement that stores a value in a location assigned to a given variable. The general format of the let statement is:-
Line # let variable = expression

10.12 Comma and Semicolon

A comma in print statement separates strings and numerical expression.
The semicolon is used in print statement to separate strings.

10.13 Assignment statements

The assignment statement is used assign an initial value to a variable or assign a value as a result of a computation, for example:-
A=10
B=A+50

10.14 Remark statement

The Rem statement documents the program and provides information to the user about the program, which may be purpose of program, necessary information for its execution and its logic for users. Rem statement is non-executable.

10.15 Input statement

Input statement allows to enter value of a variable while the program is running. The format of the input statement is :
Line # input (variable name)

10.16 Read..Data statements

Read…Data statements are used to assign values. This is useful when the data contains large number of values. A Data statement contain values of the variables which are assigned to the respective variable names in Read statement. The format is:
Line # Read (1st variable, 2nd variable)….
Line # Data (1st value, 2nd value)….

10.17 Tab function

Tab function allows us to line up the numbers vertically. The Tab function must be accompanied by a number enclosed in the parenthesis telling the computer to move cursor to column specified such as Tab(20).

10.18 Print using statement

Print using statement allows specifying the appearance and position of numbers. It also provides a way to round of numbers to a given number of decimal places. Its format is:
Line # print using “####.##”
Read More