Introduction To Basic Programming
Introduction To Basic Programming
TOPICS
Basic Character Set Variables Constants Strings Arithmetic Operators Expressions
TOPICS
Statement or Line numbers The LET Statement The READ Statement
TOPICS
The PRINT USING Statement The END Statement The STOP Statement The REM Statement The RESTORE Statement The GOTO Statement The IF-THEN Statement The IF-THEN-ELSE Statement The FOR-TO-STEP Statement
BASIC PROGRAMMING
Basic is an Acronym for Beginners
Darmouth College by John Kemeny & Thomas Kurtz in the mid 1960s
It was intended to be an easy-to-
learn programming language that students could use to solve simple problems
SAMPLE PROGRAM
Output:
10 20 30 40
Hello! My name is
SAMPLE PROGRAM
10 CLS 20 PRINT "Hello, world!" 30 PRINT "I'm learning about commands in BASIC." 40 PRINT
command." "This text is being printed via the PRINT
50 PRINT "On the next line, I'll use CLS." 60 CLS 70 PRINT "Finally, on line 80, I'll use END."
CLS
An abbreviation that stands for the words CLear Screen. In the above program, when you used CLS on line 60, all of the words that were printed to the screen were wiped away.
PRINT
Writes to the screen or displays output on the screen. Each new PRINT command will start printing on a new line.
END
It stops the program at that line; that is, anything that's added after that won't show. That's why the PRINT command on line 90 didn't print anything. The END command can be included in control structures to end the program if a condition is met. This will be discussed with control structures.
command clears BASIC's internal memory and prepares it for a new program.
The SAVE
command stores
BASIC COMMANDS
To finish your session in the
BASIC environment, first SAVE your work with the BASIC SAVE command. Then terminate the session with the command EXIT.
STATEMENTS & LINE PRIMARY Functions of Line Numbers NUMBERS A. They are used as labels. The
sequence can be referred to by its statement number.
B. They indicate the order in which the
statements are to be executed by the computer. When the computer executes a program, it starts by following or
. .
numbers)
STATEMENTS & LINE Between 1 and the largest number NUMBERS specified by the computer system.
Most line number 1 to 9999 are
largest
They do not need to be in numerical
sequence
There can be unequal intervals
between them
the equal
sign is assigned to the variable on
the left.
The word LET is optional,
to suspend execution of a program and wait until the user has entered the specified number and type of values through the keyboard.
INPUT accepts a numeric or string
variable.
Data must be entered one at a time
or separated by commas.
LINE # INPUT item list LINE # INPUT prompt statement; item list
Ex.
computer to read in a value from a DATA statement and assign that value to the specified variable.
When Basic comes to a READ
statement containing a list of variables, it looks for a DATA statement containing a list of values. It reads the data values one at a time, in the order listed and
computer to read in a value from a DATA statements and assign that value to the specified variable.
The list can contain ordinary numeric
computer to read in a value from a DATA statements and assign that value to the specified variable.
The list can contain ordinary numeric
statement number, the keyword READ followed by a list of INPUT variables separated by commas. The THE READ STATEMENT list can contain ordinary numeric and/or string variables
General Form:
Ex.
colons/semicolons.
Ex.
(mapping) & in type THE DATA STATEMENT to the variables specified by the READ Statement. b) There must at least be as many data elements in the DATA block as there are variables in the READ
DATA must be numeric or string in nature, not variables or formula. e) Strings containing commas or beginning or ending with blank spaces must be enclosed in quotation
f) DATA statements
should (but do not have to) placed consecutively near the end of the program.
portion of one line. STATEMENT THE ThePRINT item can be any of the following: numeric contants, numeric variables, or expressions
Commas may be used
Ex.
A) 200 PRINT THE ANSWER IS = ; A B) 210 PRINT 10,20,30,400 C) 220 PRINT (A+B),(X-Y) D) 230 PRINT 5*X-2
unless the list presented by PRINT the PRINT statement THE STATEMENT requires more than one line
b. A PRINT statement
THE LPRINT STATEMENT The LPRINT statement is used to print out data onto a line printer or a hard-copy terminal
The LPRINT and PRINT
statements may not be used at the same time in running a program. The programmer must decide
Ex.
A) 200 LPRINT C,D,E,S$ B) 210 LPRINT THE VALUE OF INSURANCE IS;IN C) 220 LPRINT THE ANSWER IS = ; A
THE PRINT TAB STATEMENT The TAB is a special function used in conjunction with the PRINT or LPRINT statement. It permits the programmer to specify the exact positioning of each output item listed in a PRINT or LPRINT statement.
Ex.
A) 200 PRINT TAB (1) TOTAL SALES B) 210 PRINT TAB (15) AREA C) 220 PRINT TAB (J) NUMBER
THE PRINT USING STATEMENT PRINT USING is used for displaying special formats, primarily currency amounts , accounting values, engineering or scientific data, or any other data which have to be tabulated.
the proper position of each digit in the item list. THE PRINT USING STATEMENT
b) The decimal point, . can
be placed anywhere in the format field established by the #. Rounding off will take place if the digits to the right of the decimal point are suppressed .
c) The comma, , can be
the field will cause all unused positions the left THE PRINT USING to STATEMENT of the decimal point to be filled with asterisks.
e) Two dollar signs, $$,
placed at the beginning of the format field will act as a floating dollar sign.
f) A combination of the **
42.35
%2.35 **42.35 $42.35 **$42.35 ***$42
Ex.
A) 200 PRINT USING ##.##;A B) 210 PRINT TAB(10) USING $ $###.##;C C) 220 PRINT USING +##.##;AMT
THE END STATEMENT The END statement is primarily used to cause execution to terminate at some point other than the logical end of the program. END Causes the BASIC interpreter to stop program execution & return to the command mode.
LINE # END
Ex.
that the rest of the line only consists of comments, and should be ignored.
Used to document a