258 Appendix A: Functions and Instructions
when() CATALOG
when(
condition
,
trueResult
[,
falseResult
]
[,
unknownResult
]) ⇒
expression
Returns
trueResult
,
falseResult
, or
unknownResult
,
depending on whether
condition
is true, false, or
unknown. Returns the input if there are too few
arguments to specify the appropriate result.
Omit both
falseResult
and
unknownResult
to make
an expression defined only in the region where
condition
is true.
when(x<0,x+3)|x=5 ¸
when(x<0,3+x)
Use an undef
falseResult
to define an expression
that graphs only on an interval.
ClrGraph ¸
Graph when(x‚ë p and
x<0,x+3,undef)
¸
Omit only the
unknownResult
to define a two-piece
expression.
Graph when(x<0,x+3,5ì x^2) ¸
Nest when() to define expressions that have
more than two pieces.
@ "
H ¥ "
ClrGraph ¸ Done
Graph when(x<0,when(x<ë p,
4ù sin(x),2x+3),5ì x^2)
¸
when() is helpful for defining recursive functions.
when(n>0,nù factoral(nì 1),1)
! factoral(n)
¸ Done
factoral(3)
¸ 6
3!
¸ 6
While CATALOG
While
condition
block
EndWhile
Executes the statements in
block
as long as
condition
is true.
block
can be either a single statement or a
sequence of statements separated with the “:”
character.
Program segment:
©
:1! i
:0! temp
:While i<=20
: temp+1/i! temp
: i+1! i
:EndWhile
:Disp "sum of reciprocals up
to 20",temp
©
“With” See |, page 277.