1 © 2000 John Urrutia. All rights reserved. Qbasic Looping Statements & Formatted Output
2 © 2000 John Urrutia. All rights reserved. Here we go Loop de Loop A loop is a set of statements that are executed repeatedly. Types Controlled Pre-test Post-test Infinite
3 © 2000 John Urrutia. All rights reserved. Infinite Loops Generally a bad thing. Keeps going and going and … Going
4 © 2000 John Urrutia. All rights reserved. Controlled Loops Governed by a condition. Logical Sentinel Mathematical Counter Environmental EOF()
5 © 2000 John Urrutia. All rights reserved. Infinite DO…LOOP syntax DO statements LOOP Pre-Test { WHILE | UNTIL } condition Post-Test
6 © 2000 John Urrutia. All rights reserved. Condition A comparison of two or more things. The comparison will result in: TRUE state FALSE state
7 © 2000 John Urrutia. All rights reserved. DO WHILE…LOOP Statement Pretest loop If condition is true execute DO WHILE DAY$ = YES$ PRINT “Is it Night yet?” LOOP
8 © 2000 John Urrutia. All rights reserved. DO…LOOP UNTIL Statement Posttest loop Executes at least once DO PRINT “DO-WAH-DIDDY” LOOP UNTIL The.Cows = Come.Home
9 © 2000 John Urrutia. All rights reserved. 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
10 © 2000 John Urrutia. All rights reserved. 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
11 © 2000 John Urrutia. All rights reserved. Comparisons Numeric comparisons are simple. Compares bit for bit Negative numbers are stored in 2’s compliment = = = = = FFFF 16 = = FFFE 16 = = FFFD 16 =
12 © 2000 John Urrutia. All rights reserved. Comparisons Strings are based on the collating sequence (ASCII shown below) “1” char =48 10 =30 16 = “9” char =57 10 =39 16 = “A” char =65 10 =41 16 = “Z” char =90 10 =5A 16 = “a” char =97 10 =61 16 = “z” char = =7A 16 =
13 © 2000 John Urrutia. All rights reserved. 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.
14 © 2000 John Urrutia. All rights reserved. Compound Conditions When 2 or more expressions are combined together. Used to specify complex conditions in one statement.
15 © 2000 John Urrutia. All rights reserved. Boolean Operators NOT– negation (bit-wise complement) AND– logical addition (conjunction) OR– logical subtraction (disjunction) XOR– exclusive “or” EQV– logical equivalence IMP– logical implication
16 © 2000 John Urrutia. All rights reserved. NOT Boolean Truth Tables Expr NOT FalseTrue FalseTrue
17 © 2000 John Urrutia. All rights reserved. AND Boolean Truth Tables Expr 1Expr 2 AND True FalseTrue False TrueFalse
18 © 2000 John Urrutia. All rights reserved. OR Boolean Truth Tables True FalseTrue FalseTrue False Expr 1Expr 2 OR
19 © 2000 John Urrutia. All rights reserved. Nested Loops A loop within a loop ones% = 0 tens% = 0 DO WHILE tens% < 10 DO WHILE ones% < 10 PRINT tens% ; “-“ ; ones% ones% = ones% + 1 LOOP ones% = 0 tens% = tens% + 1 LOOP
20 © 2000 John Urrutia. All rights reserved. More Formatted Output TAB( n ) n – represents the column number to tab to SPC( n ) n – represents the number of spaces to insert
21 © 2000 John Urrutia. All rights reserved. TAB examples PRINT TAB(10); “10”; TAB(20); “20”; TAB(30); “30”
22 © 2000 John Urrutia. All rights reserved. SPC example PRINT SPC(10); “10”; SPC(10); “20”; SPC(10); “30”
23 © 2000 John Urrutia. All rights reserved. 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
24 © 2000 John Urrutia. All rights reserved. USING format characters Strings \n\ – first n +2 characters in the string ! – first character in the string & – no formatting _ – print character not format
25 © 2000 John Urrutia. All rights reserved. USING format characters Numbers # – number digit . – decimal point , – thousands separator + – sign of number - – trailing minus sign $ $$ – fixed / floating dollar sign
26 © 2000 John Urrutia. All rights reserved. PRINT USING example S PRINT USING “A=# and B=$#,###.##”; 5; A=5 and B=$1, PRINT USING “You’re a \ \ and I’m a & _!”; “nutria”; “foolish” You’re a nut and I’m a fool!
27 © 2000 John Urrutia. All rights reserved. Qbasic screen manipulation The LOCATE statement
28 © 2000 John Urrutia. All rights reserved. LOCATE LOCATE – position on screen row%, column% cursor% start% stop% CSRLIN – line cursor is on POS(0) – column cursor is on