If-then-elseΒΆ

Like many other programming languages, FriCAS uses the three keywords if, then, and else to form conditional expressions. The else part of the conditional is optional. The expression between the if and then keywords is a predicate: an expression that evaluates to or is convertible to either true or false, that is, a Boolean.

The syntax for conditional expressions is

if predicate then expression1 else expression2

where the “else expression2” part is optional. The value returned from a conditional expression is expression1 if the predicate evaluates to true and expression2 otherwise. If no else clause is given, the value is always the unique value of Void.

An if-then-else expression always returns a value. If the else clause is missing then the entire expression returns the unique value of Void. If both clauses are present, the type of the value returned by if is obtained by resolving the types of the values of the two clauses.

The predicate must evaluate to, or be convertible to, an object of type Boolean: true or false. By default, the equal sign “=” creates an equation.

x + 1 = y
 x + 1 = y
             Type: Equation Polynomial Integer

This is an equation, not a boolean condition. In particular, it is an object of type Equation Polynomial Integer.

However, for predicates in if expressions, FriCAS places a default target type of Boolean on the predicate and equality testing is performed. Thus you need not qualify the “=” in any way. In other contexts you may need to tell FriCAS that you want to test for equality rather than create an equation. In these cases, use “@” and a target type of Boolean.

The compound symbol meaning “not equal” in FriCAS is “~=”. This can be used directly without a package call or a target specification. The expression “a ~= b” is directly translated to “not(a = b)”.

Many other functions have return values of type Boolean. These include <, <=, >, >=, ~=, and member?. By convention, operations with names ending in ”?” return Boolean values.

The usual rules for piles are suspended for conditional expressions. In .input files, the then and else keywords can begin in the same column as the corresponding if by may also appear to the right. Each of the following styles of writing if-then-else expressions is acceptable:

if i>0 then output("positive") else output("nonpositive")

if i>0 then output("positive")
  else output("nonpositive")

if i>0 then output("positive")
else output("nonpositive")

if i>0
then output("positive")
else output("nonpositive")

if i>0
  then output("positive")
  else output("nonpositive")

A block can follow the then or else keywords. In the following two assignments to a, the then and else clauses each are followed by two line piles. The value returned in each is the value of the second line.

a :=
  if i > 0 then
    j := sin(i * pi())
    exp(j + 1/j)
  else
    j := cos(i * 0.5 * pi())
    log(abs(j)**5 + i)


a :=
  if i > 0
    then
      j := sin(i * pi())
      exp(j + 1/j)
    else
      j := cos(i * 0.5 * pi())
      log(abs(j)**5 + i)

These are both equivalent to the following:

a :=
  if i > 0 then (j := sin(i * pi()); exp(j + 1/j))
  else (j := cos(i * 0.5 * pi()); log(abs(j)**5 + i))

Table Of Contents

This Page