The Xcas language has different ways of writing if…then statements (see section 4.7.2). The standard version of the if…then statement consists of the if keyword, followed by a boolean expression (see section 5.2 in parentheses, followed by a statement block which will be executed if the boolean is true.
As an example, if the variables a and b have the values 3 and 2, respectively, and you enter
then since a > b will evaluate to true, the variable a will be reset to 8 and b will be reset to the value 6.
An if statement can include a block of statements to execute when the boolean is false by putting it at the end following the else keyword. For example, if the variable val has a real value, then the statement
will set abs to the same value as val if val is positive and it will set abs to negative the value of val otherwise.
An alternate way to write an if statement is to enclose the code block in then and end instead of braces; if the variable a is equal to 3, then
will reset a to 8. An else block can be included by putting the else statements after else and before the end. For example, with a having the value 8 as above,
will reset a to the value 3. This can also be written:
Several if statements can be nested; for example, the statement
A simpler way is to replace the else if by elif; the above statement can be written
In general, such a combination can be written
if (boolean 1) then block 1; elif (boolean 2) then block 2; ... elif (boolean n) then block n; else last block; end
(where the last else is optional.) For example, if you want to define a function f by
f(x) = |
|
you can enter
f(x) := { if (x > 8) then return 8; elif (x > 4) then return 4; elif (x > 2) then return 2; elif (x > 0) then return 1; else return 0; end; }