Download presentation
Presentation is loading. Please wait.
Published byかずゆき よしなが Modified over 5 years ago
1
Chapter 2 Modular Programs with Calculations and Strings
QBASIC Chapter 2 Modular Programs with Calculations and Strings
2
QBASIC Character Set Letters: a-z and A-Z Digits: 0-9
Blank: the space character ( ) Special characters: + - * / \ = < > . , ’ ” ( ) : ; ^ _ $ # ? ! % &
3
Qbasic Keywords A keyword has a predefined meaning within Qbasic.
Examples: LET END REM PRINT
4
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
5
QBASIC Constants Numeric Constants
Can only contain these 13 characters: If not identified with a label they are called a “literal” Can not have + - internal or trailing Can only have one decimal
6
Numeric Constants amount =
7
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.
8
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!
9
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 )
10
QBASIC Data Types Strings: $ - data type identifier
Any set of characters enclosed in double quotation marks. “ ” Maximum of 14,656 Characters
11
Arithmetic Operators * – Multiplication ^ – Exponentiation
/ – Division \ – Integer Division MOD – Modula (remainder) + – Addition - – Subtraction
12
Calculations Order of operations (precedence) ^ (Power) +, - (Unary)
*, /, \, MOD, +, - (Arithmetic) <, <=, >, >=, <>, = (Relational) Boolean operators NOT, AND, OR, XOR, EQV, IMP
13
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
14
Functions A function is a set of instructions that perform a specific task. FunctionName (argument1, …) Built-in & User defined X$ = INT(X)
15
Built-in Functions Conversions System MKI$ - CVI, MKD$ - CVD
CHR$ - ASC CINT(), CSGN(), CDBL() System DATE$ - TIME$
16
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
17
Using INPUT to prompt users
INPUT “Want a date”; date.in Want a date?__ INPUT “Enter date”, date.in Enter date __
18
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.
19
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.
20
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
21
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.
22
Hierarchy Chart Score Average Program Input Name & Scores
Calculate Average Output Name & Average
23
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
24
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.