if: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Macros:Functions:if moved to if)
(Added version, cleaned up formatting, removed condescending wording, moved generic condition text to Branching and Looping.)
Line 1: Line 1:
{{MacroFunction
{{MacroFunction
|name=if
|name=if
|version=1.3b38
|description=
|description=
If ''testCondition'' evaluates to 0 then the result of ''falseExpr'' is returned, otherwise the result of ''trueExpr'' is returned. Note: '''both''' ''trueExpr'' and ''falseExpr'' are evaluated no matter what the result of ''testCondition'' is. You may want to consider using the [[Macros:Branching_and_Looping#IF_Option| [IF():]]] roll option instead.
This function is used to check whether a certain code ''expression'' should be executed or not. If the ''condition'' to be evaluated with this function is {{code|true}}, the first ''expression'' of code is executed, otherwise the second ''expression'' of code is executed.  


Test condition notation:
'''Note:''' This function has been depreciated in favor of the [[if (roll option)|if (roll option)]]. Both {{code|trueExpr}} and {{code|falseExpr}} are evaluated regardless of what {{code|condition}} returns, so this should only be used in limited cases where a small ''branch'' is required within other functions.
 
* > for greater than
* < for less than
* >= for greater than or equal to
* <= for less than or equal to
* == for equal to - yes, you must use TWO equal signs to evaluate "is equal to"
 
If you are checking for a text string, place quotes around the text. You can find more information on these and other logical operations useful for the if function halfway down this page: http://rptools.net/doku.php?id=parser


|usage=
|usage=
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: result = if(testCondition, trueExpr, falseCondition)]
if(condition, trueExpr, falseExpr)
</source>
</source>
'''Parameters'''
* {{code|condition}} - What is tested to determine is the {{code|trueExpr}} or {{code|falseExpr}} will be executed. This follows the standard rules for ''conditions'' that can be found in the [[Branching and Looping|Branching and Looping]] article.
* {{code|trueExpr}} - A section of code that is executed if {{code|condition}} returns as {{code|true}}.
* {{code|falseExpr}} - A section of code that is executed if {{code|condition}} does not return as {{code|true}}.


|examples=
|examples=
'''Example 1:''' If {{code|a}} is {{code|10}} and {{code|b}} is {{code|20}}:
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[r: if(a>b, "A is larger than B", "A is not larger than B")]
[r: if(a > b, "A is larger than B", "A is not larger than B")]
[someProperty = if(variable>=1,20,"")]
</source>
</source>
'''Returns:''' {{code|A is not larger than B}}


Sets ''someProperty'' to 20 if variable is greater than or equal to 1, or makes it blank (but not empty) otherwise.
'''Example 2:''' If {{code|number}} is {{code|1}}:
<source lang="mtmacro" line>
[r: if(number >= 1, 20, "")]
</source>
'''Returns:''' A ''blank string'', please note that a ''blank string'' is not an ''empty variable'' if you were to assign the ''output'' of this function.


'''Example 3:''' If {{code|variable}} is {{code|"Foobar"}}:
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[someProperty = if(variable=="Text",1,0)]</source>
[r: if(variable == "Text", 1, 0)]
</source>
'''Returns:''' {{code|0}}


Sets ''someProperty'' to 1 if variable contains the text string Text, 0 otherwise.
'''Example 4:''' If {{code|variable}} is {{code|20}}
<source lang="mtmacro" line>
[property = if(variable > 0 && variable < 20, 1, 0)]
</source>
'''Returns:''' {{code|property}} set to {{code|0}}


<source lang="mtmacro" line>
|also=
[someProperty = if(variable>0 && variable<20,1,0)]</source>
[[Branching and Looping|Branching and Looping]], [[if (roll option)|if (roll option)]]
Sets ''someProperty'' to 1 if variable is between 0 and 20.
}}
}}
[[Category:Miscellaneous Function]]
[[Category:Miscellaneous Function]]

Revision as of 14:41, 9 March 2009

if() Function

Introduced in version 1.3b38
This function is used to check whether a certain code expression should be executed or not. If the condition to be evaluated with this function is true, the first expression of code is executed, otherwise the second expression of code is executed. Note: This function has been depreciated in favor of the if (roll option). Both trueExpr and falseExpr are evaluated regardless of what condition returns, so this should only be used in limited cases where a small branch is required within other functions.

Usage

if(condition, trueExpr, falseExpr)

Parameters

  • condition - What is tested to determine is the trueExpr or falseExpr will be executed. This follows the standard rules for conditions that can be found in the Branching and Looping article.
  • trueExpr - A section of code that is executed if condition returns as true.
  • falseExpr - A section of code that is executed if condition does not return as true.

Examples

Example 1: If a is 10 and b is 20:
[r: if(a > b, "A is larger than B", "A is not larger than B")]

Returns: A is not larger than B

Example 2: If number is 1:

[r: if(number >= 1, 20, "")]

Returns: A blank string, please note that a blank string is not an empty variable if you were to assign the output of this function.

Example 3: If variable is "Foobar":

[r: if(variable == "Text", 1, 0)]

Returns: 0

Example 4: If variable is 20

[property = if(variable > 0 && variable < 20, 1, 0)]
Returns: property set to 0

See Also