AlgoML
FR

Expressions and operators

Arithmetic, comparisons, logic, operator priority and compound assignment.

Arithmetic

OperatorMeaningExampleResult
+ - *Add, subtract, multiply7 * 214
/Division, always a REAL7 / 23.5
divInteger quotient7 div 23
modRemainder7 mod 21

div and mod work on INT values. To keep an integer result, use div rather than /.

Comparisons

OperatorMeaning
=equal
<>different
< <=lower, lower or equal
> >=greater, greater or equal

Note that = compares and <- assigns. A comparison gives a BOOLEAN.

Logic

AND, OR and NOT combine BOOLEAN values. Their operands must be BOOLEAN: NOT 5 is an error.

Algorithm Operators
version '1.0'
language C
variable a : INT
variable b : INT
variable even : BOOLEAN
begin
a <- 17
b <- 5
write(a div b, "\n")
write(a mod b, "\n")
even <- a mod 2 = 0
if NOT even AND a > b then
write("a is odd and greater than b\n")
end if
a +<- 3
write(a, "\n")
end
end algorithm
Output 3 2 a is odd and greater than b 20operators.aml on GitHubTry itSoon

Priority

From the highest to the lowest:

  1. NOT and the unary -
  2. *, /, div, mod
  3. +, -
  4. comparisons
  5. AND, OR, from left to right

So a mod 2 = 0 is read as (a mod 2) = 0. When in doubt, add parentheses: they make the intention clear and change nothing in the generated code.

Compound assignment

Short formSame as
x +<- 3x <- x + 3
x -<- 3x <- x - 3
x *<- 3x <- x * 3
x /<- 3x <- x / 3

They work on INT and REAL variables. Remember that / gives a REAL: x /<- 2 is refused when x is an INT.

Report a problem with this page