300 likes | 539 Views
Qbasic. Looping Statements & Formatted Output. Here we go Loop de Loop. A loop is a set of statements that are executed repeatedly. Types Controlled Pre-test Post-test Infinite. Infinite Loops. Generally a bad thing. Keeps going and going and …. Going. Going. Going. Going. Going.
E N D
Qbasic Looping Statements & Formatted Output
Here we go Loop de Loop • A loop is a set of statements that are executed repeatedly. • Types • Controlled • Pre-test • Post-test • Infinite
Infinite Loops • Generally a bad thing. • Keeps going and going and … Going Going Going Going Going Going Going Going Going
Controlled Loops • Governed by a condition. • Logical • Sentinel • Mathematical • Counter • Environmental • EOF()
DO…LOOP syntax Post-Test Pre-Test Infinite DO statements LOOP { WHILE | UNTIL } condition { WHILE | UNTIL } condition
Condition • A comparison of two or more things. • The comparison will result in: • TRUE state • FALSE state
DO WHILE…LOOP Statement • Pretest loop • If condition is true execute DO WHILE DAY$ = YES$ PRINT “Is it Night yet?” LOOP
DO…LOOP UNTIL Statement • Posttest loop • Executes at least once DO PRINT “DO-WAH-DIDDY” LOOP UNTIL The.Cows = Come.Home
Boolean or Logical Expressions • Used in all conditions • Algebra created by George Boole • Always evaluates to a binary state • Generally: • 1 is TRUE • 0 is FALSE
Relational Expressions • Single relational operator two operands • < Less than • > Greater than • = Equal to • <= Less than or equal to • >= Greater than or equal to • <> Not equal to
Comparisons • Numeric comparisons are simple. • Compares bit for bit • Negative numbers are stored in 2’s compliment • +110 = 000016 = 00000000 000000012 • +010 = 000016 = 00000000 000000002 • -110 = FFFF16 = 11111111 111111112 • -210 = FFFE16 = 11111111 111111102 • -310 = FFFD16 = 11111111 111111012
Comparisons • Strings are based on the collating sequence (ASCII shown below) • “1”char =4810 =3016 =001100002 • “9”char =5710 =3916 =001110012 • “A”char =6510 =4116 =010000012 • “Z”char =9010 =5A16 =010110102 • “a”char =9710 =6116 =011000012 • “z”char =12210 =7A16 =011110102
When is an “A” not an “a”? • When comparing strings the case counts. • Use the UCASE$() function to limit the number of options from your user.
Compound Conditions • When 2 or more expressions are combined together. • Used to specify complex conditions in one statement.
Boolean Operators • NOT – negation (bit-wise complement) • AND – logical addition (conjunction) • OR – logical subtraction (disjunction) • XOR – exclusive “or” • EQV – logical equivalence • IMP – logical implication
NOT Boolean Truth Tables NOT True False False True
AND Boolean Truth Tables AND True True True False True False False True False False False False
OR Boolean Truth Tables OR True True True True True False False True True False False False
Nested Loops • A loop within a loop ones% = 0tens% = 0 DO WHILE tens% < 10 DO WHILE ones% < 10 PRINT tens% ; “-“ ; ones% ones% = ones% + 1 LOOP ones% = 0 tens% = tens% + 1LOOP
More Formatted Output • TAB(n) • n – represents the column number to tab to • SPC(n) • n – represents the number of spaces to insert
TAB examples PRINT TAB(10); “10”; TAB(20); “20”; TAB(30); “30” 00000000011111111112222222222333333333341234567890123456789012345678901234567890 10 20 30
SPC example PRINT SPC(10); “10”; SPC(10); “20”; SPC(10); “30” 00000000011111111112222222222333333333341234567890123456789012345678901234567890 10 20 30
The PRINT USING statement • Writes formatted data to the teminal PRINT USING “format-string” ;output-list • The format-string specifies • Numeric edited data formats • String formats • Literal data
USING format characters • Strings • \n\ – first n +2 characters in the string • ! – first character in the string • & – no formatting • _ – print character not format
USING format characters • Numbers • # – numberdigit • . – decimal point • , – thousands separator • + – sign of number • - – trailing minus sign • $ $$ – fixed / floating dollar sign
PRINT USING exampleS PRINT USING “A=# and B=$#,###.##”; 5; 1234.56 A=5 and B=$1,234.56 PRINT USING “You’re a \ \ and I’m a & _!”; “nutria”; “foolish” You’re a nut and I’m a fool!
Qbasicscreen manipulation The LOCATE statement
LOCATE • LOCATE – position on screen • row% , column% • cursor% • start% • stop% • CSRLIN – line cursor is on • POS(0) – column cursor is on