QBASIC

of 24

Please download to get full document.

View again

All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
PDF
24 pages
0 downs
3 views
Share
Description
QBASICChapter 2Modular Programs with Calculations and StringsQBASIC Character SetLetters:a-z and A-ZDigits:0-9Blank:the space character ( )Special characters:+ - * / \ =…
Transcript
QBASICChapter 2Modular Programs with Calculations and StringsQBASIC 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
  • “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.678amount = 345678.1234567Labels
  • A name assigned to represent a variable.
  • 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 = valueLET my.nbr! = 0LET 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% = 100LET B% = 4PRINT A% * B%PRINT A% + B% - B% * 4 MOD 3400PRINT A% + B% - B% * (4 MOD 3)103100Functions
  • 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
  • MKI$ - CVI, MKD$ - CVD
  • CHR$ - ASC
  • CINT(), CSGN(), CDBL()
  • System
  • DATE$ - TIME$
  • The INPUT statement
  • INPUT variable-listINPUT “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 usersINPUT “Want a date”; date.inWant a date?__INPUT “Enter date”, date.inEnter 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.nameGOSUB my.summationPRINT “The sum is “; x%ENDmy.summation: INPUT “Number Please:”, y% x%=(y-1)+(y-2)+(y-3)… RETURNRETURN [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 ScoreAverageProgramInputName & ScoresCalculateAverageOutputName &AverageStartInputScoresCalculateAverageWriteOutputInputname & scoresInputNameAvg1 = (3 Scores)/3PrintName &AverageInput 3ScoresReturnCalculateAverageAverage = Avg1 RoundReturnOutputName &AverageReturnEndFlowchartStartInputScoresCalculateAverageWriteOutputInputname & scoresInputNameAvg1 = (3 Scores)/3PrintName &AverageInput 3ScoresReturnCalculateAverageAverage = Avg1 RoundReturnOutputName &AverageReturnEndCodeMAIN:GOSUB Input.Name.ScoresGOSUB Calculate.AverageGOSUB Write.OutputEND.MAIN:ENDInput.Name.Scores:INPUT “Enter Name: “ , Name$INPUT “Enter Score 1: “ , Score1%INPUT “Enter Score 2: “ , Score2%INPUT “Enter Score 3: “ , Score3%RETURNCalculate.Average: Avg=INT((Score1%+ Score2%+ Score3%)/3)):RETURNWrite.Output: PRINT Name$; ” – “; Avg: RETURN
    Related Search
    We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks