Skip to content

Expressions

An expression is the building block of a computer program. In fact the vast majority of instructions in a typical computer program are probably expressions. An expression is basically any computer instruction that results in a value being created or changed. An infinite variety of expressions is possible, but they are all made up of a finite list of building blocks:

  1. Values
  2. Functions
  3. Operators

Values

Generally speaking, there are two distinct kinds of values that appear in expressions. There are variables, that have previously been saved into memory. And then there are literal values, which are any values that we have typed directly into our code. Here's an example of an expression that contains both a variable and a literal value:

1
myAge + 25;

myAge is a number variable. 25 is a number literal.

Most of the types of variables available in the Modulo1 language can be also be used as literal values within an expression. For example:

1
"My age is " + 34;

"My age is " is a string literal. 34 is a number literal.

1
[1, 2, 3] && { name : "smith" };

[1, 2, 3] is an array literal. { name : "smith" } is an object literal.

1
null or true;

null is a null literal. true is a boolean literal.


Functions

Functions always result in some kind of value (even if the value is sometimes a useless null value). And therefore functions are an important part of many expressions. The simplest way to use a function in an expression, is for the function to be run alone:

1
myFunction();

This expression, above, will generate a value from the function but it does not do anything with that generated value.

Here is a slightly more complex expression that does something with the generated value:

1
myFunction() * 10;

The result of myFunction() is multiplied by 10


Operators

An operator is kind of like a function. Except that they are built into the Modulo1 language, and instead of being identified by a name, they have are represented by symbol/s instead.

Several operators can be used in one expression, like this:

1
((32 + 12) / 7 * 2) % 5;

Or they can be used individually:

1
1 + 1;

The + operator performs addition when performed on numbers

1
"Hello " + "World";

The + operator performs concatenation when performed on strings (that is, it joins the strings to each other and to other variables)

1
5 - 3;

The - operator performs subtraction

1
6 * 9;

The * operator performs multiplication

1
896 / 45;

The / operator performs division

1
101 % 2;

The % operator performs modulus

The operators above all perform mathematical calculations. But there are also operators designed specifically for making comparisons between values. These operators return either true or false:

1
x == y;

The == operator returns true if x and y are equal

1
x != y;

The != operator returns true if x and y are not equal

1
x > y;

The > operator returns true if x is greater than y

1
x < y;

The < operator returns true if x is smaller than y

1
x >= y;

The >= operator returns true is x is greater than or equal to y

1
x <= y;

The <= operator returns true if x is smaller than or equal to y

Several comparison operators can be combined together through the use of logical operators:

1
(x == y and z != w) or (a > b and c <= d);

Or logical operators can be used on their own:

1
x and y;

The and operator returns true if x and y are both true

1
x or y;

The or operator returns true if either x or y are true

1
x xor y;

The xor operator returns true if x is true and y is false, or vice versa

None of the expressions that have appeared on this page so far actually cause their resulting value to be saved, but there certainly are expressions that do cause a value to be saved. They are collectively referred to as assignment operators:

1
var x = y;

The = operator is the most common assignment operator and it can be used for all types of variables.

1
var x += y;

The += operator adds x to y, then replaces x with the result

1
var x -= y;

The -= operator subtracts y from x, then replaces x with the result

1
var x *= y;

The *= operator multiplies x by y, then replaces x with the result

1
var x /= y;

The /= operator divides x by y, then replaces x with the result

1
var x %= y;

The %= operator divides x by y, then replaces x with the remainder