Chapter 2 Modular Programs with Calculations and Strings QBASIC Chapter 2 Modular Programs with Calculations and Strings
QBASIC Character Set Letters: a-z and A-Z Digits: 0-9 Blank: the space character ( ) Special characters: + - * / \ = < > . , ’ ” ( ) : ; ^ _ $ # ? ! % &
Qbasic Keywords A keyword has a predefined meaning within Qbasic. Examples: LET END REM PRINT
Constants & Variables Constants Variables “literal” values that cannot be changed May or may not have a label Variables Value can be changed Must be referenced by a label
QBASIC Constants Numeric Constants Can only contain these 13 characters: + - . 0 1 2 3 4 5 6 7 8 9 If not identified with a label they are called a “literal” Can not have + - internal or trailing Can only have one decimal
Numeric Constants 123 +234 -456 +123.456 .678 amount = 345678.1234567
Labels A name assigned to represent a variable. Should be meaningful. Must start with a letter Should be meaningful. Can have periods imbedded. Should carry the data type.
The LET statement Assigns a value to a variable. Can be Explicit or Implicit LET variable.Name = value LET my.nbr! = 0 LET my.str$ = “This is my string” LET tot! = tot! + purchases! + taxes!
QBASIC Data Types All data in QBASIC is identified by a data type Numbers % – Integer -32,768 to 32,767 & – Long integer -2,147,483,648 to 2,147,483,647 ! – Single precision 7 digit (1038 ) (Default data type) # – Double precision 15 digit (10308 )
QBASIC Data Types Strings: $ - data type identifier Any set of characters enclosed in double quotation marks. “ ” Maximum of 14,656 Characters
Arithmetic Operators * – Multiplication ^ – Exponentiation / – Division \ – Integer Division MOD – Modula (remainder) + – Addition - – Subtraction
Calculations Order of operations (precedence) ^ (Power) +, - (Unary) *, /, \, MOD, +, - (Arithmetic) <, <=, >, >=, <>, = (Relational) Boolean operators NOT, AND, OR, XOR, EQV, IMP
Some math Examples LET A% = 100 LET B% = 4 PRINT A% * B% PRINT A% + B% - B% * 4 MOD 3 400 PRINT A% + B% - B% * (4 MOD 3) 103 100
Functions A function is a set of instructions that perform a specific task. FunctionName (argument1, …) Built-in & User defined X$ = INT(X)
Built-in Functions Conversions System MKI$ - CVI, MKD$ - CVD CHR$ - ASC CINT(), CSGN(), CDBL() System DATE$ - TIME$
The INPUT statement INPUT variable-list INPUT “prompt ” ; or , variable-list prompt – any valid string ; – Question mark generated , – No Question mark generated variable-list – mix and match separate with commas
Using INPUT to prompt users INPUT “Want a date”; date.in Want a date?__ INPUT “Enter date”, date.in Enter date __
Modular Programming As we have seen previously: Heirarchy Charts allow us to break large problems into smaller more manageable blocks that perform a specific unit of work Each of these “blocks” can be represented as a “subroutine”. A fixed combination of instructions that can be re-used.
Subroutines A subroutine is identified with a label just like variables. Subroutines are “called” from someplace in the program using GOSUB and return to the statement following the call using the RETURN statement.
GOSUB label.name GOSUB my.summation PRINT “The sum is “; x% END my.summation: INPUT “Number Please:”, y% x%=(y-1)+(y-2)+(y-3)… RETURN
RETURN [ label.name] Returns to caller by default Command following the GOSUB gets control Returns to label if specified Should only be used under special circumstances.
Hierarchy Chart Score Average Program Input Name & Scores Calculate Average Output Name & Average
Flowchart Start End Return Start Return Return Return End Input name & scores Calculate Average Output Name & Average End Input Scores Write Output Input Name Input 3 Scores Return Avg1 = (3 Scores)/3 Average = Avg1 Round Print Name & Average Start Input Scores Calculate Average Write Output Input name & scores Input Name Avg1 = (3 Scores)/3 Print Name & Average Input 3 Scores Return Calculate Average Average = Avg1 Round Return Output Name & Average Return End
Code MAIN: GOSUB Input.Name.Scores GOSUB Calculate.Average GOSUB Write.Output END.MAIN: END Input.Name.Scores: INPUT “Enter Name: “ , Name$ INPUT “Enter Score 1: “ , Score1% INPUT “Enter Score 2: “ , Score2% INPUT “Enter Score 3: “ , Score3% RETURN Calculate.Average: Avg=INT((Score1%+ Score2%+ Score3%)/3)):RETURN Write.Output: PRINT Name$; ” – “; Avg: RETURN