Unformatted text preview: QBASIC Chapter 3
QBASIC Chapter 3
Calculations, Strings, and An Introduction
Calculations,
to the TopDown Approach
to
Created by Richard D. Taylor
Illustrated by Saundra J. Taylor Introduction
Introduction
Focus of the chapter Constants, variables, expressions, functions,
Constants,
rounding and truncation techniques
rounding
Type of data that can be assigned to variables
Presents topdown, or modular approach to
Presents
solving problems
solving Constants
Constants
Numeric Constants  one of three forms: Integer  a positive or negative whole number
Integer
with no decimal point
with
Fixed Point  a positive or negative real number
Fixed
with a decimal point
with
Exponential Form  A number written as an
Exponential
integer or fixed point constant followed by the
letter D or E and an integer (D or E stand for 10
times the power)
times Constants
Constants
Numeric Constants in Exponential Form expressed as a value between 1 and 10
expressed
multiplied by a power of 10
multiplied
1.5 X 106 written as 1.5E6 or 1.5 D6
Numeric Constants in Scientific Notation represents the fact that the decimal point was
represents
moved X places to the right
moved
0.000000001234 written as 1.234E9 / 1.234D9 Constants
Constants
Type and Range of Numeric Constants Short Integer 32,768 to + 32,767
Long Integer 2,147,483,648 to +2,147,483,648
Single Precision 3.4DE+38 to 3.40E+38 with
Single
up to 7 digits of significance
up
Double Precision 1.8+3D8 to +1.790+3D8 with
Double
up to 15 digits of significance
up Constants
Constants
String Constants has as its value the string of all characters
has
between surrounding quotation marks
between
Length  0  32,767 characters
Null String 0r Empty String  a string with a
Null
length of zero
length
INPUT “Customer’s Name”; first.name
PRINT first.name$: “’s neck size is”; Neck.size Variables
Variables
Simple Variables  used to store single values
Subscripted Variable  used to store groups of
Subscripted values
values
Numeric Variable  assigned only a numeric
Numeric
value
value
String Variable  assigned a string of characters Variables
Variables
Selection of Variable Names can be up to 40 characters in length
must begin with a letter, followed by up to 39
must
characters (letters, digits, or periods)
characters
if a variable name ends with a $, it is labeled a string
if
variable
variable
Reserved words cannot be used, as LET, PRINT,
Reserved
and END
and Variables
Variables
Selection of Variable Names not casesensitive
make variable names as meaningful as possible The LET Statement
The LET Statement
Form: LET numeric variable = numeric
LET
expression
expression
LET string variable = string
LET
expression
expression
Purpose:Causes the evaluation of the
Purpose:Causes
expression, followed by the assignment of
the resulting value to the variable to the left
of the equal sign
of The LET Statement
The LET Statement
Examples:
LET Perimeter = 2 * Side l + 2 * Side2
LET Q = (B + A) / 2  Q + R
Count = Count + 1
NOTE: the keyword LET is optional Expressions
Expressions
Numeric Expressions consists of one or more numeric constants,
consists
numeric variables, and numeric function
references, all of which are separated from each
other by parentheses and arithmetic operators
other
Modulo Operator returns the integer remainder of integer
returns
division
division Expressions
Expressions
String Expression consists of one or more string constants, string
consists
variables and string function references
separated by the concatenation operator (+)
separated
Concatenation Operator (+) which joins two strings into one Expressions
Expressions
Formation of Numeric Expressions the definition of a numeric expression dictates the
the
manner in which a numeric expression is to be
validly formed
validly
A = 2B
A=2*B
Evaluation of Numeric Expressions reading from left to right, then exponentiations, then
reading
multiplication /division, and addition/subtraction
multiplication Expressions
Expressions
Parentheses may be used to change the order
Parentheses of operation and are normally used to avoid
ambiguity and to group terms in a numeric
expression
expression () Expressions
Expressions
Construction of ErrorFree Numeric Expression Do not attempt to divide by zero
Do not attempt to determine the square root of a
Do
negative value
negative
Do not attempt to raise a negative value to a
Do
nonintegral value
nonintegral
Do not attempt to compute a value that is greater
Do
than the largest permissible value or less than the
smallest permissible nonzero value for the PC
smallest Expressions
Expressions
Numeric Functions INT Function  returns the largest integer not
INT
greater than the argument
Argument  a number or numeric expressions
Argument
surrounded by parentheses that immediately
follow the word
follow
SQR Function returns the square root of the positive argument Expressions
Expressions
Rounding and Truncation  Steps to Hat.Size = Hat.Size + .005
Hat.Size = Hat.Size * 100
Hat.Size = INT(Hat.Size)
Rounded.Hat.Size = Hat.size / 100 Expressions
Expressions
String Expression joining of two strings into one string
PRINT City$ + “,” + ST$ Expressions
Expressions
Use of LEFT$, LEN, MID$, and RIGHT$
Use String Functions
String
LEFT$(X$, N)  returns the left most N characters
LEFT$(X$,
of the string argument X$
of
MET(X$)  returns the number of characters in the
MET(X$)
value associated with the string argument X$
value NOTE: Where X$ is a string expression, and N
NOTE:
and P are numeric expressions
and Expressions
Expressions
Use of LEFT$, LEN, MID$, and RIGHT$
Use String Functions
String
MDI$(X$,P,N)  returns N characters of the string
MDI$(X$,P,N)
argument X$ beginning at P
argument
RIGHT$(N$,N)  returns the rightmost N characters
RIGHT$(N$,N)
of the string argument X$
of NOTE: Where X$ is a string expression, and N
NOTE:
and P are numeric expressions
and The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Topdown Approach  to take the original
Topdown problem and break it into smaller and more
manageable subproblems, each of which is
easier to solve than the original one
easier The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Four Major Steps Problem analysis
Topdown design  a strategy that breaks large,
Topdown
complex problems into smaller less complex
problems and then decomposes each of these into
even smaller problems
even The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Four Major Steps Topdown programming  a strategy that codes
Topdown
highlevel modules as soon as they are designed
and generally before the lowlevel modules have
been designed
been
Topdown testing and debugging  a strategy that
Topdown
tests and debugs the highlevel modules of a
system before the lowlevel modules have been
coded, possibly before they have been designed
coded, The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
TopDown Design Highlevel design  identifies what tasks need to
Highlevel
be performed
be
Detailed design  addresses how the tasks should
Detailed
be performed
be The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
HighLevel Design Identify the overall problem
Identify the subproblems, each subproblem may
Identify
be further subdivided  this process continues until
a level is reached where each problem identified is
easily comprehended
easily The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
TopDown Chart, Hierarchy Chart or Visual
TopDown Table of Contents (VTOC)  graphical
representation of the topdown design approach
representation
Topdown Chart differs from a program
Topdown
flowchart in that it does not show decisionflowchart
making logic but it shows organization and
making
functionality
functionality The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Peer Review Group  composed of programmers
Peer and analysts for further review and refinement,
also called structured walkthrough
also The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Detailed Design how the tasks in the topdown chart should be
how
performed
performed
Implementing a TopDown Design Using
Implementing subroutines (Modules)
subroutines
Subroutine or Module  each task in the topdown
Subroutine
chart
chart
Main module  highest level module in a program
Main
(level 0)
(level The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Form: GOSUB label  where label is a line
GOSUB
label or line number that designates the
beginning of a subroutine
beginning
Purpose: Causes control to transfer to the
Causes
subroutine represented by label
Causes the location of the next executable
Causes
statement following the GOSUB to be retained
statement The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Examples: GOSUB A100.Initialization
GOSUB B200.Compute.Cost NOTE: In Qbasic, if label is a line label, rather
NOTE:
than a line number, then it must be followed
immediately by a colon
immediately The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Form: RETURN
Purpose: Causes control to transfer from the
Causes
subroutine back to the first executable statement
immediately following the GOSUB statement
that referenced it
that
Examples:
RETURN
NOTE: The RETURN statement may be followed by a label The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Recommended Style and Tips When Using the
Recommended TopDown Approach
TopDown
Start each subroutine with a comment box  a
Start
comment box consists of three or more remark lines
with asterisks surrounding a brief comment
with
End each subroutine with a blank line
Indent by three spaces the statements between the
Indent
subroutine name and the RETURN statement
subroutine The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Recommended Style and Tips When Using
Recommended the TopDown Approach
the
So that lowerlevel subroutines can be located
So
easily for debugging purposes, they should be
placed below the subroutine which calls them and
in the order in which they are numbered
in The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
More About Subroutines Last statement should always be the RETURN
Last
statement, otherwise diagnostic message RETURN
diagnostic
without GOSUB
without
Nested Subroutines one subroutine calling another subroutine but each
one
must have their own RETURN
must
Control stack  maintain locations of return when
Control
RETURN statements are executed
RETURN The TopDown (Modular) Approach and the The TopDown (Modular) Approach and the GOSUB and RETURN Statements
Stubs a skeleton version of the final subroutineit does
skeleton
not contain details or fulfill program tasks, it simply
exists as the target of a GOSUB statement
exists Summary Slide
Summary Slide
Introduction
Constants
Variables
The LET Statement
Expressions
The TopDown (Modular) Approach and
The the GOSUB and RETURN Statements
the ...
View
Full Document
 Spring '11
 Grigoletti
 Subroutine, Return statement, Control flow, Evaluation of Numeric Expressions

Click to edit the document details