Arithmetics
This chapter covers the arithmetic operations on data types. This includes trivial mathematical operations such as addition and multiplication, as well as boolean logic and overloaded operators on other types.
Mathematical Operations
Mathematical operations in pseudocode are similar to that in any other languages. For instance, the addition of two integers:
A = 24
B = 45
output A + B // outputs "69"
There are 6 math related binary operations: +, -, *, /, % and div, which correspondes to addition, subtraction, multiplication, division, modulus and integer division respectively. Operators have precedence, i.e. A + B * C is interpreted as A + (B * C).
Note
According to the IB Pseudocode Guide, the modulus operators are commonly written in word form: mod. Donkey IDE also supports this. mod is functionally identical to %. For example, 32 mod 3 is the same as 32 % 3.
Note that the quotient of integers is a real number (contains decimal points). To perform integer division, use div instead of /:
output 20 div 3 // outputs "6"
The integer conversion function is crucial, as certain operations require an integer type instead of a real number (even if the real is a whole number), such as indexing an array.
In addition, the subtraction operator - can be used as a unary operator to negate a numeric value:
A = 20
output -A // outputs "-20"
Comparison Operators
Comparison operators compare two values. For instance, A <= B evaluates to true if A is less than or equal to B, and false otherwise.
There are 6 comparison operators in pseudocode:
A == B: Tests for equality ofAandB. Returnstrueif the two operands are equal.A != B: Tests for inequality. Returnstrueif the two operands are not equal.A < B: ReturnstrueifAis strictly less thanB.A <= B: ReturnstrueifAis less than or equal toB.A > B: ReturnstrueifAis strictly greater thanB.A >= B: ReturnstrueifAis greater than or equal toB.
While most of the above operators only work on numeric types (i.e. integer and real), the equality operator == and inequality operator != works on all basic data types, and behaves according to the operands equality. For instance, equality of strings can be tested as such:
output "abc" == "abc" // outputs "true"
Note that the equality and inequality operators does NOT work on compound data types. This means that the equality of, for instance, lists cannot be tested with the equality operator.
Boolean Logic Operators
Boolean logic operators act on operands of type boolean. There are 3 boolean operators:
- A and B: Returns true if both A and B are true.
- A or B: Returns true if either A or B is true.
- not A: Returns the opposite of A, i.e. not true returns false.
Note
Many official IB documents use the capitalized AND, OR and NOT for boolean logic. However, this contradicts with IB’s “all keywords are lowercased while variable names are uppercased”. Since using uppercase for boolean operators makes absolutely no sense, Donkey IDE adheres to the “lowercase keyword” rule.
In addition to the above operators, there is also the unary operator !. This operator, like the not operator, inverses the given boolean value. However, this operator has a higher precedence than any of the comparison operators (this design choice mimics the precedence of the ! operator in most other programming languages).
Precedence Table
The following table lists all the operators from highest precedence to lowest precedence:
!,-(negate)*,div,mod,/,%+,-(subtraction)==,!=,>=,<=,>,<notandor