Transcript Qbasic Looping Statements & Formatted Output
Qbasic
Looping Statements & Formatted Output © 2000 John Urrutia. All rights reserved. 1
Here we go Loop de Loop
A loop is a set of statements that are executed repeatedly.
Types Controlled Pre-test Post-test Infinite © 2000 John Urrutia. All rights reserved. 2
Infinite Loops
Generally a bad thing.
Keeps going and going and …
Going Going Going Going Going Going Going Going Going
© 2000 John Urrutia. All rights reserved. 3
Controlled Loops
Governed by a condition.
Logical Sentinel Mathematical Counter Environmental EOF() © 2000 John Urrutia. All rights reserved. 4
DO…LOOP syntax DO
{ WHILE | UNTIL }
condition
statements
LOOP
{ WHILE | UNTIL }
condition
© 2000 John Urrutia. All rights reserved. 5
Condition
A comparison of two or more things.
The comparison will result in: TRUE state FALSE state © 2000 John Urrutia. All rights reserved. 6
DO WHILE…LOOP Statement
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
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
Used in all
conditions
Algebra created by George Boole Always evaluates to a binary state Generally: 1 is TRUE 0 is FALSE © 2000 John Urrutia. All rights reserved. 9
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 © 2000 John Urrutia. All rights reserved. 10
Comparisons
Numeric comparisons are simple.
Compares bit for bit Negative numbers are stored in 2’s compliment
+1 10
+0 10
-1 10
-2 10
-3 10 = 0000 16 = 0000 0000 000 00001 2 = 0000 16 = 0000 0000 0000 0000 2 = FFFF 16 = 1111 1111 1111 1111 2 = FFFE 16 = 1111 1111 1111 1110 2 = FFFD 16 = 1111 1111 1111 1101 2
© 2000 John Urrutia. All rights reserved. 11
Comparisons
Strings are based on the collating sequence
(ASCII shown below)
“1” char
=48
10
=30
16
=0011 0000 2
“9” char
=57
10
=39
16
=0011 1001 2
“A” char
=65
10
=41
16
=0100 0001 2
“Z” char
=90
10
=5A
16
=0101 1010 2
“a” char
=97
10
=61
16
=0110 0001 2
“z” char
=122
10
=7A
16
=0111 1010 2
© 2000 John Urrutia. All rights reserved. 12
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. © 2000 John Urrutia. All rights reserved. 13
Compound Conditions
When 2 or more expressions are combined together.
Used to specify complex conditions in one statement.
© 2000 John Urrutia. All rights reserved. 14
Boolean Operators
NOT AND OR XOR EQV IMP – negation (bit-wise complement) – logical addition (conjunction) – logical subtraction (disjunction) – exclusive “or” – logical equivalence – logical implication © 2000 John Urrutia. All rights reserved. 15
NOT Boolean Truth Tables
Expr
True False NOT False True
© 2000 John Urrutia. All rights reserved. 16
AND Boolean Truth Tables
Expr 1
True True False False
Expr 2
True False True False AND True False False False
© 2000 John Urrutia. All rights reserved. 17
OR Boolean Truth Tables
Expr 1
True True False False
Expr 2
True False True False OR True True True False
© 2000 John Urrutia. All rights reserved. 18
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
© 2000 John Urrutia. All rights reserved. 19
More Formatted Output
TAB( n ) n –
represents the column number to tab to
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” 0000000001111111111222222222233333333334 1234567890123456789012345678901234567890 10 20 30
© 2000 John Urrutia. All rights reserved. 21
SPC example PRINT SPC(10); “10”; SPC(10); “20”; SPC(10); “30” 0000000001111111111222222222233333333334 1234567890123456789012345678901234567890 10 20 30
© 2000 John Urrutia. All rights reserved. 22
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 © 2000 John Urrutia. All rights reserved. 23
USING format characters
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
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
LOCATE –
position on screen
row% , column% cursor% start% stop% CSRLIN –
line cursor is on
POS(0) –
column cursor is on
© 2000 John Urrutia. All rights reserved. 28