Difference between revisions of "If Command"
From GeoGebra Manual
Line 3: | Line 3: | ||
; If[ <Condition>, <Object> ]: Yields a copy of the object if the condition evaluates to ''true'', and an undefined object if it evaluates to ''false''. | ; If[ <Condition>, <Object> ]: Yields a copy of the object if the condition evaluates to ''true'', and an undefined object if it evaluates to ''false''. | ||
; If[ <Condition>, <Object a>, <Object b> ]: Yields a copy of object ''a'' if the condition evaluates to ''true'', and a copy of object ''b'' if it evaluates to ''false''. | ; If[ <Condition>, <Object a>, <Object b> ]: Yields a copy of object ''a'' if the condition evaluates to ''true'', and a copy of object ''b'' if it evaluates to ''false''. | ||
− | {{warning|Both objects must be of the same type.}} | + | {{warning|Both objects '''must''' be of the same type.}} |
===Conditional Functions=== | ===Conditional Functions=== | ||
Line 9: | Line 9: | ||
{{Examples|1=<div> | {{Examples|1=<div> | ||
− | * <code>f(x) = If[x < 3, sin(x), x^2]</code> yields a piecewise function that equals ''sin(x)'' for ''x < 3'' and ''x<sup>2</sup>'' for ''x ≥ 3'' | + | * <code>f(x) = If[x < 3, sin(x), x^2]</code> yields a piecewise function that equals ''sin(x)'' for ''x < 3'' and ''x<sup>2</sup>'' for ''x ≥ 3''. |
− | * <code>f(x) = If[0 <= x <= 3, sin(x) ]</code> yields a function that equals ''sin(x)'' for x between 0 and 3 (and undefined otherwise) | + | * <code>f(x) = If[0 <= x <= 3, sin(x) ]</code> yields a function that equals ''sin(x)'' for x between 0 and 3 (and undefined otherwise).</div>}} |
{{note|See section: [[Boolean values]] for the symbols used in conditional statements.}} | {{note|See section: [[Boolean values]] for the symbols used in conditional statements.}} | ||
{{note| Derivative of '''If[condition, f(x), g(x)]''' gives '''If[condition, f'(x), g'(x)]'''. It does not do any evaluation of limits at the critical points.}} | {{note| Derivative of '''If[condition, f(x), g(x)]''' gives '''If[condition, f'(x), g'(x)]'''. It does not do any evaluation of limits at the critical points.}} |
Revision as of 18:15, 4 February 2013
- If[ <Condition>, <Object> ]
- Yields a copy of the object if the condition evaluates to true, and an undefined object if it evaluates to false.
- If[ <Condition>, <Object a>, <Object b> ]
- Yields a copy of object a if the condition evaluates to true, and a copy of object b if it evaluates to false.
Warning: | Both objects must be of the same type. |
Conditional Functions
The If command can be used to create conditional functions. Such conditional functions may be used as arguments in any command that takes a function argument, such as Derivative, Integral, and Intersect.
Examples:
f(x) = If[x < 3, sin(x), x^2]
yields a piecewise function that equals sin(x) for x < 3 and x2 for x ≥ 3.f(x) = If[0 <= x <= 3, sin(x) ]
yields a function that equals sin(x) for x between 0 and 3 (and undefined otherwise).
Note: See section: Boolean values for the symbols used in conditional statements.
Note: Derivative of If[condition, f(x), g(x)] gives If[condition, f'(x), g'(x)]. It does not do any evaluation of limits at the critical points.
If Command in Scripting
If command can be used in scripts to perform different actions under certain conditions.
Example: Let n be a number, and A a point. The command
If[Mod[n, 7] == 0, SetCoords[A, n, 0], SetCoords[A, n, 1]]
modifies the coordinates of point A according to the given condition. In this case it would be easier to use SetCoords[A, n, If[Mod[n, 7] == 0,0,1]]
.Note that arguments of If must be Objects or Scripting Commands, not assignments. Syntax b=If[a>1,2,3]
is correct, but b=2 or b=3 would not be accepted as parameters of If.