1 / 24

QBASIC

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.

shateque
Download Presentation

QBASIC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 • “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: + - . 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

  6. Numeric Constants 123+234-456+123.456.678amount = 345678.1234567

  7. Labels • A name assigned to represent a variable. • 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% = 100LET B% = 4PRINT 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 • MKI$ - CVI, MKD$ - CVD • CHR$ - ASC • CINT(), CSGN(), CDBL() • System • DATE$ - TIME$

  16. 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

  17. Using INPUT to prompt users INPUT “Want a date”; date.in Want a date?__ INPUT “Enter date”, date.inEnter 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.summationPRINT “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 ScoreAverageProgram InputName & Scores CalculateAverage OutputName &Average

  23. Start InputScores CalculateAverage WriteOutput Inputname & scores InputName Avg1 = (3 Scores)/3 PrintName &Average Input 3Scores Return CalculateAverage Average = Avg1 Round Return OutputName &Average Return End Flowchart Start InputScores CalculateAverage WriteOutput Inputname & scores InputName Avg1 = (3 Scores)/3 PrintName &Average Input 3Scores Return CalculateAverage Average = Avg1 Round Return OutputName &Average Return End

  24. Code MAIN:GOSUB Input.Name.ScoresGOSUB Calculate.AverageGOSUB 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

More Related