Logical Functions

There is a number of logical functions available. You can use the function IIf to make decisions, and the functions Repeat or Iterate to perform iterative computations. See also the page on logical operators.


Overview

You can use the following logical functions.
Function Description
IIf(expr, truepart {, falsepart}) Returns the result of truepart when expr is true (non zero), and returns falsepart when expr is false (zero). When falsepart is not given, zero is returned.

Iterate(x = f(x), xstart, iterations) Iterate x=f(x) for the given number of iterations. The initial value for x is given with xstart.

Not(x) The logical Not returns 0 if the value x is non zero, and 1 if the value is zero.

Repeat(expr, x, start, end {, step}) Lets the variable x go from start to end with given step size (1 by default). Each iteration the expression expr is calculated.

It is possible that a calculation with the function Repeat (and also the function Iterate) takes a long time: when end is much larger than start, or when step is small. You can abort a calculation by pressing the ESC key.
Back to top

Examples

' Examples of using logical functions

' You can use IIf to choose a result depending on a condition.
a = 3.5;
b = 7.1;
IIf(a > 0, 10, -10)
       Ans = 10

' Create your own min function.
IIf(a < b, a, b)
       Ans = 3.5

' Examples of using Not.
Not(3.2)
       Ans = 0
Not(0)
       Ans = 1
Not(-5.1)
       Ans = 0

' Iteratively calculate factorial of five using the 
' function Repeat. 5! = 1*2*3*4*5 
fac = 1;
Repeat(fac = fac * n, n, 1, 5)
       Ans = 120

' Summation of values 0 to 100
total = 0;
Repeat(total = total + x, x, 0, 100)
       Ans = 5050

' You can choose another step size
' Summation of all even values between 0 and 100
total = 0;
Repeat(total = total + x, x, 0, 100, 2)
       Ans = 2550

' Calculate the amount of money you have when
' you save 1000 dollar on a bank with 3.5 percent
' interest per year
Iterate(balance = balance * (1 + 0.035), 1000, 10)
       Ans = 1410.598760621

' Calculate the amount of money you have when
' you save 1000 dollar each year on a bank with 
' 3.5 percent interest per year.
amountperyear = 1000;
interest = 3.5;
years = 10;
Iterate(balance = (balance + amountperyear) * (1 + interest/100), 0, years)
       Ans = 12141.991921225

Back to top

See Also

Functions overview, Logical operators, Functionstree