Introduction to basic programming
TOPICS
Basic Character Set Variables Constants Strings Arithmetic Operators Expressions
Hierarchy of Operations Use of Parenthesis Numeric Precision
TOPICS
Statement or Line numbers The LET Statement The READ Statement
The INPUT Statement 4
5 The DATA Statement
The PRINT Statement The LPRINT 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
All-purpose Symbolic Instruction Code
It was originally developed at
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
CLS PRINT Hello! ; PRINT My name is ; PRINT Eloisa.
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.
BASIC COMMANDS The RUN command executes
the lines of the program in internal storage. It starts with the lowest line number and does whatever the lines of the program tell it to do until either the END statement or an error is encountered. Before entering the RUN command,
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.
have a statement or line number.
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
. .
executing the statement or instruction with the smallest
Line numbers must be integers (whole
numbers)
STATEMENTS & LINE Between 1 and the largest number NUMBERS specified by the computer system.
Most line number 1 to 9999 are
acceptable in most system.
Must be arranged from smallest to the
largest
They do not need to be in numerical
sequence
There can be unequal intervals
between them
LINE # LET variable = expression
THE LET STATEMENT
Ex. A) 50 LET A = 2*B/C B) 55 LET PI = 3.1416 C) 60 LET Q$ = HELLO The value of the term on the right of
the equal
sign is assigned to the variable on
the left.
The word LET is optional,
THE INPUT STATEMENT
This statement is used to enter the
data from the keyboard.
The statement causes the computer
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.
THE INPUT STATEMENT
General Form:
LINE # INPUT item list LINE # INPUT prompt statement; item list
Ex.
A) 10 INPUT R B) 20 INPUT ENTER ONE VALUE; V
THE READ STATEMENT
This statement instructs the
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
THE READ STATEMENT
This statement instructs the
computer to read in a value from a DATA statements and assign that value to the specified variable.
The list can contain ordinary numeric
and/or string variables.
The variable types in the DATA list
must correspond to the variable types in the READ statement.
THE READ STATEMENT
This statement instructs the
computer to read in a value from a DATA statements and assign that value to the specified variable.
The list can contain ordinary numeric
and/or string variables.
The variable types in the DATA list
must correspond to the variable types in the READ statement.
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:
LINE # READ list of variables A) 20 READ X1 B) 30 READ A,B,C,D C) 40 READ K,L,B$
Ex.
THE DATA STATEMENT The items can be string or numeric
values and the order of values in a DATA statement must match up with the variables types in the READ statement. Double quotes must enclose a string value if the string has leading blanks or embedded commas,
define a list of numeric or string values to be used in the program.
colons/semicolons.
THE DATA STATEMENT
General form:
LINE # DATA list of items
Ex.
A) 100 DATA 15.2,20,-30,18,-
B) 200 DATA BLUE, WHITE,RED,GREEN C) 300 DATA
(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
d) Elements of the THE DATA STATEMENT
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
THE DATA STATEMENT
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
between the items to tab to the next print zone
Semicolons is used to avoid
spaces between lines
THE PRINT STATEMENT
General form:
LINE # PRINT item list
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
containing no data item will produce a blank line
c. Strings must always be
enclosed with quotation marks
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
THE LPRINT STATEMENT
General form:
LINE # LPRINT item list
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.
THE PRINT TAB STATEMENT
General form:
LINE # PRINT TAB (Expression)
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
placed at any position
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 **
and $$ will fill empty
THE PRINT USING FORMAT DISPLAY STATEMENT
a.##.##
b. #.## c. **##.## d. $$#.## e. **$###.## f. **$###
42.35
%2.35 **42.35 $42.35 **$42.35 ***$42
THE PRINT USING STATEMENT
General form:
LINE # PRINT USING string, item list
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.
THE END STATEMENT
General form:
LINE # END
Ex.
A) 200 END B) 999 END
BASIC program execution. THE STOP STATEMENT
General form: LINE # STOP Ex.
A) 200 STOP B) 999 STOP
REM is an abbreviation for
THE REM STATEMENT Remark.
This informs the computer
that the rest of the line only consists of comments, and should be ignored.
Used to document a
program, Program heading, to identify program variables.