UPPER LIMIT FOR NORMAL AIR PRESSURE? 15 WHAT IS AIR PRESSURE? 14
WH~T IS ~IR PRESSURE? 13
WHAT IS AIR PRFSSURE? 16
WARN I N G! A I R PRE S5 {} RET 00 HIGH
WHAT IS AIR PRFSSURE? I~
WARNING! AIR PRESSURE TOO LOW
And so on •••
In Qeneral, the IF statement has the form of THFN.
I F cond I t 1 on - THFN s ta tement
For exa,"ole. thp. following are two IF statements you~ve already seen'
IF P = 14 THF.N PRINT '''AIRPRESSURE IS NORMAL'"
I (
Condition Statement
If P<L THEN PRINT ,"WARNING! AIR PRESSURE TOO LOW"
I I
Conrlitlon Statement
The following, Is An IF statement that you will be uslno soon.
IF F>2~ THEN GO TO 5'~
I
I
Conrlition State~ent
The condition is frequently a comoarison between two Quantities. Here is a handy table of comparisons that can be used in IF statements'
NIBL Symbol . Meaninq Math Symbol
=
<
>
<=
>=
<>
Is equal to Is less than Is Qreater than
Is less than or equal to Is Qreater than or equal to Is not equ&l to, i.e., Qreater~
or less than
=
<
>
~
.2:.
The auanti tiee; beinQ cornparerf can be numbers, variables or alQebraic expressions. The comparison can be TRUE or FALSE.
Below ·are compar1sons and their truth values, TRUE or fALSE' 3 + 5 > 6 is TRUF, always.
It
A = R and B = 3~, then 4*A <= B Is FALSE"1 f A = 8 and 8
=
32, then 4*A <= B is TRUEIf A = 8 and 9 = 4~. then 4*A <= B ie; TRUE
If the cornoarison is TRUE, then the next statement on the same line as the I F is executed. It can be a'ny kind of statement. A PRINT. a GO TO. another IF. or even those kinds of statements yet to be introduc~d.
If the comoarison is FALSE, then the statement followinq the co~oar1son
is iqnored and the next h1Qhest numbered statement 1s executed.
IF P<L THEN PRINT "'WARNING! AIR PRESSURE TOO LOW"
Do this if the condition P < L is TRUE.
Don~t do this if P < L is FALSE.
ThAt~s all there ie; to IF statements, except that the word THEN may be omitted .if you wish. For examole, instead of writinQt
I
r
P= 14 THEN PR INT "A I R PRESSURE 1 S NORMAL'"you can om i t the word THEN and wri tel
IF P= 14 PRINT'" AIR PRESSURE IS NORMAL"
Sometimes the word THEN makes the proqram easier to read. Use it if 1 t feels comfortahl e.
Re careful to avoid makinq multiole statements seoarated by colons on a line with ~n IF ~tate~ent. Remember that when an IF condition Is found to be FALSE, the entire rest of the line Is iQnored. There-fore, for the followinq proqram, a zero will be nrlnted.
10 A=O '8=99
,~ IF 8> l~A THEN PRINT "BIG Bn~A=1
30 PRINT A
The followinQ oroqram has several REM's to helo you read and
under-~tand it.
100 REM DIALYSIS FLOW MONITOR PROGRAM
II~ REM GET FLOW RATE. F
I 2(}J PR I NT tU'J PR I NT "FLOW". i INPUT F
13(}J REM CHECK IF FLOW RATE CRITICALLY HIGH J40
IF F>20
THEN GO ~O 51015~ REM CHECK IF FLOW RATE CRITICALLY LOW 160
IF
F<I91 THEN'GO TO 51A17~ REM CHECK IF FLOW RATE ABNORMALLY HIGH
180 IF
F>11
THEN GO TO 710J90 REM CHECK IF FLOW RATE ABNORMALLY LOW 200 IF FeJ3 THEN GO TO 71~
21~ REM IF FLOW RATE IS NEITHER TOO HIGH NOR TOO LOW. IT IS OK
220 PRINT "FLOW OK·II.GO TO 12~
500 REM FLOW RATe CRITICALLY HIGH OR LOW. SOUND BELLS
51 ~ PR INT "DANGER! ROW RATE CR ITI CAL R e 11 s .GO TO 12(}J'
70'" REM FLOW RATF IS ARNORMALLY HIGH OR LOW, PRINT MFS5AGE
710 PRINT JlWARN ING. FLOW RATF ARNORMALu.GO TO 120
Try this orogram, make sure it works for all bossible conditions. Try the followinq flow rates AS te~t cases.
FLOV~ OK. 13, 14, 15, 16. 17
A BN ORM AL I , ~. 11. 12. 1 R. 19, 2~
CRITICALJ 1,8.9, 2J. '2. 23
A.fter you have convinced yourself that this oroQram works. read the fol1owinq analY5i~ of it.
Follow alonq and tracp. throuqh the oroaram for a few sopciflc VAlues of F. First. supoo~e F
=
25. The condition In Line 14~ (F>20) is TRUE, therefore, NSC Tiny BASIC will 00 to 5.19J. Line 51c;, directs NSC Tiny . BAS IC to orint the me~sage ~DANGER! . FLOW RATE CRITICAL", ring the Trybell severAl ti"las. then GO TO 12~ for A new VAlue of F. This will continue to haooen tor as 10na as F remains oreater than 20.
Suopose F = 9. The condition in Line 140 (F>20) is FALSE, so NSC Tiny BASIC. ooes on to Line 16~. In Line 160, the condItion (F<10) is TRUE, so NSC Tiny BASIC will qo to 5'0, Drint the danQer messaae, rinq the bell. then GO TO 1~0 for still another value of F.
Suppose F·= 18. The condition in Lines J4~ and 160 Are both FALSE.
(Check them yourself.) Therefore, NSC Tiny BASIC arrives At Line 180.
The condition in Line lA~ (F>17) is TRUE, so NSC Tiny BASIC rioes GO TO
71~ and, as dlrecterl by Line 710 prints the message, "WARNING' FLOW RATF AANORMALJ', then goes back to Line 12~ tor another value of F.
The above ha~ tracerl three oossible paths throuah the oroQraml there Are two more, try these for F = 12 and F : 15. As there are five nossible path~ in all, you may wish to choose your favorite colors of felt tio oens and actually draw the oaths. '
START
FALSE 22c;,
FLOW OK GO TO 12~
Flowchart
TRUE 140
GO TO 5191
TRUE 16~
GO TO 510
TRUE 180
GO TO 7J~
TRUE 200
GO TO 71A
510
DANGER! FLOW RATE CRITICAL (BELLS)
GO TO 120
710
WARN ING I FLOW
RATE ABNORMAL ~~.I
GO TO 120
In the flowchart. or 10Qic diaQram, of the Dialysis Flow ~onitor Pro-rAm, the diamond shaoed boxes corresoond to the IF statements.
The numbers at the top of each box corresoond to line numbers in the orooram. Compare the flowchart wi th the nroqrATTI. Tracp. throuQh the flowchart for several values of F. Make sure you trace each of the five possible oaths throuoh the oroqram. For examole. try it for F
=
25, 9, 18, 12 and 15. (Aqain. olease note that it would be heloful to
""Ark each path with a different color.) 6.3 A More Comn~ct ProQram
In looking over the Dialysis Flow Monitor Prooram, we note the followlnq.
I. If F>2~ or 'F<'~. thp. OToqram shoulrl havp. a danQe:n meSS8Qe oIu5 alarm.
2. If the above is not true. anrl if F>17 or F<13, then the oroQram should have an ~abnormalu messaae, but not an alarm.
3. l'"f neither of the above are true and everythinQ is OK. a "fLOW OK" messaop. will suffice.
NSC Tiny BASIC oermits the use of loqical ooerators AND. OR ann NOT.
Use is made of the OR operator in the tollowinQ revision·ot the dialysis orOQra~.
100 REM DIALYSIS FLOW MONITOR PROGRAM
1 lot REM GET FLOW RATE
120 PR I NT tiM t PR INT "FLOW",. INPUT F
13" REM CHECK IF FLOW RATE CRITICALLY HIGH OR LOW 140 IF CF>20) OR (F<.1~) THEN GO TO 510
170 REM CHECK IF FLOW R~TE ABNORMALLY HIGH OR LOW
lR~ IF CF>17) OR (F<13) THEN GO TO 710
210 REM IF FLOW RATE IS NEITHER TOO HIGH NOR TOO LOW. IT IS OK 220 PRINT "FLOW OK~tGO TO 1209
5~0·RFM FLOW RATE CRITICALLY HIGH OR LOW, SOUND BELLS
51~ PRINT "DANGFR! FLOri RATE CRITIC~L Aells.GO TO .120
700 REM FLOW RATE IS ,ABNORMALLY HIGH OR LOW, PRINT MESS~GE
7J0 PRINT "WARNING' FLOW RATE ARNORMALII.GO TOl 20
Suppose F=25. Then the compound condition (F>2") OR (F<l") In Line 14?J.
is TRUE. In this case NSC Tiny BASIC will GO TO 51~. If F=9, the compound condition is also TRUE and NSC Tiny B4SIC will GO TO 510.
Suppose F=18. The cornoound condition (F>2QJ) OR (·F<I~) in Line 14"
is FALSE, so NSC Tiny BASIC continues on to Line 180. Remember, F Is now equal to lA, so the conditio.n (F>17) OR (F<13) in Line 13~ is TJ.lUF:.
NSC Tinv BASIC does a GO TO 710.
* *
*The parentheses enclosinq F>20. F<10 and so on, a re nee e ssa ry' wi thout t,nttm.. the oroqTam will not work. because loqical ooerators. as arithmetic ooerators, are evaluatep:;cfrom the lef·t- side of theexpr~~~lon to the rioht. Parenthe~es are u5ed to qive prec edenc e.
* * *
The fo llowino i ~ A f lowcha rt of the condensed dial vsls OTOQram.
START
22'"
F1.0W OK GO TO 120
5J0
DANGER! FLOW RATE CRITICAL (BELLS)
GO TO 120~_
710
WARNINGJ FLOW r---~~
.RATE ABNORM4L GO TO 120
Have yo~ noticed that both oroQrams tested for the most danqer-ous condition fir~t? Then tested for the ~econrl most danQerous.
simoly as a matter of l~fe-savinQ priorities. In this case. a few milliseconrls orobably won,'t ""Ake ",uch difference. however, in mAny
real time apolications, a few milliseconds do make a difference.
To illustrate to you that prOQTams usually can be improved upon, the followinq Is a slJoer-condensed Oialysis Flow Monitor Proqraml
12CiJ PR INT '"'' PR I NT "FLOW", I INPUT F
140 IF (F>2(i) OR (F<J~) JJRINT -DANGER! FLOW ~ATE CRITICAL'"
GO TO 12(i)
160 IF (F>11J OR (F<13) PRINT '''WARNING' FLOW RATE ABNORMAL ....
GO TO 12CiJ
22" PR INT .MFLOW OK"'GO ,'TO;,: 1·20
The AND, OR and NOT operators neerl not he limited to use in IF
state-~ents. They Are logical ooerAtor~ anrl onerate Bit-by-~it on any
con-~tAnt or variable. This will be illustrated latar on in this manual with an example on some 1/0 bits.
The followlno proQram implement~ the function indicated In the Qraoh beneath it.
100 REM HASTILY CONSTRUCTED PROGRAM TO ILLUSTRATF. USE OF "AND"
11~ PRINT "".PRINT "X=fl,~INPUT X
J20 IF (0<=X) AND (X<=l00) PRINT .. Y=.II,X.GO TO 110 130 IF (10CiJ<X) AND (X<=20~) Pf.lINT "Y=·',IOO.GO TO 11~
140
IF
(200<X) AND (X<=400) PRINT lIy=u.15",tGO TO IJ0 15A P~INT "Y IS NOT DEFINED FOR X=-,XNOTE' The parentheses around CIJ<=X, X<=l~c.J. and so on in the
IF statement are necessary. Without them, the orogram will not work. This is because of the multiollcity in the conditions beinq checked.
Y
=
f(x)15~
---~---~---~----~x 400
X for 0 < X < 100
y = t(x) = l~ for 100 < X < 2~
15~ for 2~ < X < 4A0
The followinQ 1 s a RUN of thp. orecedlna orOQram. All cri tiCAl ooints have'been checked.
RUN X=? -I