Math Functions: Conditionals Calculator
Math Functions: Conditionals
Sometimes different outputs are desired. Conditionals allow you to make appropriate comparisons and output based on that comparison. Conditionals are indispensable tools for creating templates.
PowerOne offers two types of conditionals: if statements and choose functions.
__These functions are only available in templates.__
++if(comparison; true statement; false statement)++
Returns the 'true statement' or 'false statement' of 'comparison'. 'comparison' is a comparison or expression that evaluates to either true or false. To read this literally: if the result of 'comparison' is true than do 'true statement'; otherwise do 'false statement'.
__This function is only available in templates.__
Assume a 4% commission rate if sales are less than $100,000 but a 5% commission otherwise:
The 'comparison' of Sales < 100000 returns a 'true statement' of 4% or a 'false statement' of 5%. Note that the true and false statements in this case are math expressions. If Sales are $90,000 then the result of this comparison is 0.04. If, on the other hand, sales are $110,000 then the result of this comparison is 0.05.
The inverse also works (if sales are greater than or equal to $100,000 then return 5% otherwise return 4%):
= 100000; 5%; 4%)>
The comparison could include a variable and a number (as above), two variables, or even a more complex statement, discussed later. For example, if your speed is greater than the posted speed then return the ticket price, otherwise 0 (no fine!):
Posted Speed; Ticket Price; 0)>
There are seven conditionals. They are:
- ++**equal to (==)**++ : value A is equal to value B (value A == value B) [Note: it is two equals signs, not one]
- ++**not equal (!=)**++ : value A does not equal value B (value A != value B)
- ++**less than (<)**++ : value A is less than value B (value A < value B)
- ++**less than or equal to (<=)**++ : value A is less than or equal to value B (value A <= value B)
- ++**greater than (>)**++ : value A is greater than value B (value A > value B)
- ++**greater than or equal to (>=)**++ : value A is greater than or equal to value B (value A >= value B)
25; 1; 0) returns 1 if Value is greater than 25 and 0 otherwise>
= 25; 1; 0) returns 1 if Value is greater than or equal to 25 and 0 otherwise>
⇥Comparisons (==) vs. Assignments (=)
It is important to note that the equals to used in an if() function is different than the equal to used in a variable or equation.
if() functions use two equals signs (==) to denote __comparison__ while variables use a single equals sign (=) used to denote __assignment__.
Expressions can be used anywhere in the if() function. Assume that the local police won't ticket unless you are more than 10 mph over the limit. In this case we use an expression in the conditional:
Posted Speed + 10; Ticket Price; 0)>
Ticket prices, though, are never a fixed amount. Usually they have a fixed amount (assume 100) plus an additional amount based on the speed (assume 3 per mph). The if statement could look like this:
Posted Speed + 10; Your Speed * 3 + 100; 0)>
⇥Nested If Statements
if() functions can also be nested within each other, either on the 'true statement' side or the 'false statement' side. Let's assume the police have two different charges, one if greater than 10 mph over the speed limit and another if between 1 and 10 mph over. You could use a nested if statement for these problems:
Posted Speed + 10; Your Speed * 3 + 100; if(Your Speed > Posted Speed; Your Speed * 2 + 50; 0))>
If you are driving 10 mph or more over the speed limit, then calculate the ticket as your speed times 3 plus 100. Otherwise, if you are driving over the speed limit then calculate the ticket as your speed times 2 plus 50. Finally, if you are at or below the speed limit there is no fine (ticket price is 0). Because the first if() function takes care of cases where the speed is greater than 10 mph that case doesn't need to be handled in the nested if statement.
You can nest up to 256 if statements (but we don't recommend trying it).
Some comparisons are more complicated where more than one condition has to be true for a true result. In the commission example, instead assume the 5% commission is only available if sales are between $100,000 and $500,000. Otherwise the commission is 4%. In this case we use an and (&&) to combine comparisons:
100000 && Sales < 500000; 5%; 4%)>
The following options are available:
- ++**and (&&)**++ : comparison 1 and comparison 2 must be true for a true result
- ++**or (||)**++ : comparison 1 or comparison 2 can be true for a true result
- ++**not (!)**++ : comparison 1 is not true
You can use all, some or none of these to create even more complex comparisons. Assume that the 5% commission is paid if your sales are between 100,000 and 500,000 or between 750,000 and 900,000:
100000 && Sales < 500000 || Sales > 750000 && Sales < 900000; 5%; 4%)>
This can get very complicated. The best solution for writing conditionals is to write them the way you would say them. In some cases it may be easiest to think in terms of the positive condition and in other cases it may be easier to think in terms of the negative condition. Let's say, for instance, that the commission is paid at 5% except when sales are less than 100,000 or more than 500,000, where it is then paid at 4%. The if() function could be written like this:
500000; 4%; 5%)>
Alternatively you can use not (!) to inverse the condition. In this case we are saying if we are not between 100,000 and 500,000 then return 4% otherwise return 5%:
= 100000 && Sales <= 500000); 4%; 5%)>
Both of these return the same results. Write it in the method easiest for you to read and understand.
++choose(index; expression 1; …; expression N)++
Returns 'expression 1' through 'expression N' where the expression number is 'index'. The choose function is a shortcut for writing some if statements. Often times, if statements fall into this pattern:
if 1 then ...
if 2 then ...
if 3 then...
Instead of writing an if statement like this:
use a choose function:
__These functions are only available in templates.__
- 'index' can be a value or expression that evaluates to a value
- 'index' must be a whole number between 1 and N, inclusive, otherwise a "Parameter out of range" calculation error will be returned
- 'expression' can be a value or expression as well that evaluates to the row's type
Not Equal to
Less Than or Equal To
Greater Than or Equal to