Difference between revisions of "If Command"

From GeoGebra Manual
Jump to: navigation, search
(:{{note| 1=This syntax isn't supported in the CAS View}})
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
<noinclude>{{Manual Page|version=5.0}}</noinclude>{{command|logical}}
 
<noinclude>{{Manual Page|version=5.0}}</noinclude>{{command|logical}}
; If[ <Condition>, <Then> ]
+
; If( <Condition>, <Then> )
 
: Yields a copy of the object ''Then'' if the condition evaluates to ''true'', and an undefined object if it evaluates to ''false''.
 
: Yields a copy of the object ''Then'' if the condition evaluates to ''true'', and an undefined object if it evaluates to ''false''.
 
:{{Examples|1=<div>
 
:{{Examples|1=<div>
:*Let ''n'' = 3. <code><nowiki>If[n==3, x + y = 4]</nowiki></code> yields line ''x'' + ''y'' = 4, because the condition on number ''n'' is met.  
+
:*Let ''n'' = 3. <code><nowiki>If(n==3, x + y = 4)</nowiki></code> yields line ''x'' + ''y'' = 4, because the condition on number ''n'' is met.  
:*Let ''n'' = 4. <code><nowiki>If[n==3, x + y = 4]</nowiki></code> creates an ''undefined'' object, because the condition on number ''n'' is not met .</div>}}
+
:*Let ''n'' = 4. <code><nowiki>If(n==3, x + y = 4)</nowiki></code> creates an ''undefined'' object, because the condition on number ''n'' is not met .</div>}}
; If[ <Condition>, <Then>, <Else> ]
+
; If( <Condition>, <Then>, <Else> )
: Yields a copy of object ''Then'' if the condition evaluates to ''true'', and a copy of object ''Else'' if it evaluates to ''false''. Both objects ''must'' be of the same type.
+
: Yields a copy of object ''Then'' if the condition evaluates to ''true'', and a copy of object ''Else'' if it evaluates to ''false''. '''Both objects ''must'' be of the same type.'''
:{{example|1= Let ''n'' be a number. <code><nowiki>If[n==3, x + y = 4, x - y = 4]</nowiki></code> yields line ''x'' + ''y'' = 4 when ''n'' = 3, and line ''x'' - ''y'' = 4 for all ''n'' not equal to 3.}}
+
:{{example|1= Let ''n'' be a number. <code><nowiki>If(n==3, x + y = 4, x - y = 4)</nowiki></code> yields line ''x'' + ''y'' = 4 when ''n'' = 3, and line ''x'' - ''y'' = 4 for all ''n'' not equal to 3.}}
 +
; If( <Condition 1>, <Then 1>, <Condition 2>, <Then 2>, ... , <Else (optional)> )
 +
: Yields a copy of "Then 1" when first condition is satisfied, "Then 2" if second condition is satisfied etc. If none of the conditions are satisfied and Else is given, this command yields a copy of Else. Otherwise undefined is returned.
 +
:{{example| 1=<code>If(a ≟ 1, "Matthew", a ≟ 2,"Larry", a ≟ 3, "Vivian", "Alex")</code> When ''a''=1 this returns the text "Matthew", for ''a''=2' it returns "Larry", for ''a''=3 "Vivian" and for all other values of ''a'' it yields "Alex".}}
 +
:{{note| 1=This syntax isn't supported in the CAS View}}
  
 
===Conditional Functions===
 
===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 Command|Derivative]], [[Integral Command|Integral]], and [[Intersect Command|Intersect]].
 
: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 Command|Derivative]], [[Integral Command|Integral]], and [[Intersect Command|Intersect]].
 
:{{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).
:* <code>f(x) =If[x<-1,x²,-1<=x<=1,1,-x²+2]</code> yields the piecewise function <math>f(x)  = \left\{\begin{matrix}{} x^{2}& : &x < -1\\ 1& :& -1 ≤ x ≤ 1\\ -x^{2} + 2& : &\text{otherwise} \end{matrix}\right </math>.
+
::{{note|1= A shorter syntax for this is <code>f(x) = sin(x), 0 <= x <= 3</code>}}
 +
:* <code>f(x) =If(x<-1,x²,-1<=x<=1,1,-x²+2)</code> yields the piecewise function <math>f(x)  = \left\{\begin{matrix}{} x^{2}& : &x < -1\\ 1& :& -1 ≤ x ≤ 1\\ -x^{2} + 2& : &\text{otherwise} \end{matrix}\right </math>.
 
</div>}}
 
</div>}}
:{{note|1= A shorter syntax for this is <code>f(x) = sin(x), 0 <= x <= 3</code>}}
+
 
 +
 
 
:{{notes|1=<div>
 
:{{notes|1=<div>
:* 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.
+
:* 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.
 
:* See section: [[Boolean values]] for the symbols used in conditional statements.</div>}}
 
:* See section: [[Boolean values]] for the symbols used in conditional statements.</div>}}
  
 
==If Command in Scripting==
 
==If Command in Scripting==
 
:'''If''' command can be used in scripts to perform different actions under certain conditions.
 
:'''If''' command can be used in scripts to perform different actions under certain conditions.
:{{example|1= Let ''n'' be a number, and ''A'' a point. The command <code>If[Mod[n, 7] == 0, SetCoords[A, n, 0], SetCoords[A, n, 1]]</code> modifies the coordinates of point ''A'' according to the given condition. In this case it would be easier to use  <code> SetCoords[A, n, If[Mod[n, 7] == 0,0,1]]</code>.}}
+
:{{example|1= Let ''n'' be a number, and ''A'' a point. The command <code>If(Mod(n, 7) == 0, SetCoords(A, n, 0), SetCoords(A, n, 1))</code> modifies the coordinates of point ''A'' according to the given condition. In this case it would be easier to use  <code> SetCoords(A, n, If(Mod(n, 7) == 0,0,1))</code>.}}
:{{note|1= Arguments of '''If''' must be Objects or [[Scripting Commands]], not assignments. Syntax <code><nowiki>b = If[a > 1, 2, 3]</nowiki></code> is correct, but ''b = 2'' or ''b = 3'' would not be accepted as parameters of '''If'''.}}
+
:{{note|1= Arguments of '''If''' must be Objects or [[Scripting Commands]], not assignments. Syntax <code><nowiki>b = If(a > 1, 2, 3)</nowiki></code> is correct, but ''b = 2'' or ''b = 3'' would not be accepted as parameters of '''If'''.}}

Latest revision as of 16:23, 2 May 2021


If( <Condition>, <Then> )
Yields a copy of the object Then if the condition evaluates to true, and an undefined object if it evaluates to false.
Examples:
  • Let n = 3. If(n==3, x + y = 4) yields line x + y = 4, because the condition on number n is met.
  • Let n = 4. If(n==3, x + y = 4) creates an undefined object, because the condition on number n is not met .
If( <Condition>, <Then>, <Else> )
Yields a copy of object Then if the condition evaluates to true, and a copy of object Else if it evaluates to false. Both objects must be of the same type.
Example: Let n be a number. If(n==3, x + y = 4, x - y = 4) yields line x + y = 4 when n = 3, and line x - y = 4 for all n not equal to 3.
If( <Condition 1>, <Then 1>, <Condition 2>, <Then 2>, ... , <Else (optional)> )
Yields a copy of "Then 1" when first condition is satisfied, "Then 2" if second condition is satisfied etc. If none of the conditions are satisfied and Else is given, this command yields a copy of Else. Otherwise undefined is returned.
Example: If(a ≟ 1, "Matthew", a ≟ 2,"Larry", a ≟ 3, "Vivian", "Alex") When a=1 this returns the text "Matthew", for a=2' it returns "Larry", for a=3 "Vivian" and for all other values of a it yields "Alex".
Note: This syntax isn't supported in the CAS View

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: A shorter syntax for this is f(x) = sin(x), 0 <= x <= 3
  • f(x) =If(x<-1,x²,-1<=x<=1,1,-x²+2) yields the piecewise function f(x) = \left\{\begin{matrix}{} x^{2}& : &x < -1\\ 1& :& -1 ≤ x ≤ 1\\ -x^{2} + 2& : &\text{otherwise} \end{matrix}\right .


Notes:
  • 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.
  • See section: Boolean values for the symbols used in conditional statements.

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: 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.
© 2024 International GeoGebra Institute