BEGINNING BASIC - part 2 BASIC is an high level language that can be used to solve a problem or perform some function on a computer. We can think of a program that we write in BASIC as a set of instructions for the computer to follow to perform some task Usually, a program will require some sort of data to operate on. It will then process the data as we have specified, and finally it will provide us with the result of that processing. The data that we give to a BASIC pro- gram is called INPUT. The answer that that we get back is called OUTPUT. As the program executes, the computer uses memory to store and retrieve data. In any high level language, there must be some convenient means for the program to retrieve, store, and process data easily. NUMBERS AND LITERALS: We've seen how the PRINT statement works, and how we can use our computer like a calculator: PRINT 5 PRINT 43 + 12 PRINT "HELLO THERE" Numbers, like 5, 43, 12, and literals, like "HELLO THERE", are what are known as CONSTANTS. Their value never changes. VARIABLES: A variable is a symbol used to represent a number or a character string. As the name implies, a variable can be changed by the programmer at any time. A variable is given a name, and through this name, we refer to some number or character string stored in memory. ASSIGNMENT STATEMENTS: A variable is assigned a value through what is know as an assignment statement. The LET statement lets us accomplish this: 10 LET X=1 20 PRINT X 30 END In this program, the variable X is given the value 1, and is then printed. The value of a variable can be changed at will: 10 LET X=1:LET X=2 20 PRINT X 30 END In this program, the variable X is fi rst assigned the value 1, and then is reassigned the value 2. When the program is RUN, the value 2 will be printed. The Statement LET is optional, and X=1, and LET X=1 do exactly the same thing: 10 X=1 20 PRINTX 30 END VARIABLE NAMES: You must follow these rules when giving names to variables: 1) The first character must be a letter. 2) The second character is optional, a letter or a number. 3) Only the first 2 characters in the variable name are significant. Names can be longer than 2 characters, but BASIC' sees' only the first 2 Characters. The names RATE RAT RA are all seen by BASIC as the variable RA. 4) The third character may be % if the variable is to be used to take on integer (whole number) values, or $, if the variable is to be used for character string val ues, like "OUR HOUSE", or "COMMODORE", EXAMPLES: A=5.3 numeric variable A%=5 integer variable A$="HELLO THERE" character variable Here are some examples of valid variable names: A X1 B4 CC J$ K% K3 NN$ Variables may be used in mathematical expressions or character string manipulations. Let's write a short program to add 2 numbers and print out the answer: 10 A=7 20 B=3.5 30 C=A+B 40 PRINT C 50 END THE INPUT STATEMENT: How do we get data to our program to process? This is accomplished by using the INPUT statement. When a program encounters this statement, it stops, and waits for the user to enter some input data. The user will be prompted with a ? symbol, and may then enter the data, termin- ated by pressing the RETURN key. Here's a simple example of a program to take a number from the user and print it out. 10 INPUT A 20 PRINT "YOU ENTERED THE NUMBER ";A 30 END The program should always prompt the user for the kind of input required. This can be done with a PRINT statement or the INPUT statement itself. 10 PRINT "ENTER A NUMBER" 20 INPUT A 30 PRINT "YOU ENTERED THE NUMBER ";A 40 END or: 10 INPUT "ENTER A NUMBER";A 20 PRINT "YOU ENTERED THE NUMBER ";A 30 END Let's write a program to take 2 number from the user, add them together, and print out the answer. 10 PRINT "ENTER THE FIRST NUMBER" 20 INPUT A 30 PRINT "ENTER THE SECOND NUMBER" 40 INPUT B 50 C=A+B 60 PRINT "THE SUM IS ";C 70 END INPUT can be used to enter more than one value at a time. 10 PRINT "ENTER THE 2 NUMBERS SEPARATED BY A COMMA" 20 INPUT A,B 30 C=A+B 40 PRINT C 50 END Try experimenting with variables, and the INPUT statement. Have Fun!