Qbasic Looping Statements Formatted Output 2000 John Urrutia
Qbasic Looping Statements & Formatted Output © 2000 John Urrutia. All rights reserved. 1
Here we go Loop de Loop l. A loop is a set of statements that are executed repeatedly. l. Types èControlled w Pre-test w Post-test èInfinite © 2000 John Urrutia. All rights reserved. 2
Infinite Loops l. Generally a bad thing. l. Keeps going and … Going Going Going © 2000 John Urrutia. All rights reserved. 3
Controlled Loops l. Governed by a condition. èLogical w Sentinel èMathematical w Counter èEnvironmental w EOF() © 2000 John Urrutia. All rights reserved. 4
DO…LOOP syntax Post-Test Pre-Test Infinite DO { WHILE | UNTIL } condition statements LOOP { WHILE | UNTIL } condition © 2000 John Urrutia. All rights reserved. 5
Condition l. A comparison of two or more things. l. The comparison will result in: è TRUE state èFALSE state © 2000 John Urrutia. All rights reserved. 6
DO WHILE…LOOP Statement l. Pretest loop èIf condition is true execute DO WHILE DAY$ = YES$ PRINT “Is it Night yet? ” LOOP © 2000 John Urrutia. All rights reserved. 7
DO…LOOP UNTIL Statement l. Posttest loop èExecutes at least once DO PRINT “DO-WAH-DIDDY” LOOP UNTIL The. Cows = Come. Home © 2000 John Urrutia. All rights reserved. 8
Boolean or Logical Expressions l. Used in all conditions l. Algebra created by George Boole l. Always evaluates to a binary state èGenerally: w 1 is TRUE w 0 is FALSE © 2000 John Urrutia. All rights reserved. 9
Relational Expressions l. Single relational operator two operands è< Less than è> Greater than è= Equal to è<= Less than or equal to è>= Greater than or equal to è<> Not equal to © 2000 John Urrutia. All rights reserved. 10
Comparisons l. Numeric comparisons are simple. èCompares bit for bit èNegative numbers are stored in 2’s compliment w +110 w +010 w -110 w -210 w -310 = = = 000016 FFFF 16 FFFE 16 FFFD 16 = = = 0000 1111 1111 000012 00002 11112 1111 11102 1111 11012 © 2000 John Urrutia. All rights reserved. 11
Comparisons l. Strings are based on the collating sequence (ASCII shown below) è“ 1”char =4810 =3016 =0011 00002 è“ 9”char =5710 =3916 =0011 10012 è“A”char =6510 =4116 =0100 00012 è“Z”char =9010 =5 A 16 =0101 10102 è“a”char =9710 =6116 =0110 00012 è“z”char =12210 =7 A 16 =0111 10102 © 2000 John Urrutia. All rights reserved. 12
When is an “A” not an “a”? l. When comparing strings the case counts. èUse the UCASE$() function to limit the number of options from your user. © 2000 John Urrutia. All rights reserved. 13
Compound Conditions l. When 2 or more expressions are combined together. l. Used to specify complex conditions in one statement. © 2000 John Urrutia. All rights reserved. 14
Boolean Operators l. NOT – negation (bit-wise complement) l. AND – logical addition (conjunction) l. OR – logical subtraction (disjunction) l. XOR – exclusive “or” l. EQV – logical equivalence l. IMP – logical implication © 2000 John Urrutia. All rights reserved. 15
NOT Boolean Truth Tables Expr NOT True False True © 2000 John Urrutia. All rights reserved. 16
AND Boolean Truth Tables Expr 1 Expr 2 AND True False True False © 2000 John Urrutia. All rights reserved. 17
OR Boolean Truth Tables Expr 1 Expr 2 True True False False OR © 2000 John Urrutia. All rights reserved. 18
Nested Loops l. A loop within a loop ones% = 0 tens% = 0 DO WHILE tens% < 10 DO WHILE ones% < 10 PRINT tens% ; “-“ ; ones% = ones% + 1 LOOP ones% = 0 tens% = tens% + 1 LOOP © 2000 John Urrutia. All rights reserved. 19
More Formatted Output l. TAB(n) è n – represents the column number to tab to l. SPC(n) è n – represents the number of spaces to insert © 2000 John Urrutia. All rights reserved. 20
TAB examples PRINT TAB(10); “ 10”; TAB(20); “ 20”; TAB(30); “ 30” 000001111122222333334 12345678901234567890 10 20 30 © 2000 John Urrutia. All rights reserved. 21
SPC example PRINT SPC(10); “ 10”; SPC(10); “ 20”; SPC(10); “ 30” 000001111122222333334 12345678901234567890 10 20 30 © 2000 John Urrutia. All rights reserved. 22
The PRINT USING statement l. Writes formatted data to the teminal PRINT USING “format-string” ; output-list l. The format-string specifies èNumeric edited data formats èString formats èLiteral data © 2000 John Urrutia. All rights reserved. 23
USING format characters l. Strings èn – first n +2 characters in the string è! – first character in the string è& – no formatting è_ – print character not format © 2000 John Urrutia. All rights reserved. 24
USING format characters l. Numbers è# – number digit è. – decimal point è, – thousands separator è+ – sign of number è- – trailing minus sign è$ $$ – fixed / floating dollar sign © 2000 John Urrutia. All rights reserved. 25
PRINT USING example. S 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! © 2000 John Urrutia. All rights reserved. 26
Qbasic screen manipulation The LOCATE statement © 2000 John Urrutia. All rights reserved. 27
LOCATE l. LOCATE – position on screen èrow% , column% ècursor% èstart% èstop% l. CSRLIN – line cursor is on l. POS(0) – column cursor is on © 2000 John Urrutia. All rights reserved. 28
- Slides: 28