1 © 2000 john urrutia. all rights reserved. qbasic looping statements & formatted output
TRANSCRIPT
2© 2000 John Urrutia. All rights reserved.
Here we go Loop de LoopA loop is a set of statements that are
executed repeatedly.
Types Controlled
Pre-testPost-test
Infinite
3© 2000 John Urrutia. All rights reserved.
Infinite LoopsGenerally a bad thing.
Keeps going and going and …
Going Going GoingGoing Going GoingGoing Going Going
4© 2000 John Urrutia. All rights reserved.
Controlled LoopsGoverned by a condition.
LogicalSentinel
MathematicalCounter
EnvironmentalEOF()
5© 2000 John Urrutia. All rights reserved.
Infinite
DO…LOOP syntax
DO
statements
LOOP
Pre-Test
{ WHILE | UNTIL }
condition
{ WHILE | UNTIL }
condition
Post-Test
6© 2000 John Urrutia. All rights reserved.
ConditionA 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 StatementPretest 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 StatementPosttest 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 stateGenerally:
1 is TRUE0 is FALSE
10© 2000 John Urrutia. All rights reserved.
Relational ExpressionsSingle 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.
ComparisonsNumeric comparisons are simple.
Compares bit for bit
Negative numbers are stored in 2’s compliment+110 = 000016 = 0000 0000 000 000012
+010 = 000016 = 0000 0000 0000 00002
-110 = FFFF16 = 1111 1111 1111 11112
-210 = FFFE16 = 1111 1111 1111 11102
-310 = FFFD16 = 1111 1111 1111 11012
12© 2000 John Urrutia. All rights reserved.
ComparisonsStrings 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 =5A16 =0101 10102
“a”char =9710 =6116 =0110 00012
“z”char =12210 =7A16 =0111 10102
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 ConditionsWhen 2 or more expressions are
combined together.
Used to specify complex conditions in one statement.
15© 2000 John Urrutia. All rights reserved.
Boolean OperatorsNOT – negation (bit-wise complement)
AND – logical addition (conjunction)
OR – logical subtraction (disjunction)
XOR – exclusive “or”
EQV – logical equivalence
IMP – logical implication
17© 2000 John Urrutia. All rights reserved.
AND Boolean Truth Tables
Expr 1 Expr 2 AND
True True True
FalseTrue False
False True False
False False False
18© 2000 John Urrutia. All rights reserved.
OR Boolean Truth Tables
True True True
FalseTrue True
False True True
False False False
Expr 1 Expr 2 OR
19© 2000 John Urrutia. All rights reserved.
Nested LoopsA loop within a loop
ones% = 0tens% = 0DO WHILE tens% < 10 DO WHILE ones% < 10
PRINT tens% ; “-“ ; ones% ones% = ones% + 1
LOOPones% = 0tens% = tens% + 1
LOOP
20© 2000 John Urrutia. All rights reserved.
More Formatted OutputTAB(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”
00000000011111111112222222222333333333341234567890123456789012345678901234567890
10 20 30
22© 2000 John Urrutia. All rights reserved.
SPC example
PRINT SPC(10); “10”; SPC(10); “20”; SPC(10); “30”
00000000011111111112222222222333333333341234567890123456789012345678901234567890
10 20 30
23© 2000 John Urrutia. All rights reserved.
The PRINT USING statementWrites formatted data to the teminal
PRINT USING “format-string” ; output-list
The format-string specifiesNumeric edited data formats
String formats
Literal data
24© 2000 John Urrutia. All rights reserved.
USING format charactersStrings
\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 charactersNumbers
# – 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 exampleSPRINT 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!