GETTING DATA INTO THE MEMORY
It should be remembered that a computer computes things numerically. If a Numeric constant has not been assigned to a numeric variable, the computer cannot do the operation which involves that numeric variable. Running a program without defining the variables properly might lead to one of the following results:
1) Error message saying that the variables are not defined.
2) Wrong answer because of assuming some other values which are already available in the memory.
It is therefore essential to assign values to variables before they are encountered in the program.
There are three ways to assign values to variables in BASIC. That is, a variable can be given a value using the LET or INPUT or READ statement.
|How to store data in memory?|
|We can store value in variable by LET statement, INPUT statement and READ…DATA statement and then value goes in memory cell in which you can store value in variable.|
LET is an assignment statement and its function is to assign a value to a variable. The form of this statement is
Valid definition of variables using LET are:
LET X = 10.5
LET A = 5
LET B = A + X
LET A = A + 1
LET A$ = “APPLE”
LET N$ = “1234”
All these statements mean that the value of the variable on the left of the equal sign is set equal to the value of the quantity or expression on the right.
It is essential that the type of values on the right hand side should match with the type of variables on the left-hand side. Statement such as:
LET N = “NAME”
LET A$ = 30.5
are not valid and make no sense to the computer. They are syntax errors.
Statement of the type
LET X, Y = 5, 10
are incorrect. Only one variable can be assigned a value using one LET statement.
An INPUT statement can be used in a program to provide data to a variable. This takes the form
INPUT [ variable1, variable2,…]
Where variable1, variable2,… are valid names, separated by a comma.
50 INPUT X
60 INPUT N$
When this program is run, the computer, on reaching the line number 50 will output a question mark ? and then waits for a numeric value for X to be typed in and then the carriage return key. Again a similar question mark will appear on reaching the line 60. But this time a string constant has to be typed in because the computer is waiting for data for the string variable N$. Thus, suitable responses to statements 50 and 60 include
? 123 carriage return
? Hello India carriage return
The numeric constant 123 will be stored in location X and the string constant Hello India will be stored in location N$.
An INPUT statement can be used to key in values for more than one variable, such as
50 PRINT “TYPE IN EMPLOYEE NAME AND SALARY”
60 INPUT N$, X
In this case, the computer will provide the following response on the terminal and waits for the programmer’s response.
TYPE IN EMPLOYEE NAME AND SALARY
The response might be:
SUNDER, 15000 Carriage return
SUNDER is the name of the employee and 15000 is his salary. It should be noted that the constants are separated by ‘comma’.
The response such as 15000, SUNDER is wrong because of mismatch of variables.
Points to note while using an INPUT statement:
1) You can have a list of variables in an INPUT statement but they should be separated by commas.
2) The order and type of your response should match with the order and type of variables appearing in the INPUT list.
3) The response values which you key in should be separated by commas.
4) The computer will not proceed further until you key in the value for the last variables in the list followed by the carriage return. This makes the INPUT statement suitable to only interactive mode of operation.
If there is a large amount of data to be processed, it is very inconvenient or impractical to key in the entire data during the program execution through INPUT statements. In Such cases READ statements are found useful. The READ statement is always used in conjunction with a DATA statement.
They are used in the following form:
READ [variable1, variable2,….., ]
DATA [ constant1, constant2, ……,]
A typical use of READ ….. DATA is shown below.
10 REM ***********
20 READ X, Y, Z
30 READ A, B, C
90 DATA 8, 7, 6, 5, 4, 3
When the program is run, it encounters the first READ statement numbered 20 with a list of variables X, Y, Z and the first three values from the DATA statement are read into these variables in order and in one-to-one correspondence. That is, it lets
X = 8, Y = 7, Z = 6
When the computer comes to the second READ statement numbered 30, the computer would let
A = 5, B = 4, C = 3
A DATA statement can be anywhere in the program but not after the END statement. However, it is normal practice to keep all the DATA statement together at the end of the program ( just before the END statement ), so that data can be easily changed, if necessary, at a later stage.
Points to be noted while using the READ …. DATA statements are:
– There should be an exact correspondence between the types of variables in READ statements and the types of values in DATA statements.
– There could be any number of DATA statements.
– Data can be in excess of the variables and there is no need that every item in a DATA statement be actually read.
– If the values in the DATA statements are less than the number of variables in the READ statements an execution time error will occur and a message OUT OF DATA will result.
– We can use more than one DATA statement to provide data to one READ statement.
There are occasions where we may have to read a set of data more than once in a program than the RESTORE statement is useful.
This statement resets the DATA statement pointer to the first item in the DATA statement.
10 READ A, B, C, D, E
20 PRINT A, B, C, D, E
40 READ F, G
50 PRINT F, G
60 DATA 1, 2, 3, 4, 5
1 2 3 4 5
What is REM?
REM is a non-executable statement and stand for Remark. It is meant to be ready by people, and the computer does not pay any attention to what is said in a REM statement.
|What is Keyword? Give an example.|
|Keyword means Reserved or in-built word. i.e. PRINT,CLS,INPUT,LET|
The captions and numerical results can be printed out using the PRINT statement. It is highly desirable that the outputs are printed in such a way that they are understandable and in an easy-to-use form.
The PRINT statement of BASIC provides, in a limited way, certain ways of controlling the alignment and spacing of print-outs in terminals.
The general form of the PRINT statement is:
PRINT [item] Separator [item] Separator …..
The item may represent one of the following:
1. A constant or a variable or an expression,
2. A character string, or
3. The special TAB function.
The ‘Separator can be a comma or a semicolon.
Some examples of PRINT statements are:
PRINT X*Y – X/Y
PRINT X, Y, Z
PRINT X; Y; Z
PRINT X, Y; Z
THE COMMA CONTROL(,)
The output medium in BASIC (either paper page or visual display screen) is usually divided into five equal zones. The width of each zone varies from 12 to 15 spaces with a space of one of to four spaces between the zones. A comma in a PRINT causes the print of the value following:
10 PRINT “NUM”,”TEMP”,”SIZE”,,”REMARK”
20 PRINT 65, -15.56, 36.64
Will print out the results as follows:
1 Zone1 17 Zone 2 33 Zone 3 49 Zone 4 65 Zone 5
The NUM is printed in the first zone, the TEMP in the second zone, and the SIZE in the third zone. The consecutive commas after SIZE causes the REMARK to be printed in the fifth zone. Thus, blank spaces can be inserted after the separator to improve the output format of the result.
The PRINT statement in line 20 causes the numbers to the printed out in the first three zones of the next line. Note that one space is reserved for the sign of the number. The + sign is not usually printed, instead, a blank space appears before the number.
10 READ X, Y, Z, A, B, C, D
20 PRINT X, Y, Z,
30 PRINT A, B, C, D
40 DATA 10, 15, 20, 25, 30, 35, 40
10 15 20 25 30
THE SEMICOLON CONTROL(;)
The major limitation of the use of comma is that we can not print more than five items in a line. On many occasions it is advantageous to print more items in each line.
A semicolon causes the carriage to stop immediately after printing the preceding item and wait for printing the next item. Thus, the items which are separated by semicolons will be printed close together. The computer automatically provides one or two spaces between two items. If a semicolon is given at the end of a PRINT statement, this will suppress the carriage return and the output of the next PRINT statement will continue on the same line.
20 PRINT 10; 15; 20; 25;
30 PRINT 30; 35; 40
50 PRINT “END”
The PRINT USING statement is used to print string or numeric data in a predefined format,
PRINT USING format$;expressions
Where, format$ is a string constant or variable consisting of the formatting characters expressions consist of string or numeric items that are to be printed.
Example : PRINT USING “##.##”;7.2,23.5,53.777,.375
7.20 23.50 53.78 .38
There are other formatting characters which determine the manner in which the string or numeric data specified will be printed.
An exclamation symbol (!) specifies that only the first character in the string is to be printed.
Example : 10 A$ = “BOBBY”
20 B$ = “VANDER”
30 PRINT USING “!”;A$; B$
A plus sign (+) can be indicated at the beginning or at the end of the format string. This causes the number’s sign (+ or -), to be printed before the number.
Example : PRINT USING “+##.##”;42.7,-.7,14.2
+42.70 -.70 +14.20