Difference between revisions of "If Command"
From GeoGebra Manual
m (sorry  missing italics again) 

Line 1:  Line 1:  
<noinclude>{{Manual Pageversion=4.0}}[[Category:Manual (official){{PAGENAME}}]]</noinclude>  <noinclude>{{Manual Pageversion=4.0}}[[Category:Manual (official){{PAGENAME}}]]</noinclude>  
{{commandlogical}}  {{commandlogical}}  
−  ; 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''. 
{{warningBoth objects must be of the same type.}}  {{warningBoth objects must be of the same type.}}  
Line 8:  Line 8:  
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 CommandDerivative]], [[Integral CommandIntegral]], and [[Intersect CommandIntersect]].  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 CommandDerivative]], [[Integral CommandIntegral]], and [[Intersect CommandIntersect]].  
−  {{  +  {{Examples1=<div> 
* <code>f(x) = If[x < 3, sin(x), x^2]</code> yields a 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 function that equals ''sin(x)'' for ''x < 3'' and ''x<sup>2</sup>'' for ''x ≥ 3''  
* <code>f(x) = If[x < 3 ∧ x>0, x^3]</code> yields a function that equals ''sin(x)'' for x between 0 and 3 and undefined for ''x ≥ 3'' or ''x ≤ 0''.</div>}}  * <code>f(x) = If[x < 3 ∧ x>0, x^3]</code> yields a function that equals ''sin(x)'' for x between 0 and 3 and undefined for ''x ≥ 3'' or ''x ≤ 0''.</div>}} 
Revision as of 17:09, 7 November 2012
 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 function that equals sin(x) for x < 3 and x^{2} for x ≥ 3 
f(x) = If[x < 3 ∧ x>0, x^3]
yields a function that equals sin(x) for x between 0 and 3 and undefined for x ≥ 3 or x ≤ 0.
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
In many programming languages if has the meaning "If condition holds, do something; otherwise do something else". In GeoGebra, arguments of If are not commands, but values, one of which becomes the value of the result. Therefore
if you want to e.g. set value of b to 3 provided a > 2, correct way to do this is SetValue[b,If[a>2,3,b]]
. The other way of nesting SetValue and If is incorrect.
Following text is about a feature that is supported only in GeoGebra 4.2.
Example: Let n be a number, and A a point. The command If[Mod[n, 2] == 0, SetCoords[A, n, 0], SetCoords[A, n, 1]] modifies the coordinates of point A according to the given condition. 