Print  Previous  Next


Expressions are used to calculate values.
Additionally, they can be assigned to variables or given as parameters to functions.
If an expression is followed by a semicolon, it is called an expression statement.
There are different operators available to write expressions.


Operands For An Expression

An expression is formed of operands and operators. The number of operands depends on the operator. There are unary operators, which use only one operand, such as "-". And there are binary operators, which need two operands, like "+".

Operands of an expression can be function calls, variables, constant values like "5" or another expression within brackets, like (3 + 5).


Assignment Operator

To assign any value to a variable the assignment operator "=" is used in the following way:

variable = expression;


The expression after the "=" may be any complex expression. But it has to be compatible with the given variable after »the conversion rules of MADRIX Script.

float f = cos(0.5);

float f2 = f; 



Math Operations

'++'/'--' Operator

The ++ and -- operators are unary operators and only defined for usage with variables of the type int. They are used like this:




They increment/decrement the variable by one. This operator is known from C/C++. This programming language offers two possible ways of using them, as suffix and as prefix operators (i++ and ++i). In MADRIX Script they are currently only available as suffix operators (i++) and they work in that way. Such a suffix operator results in the current value of the given variable and afterwards increments/decrements it.

int i = 4;

i = i++ * 2;


In the end, i  is 8. In the first line, 4 is assigned to the integer value i. In the second line, the expression i++ results in 4. The multiplication therefore is "4 * 2" which results in 8. This value is assigned to i and therefore, i = 8.


'-' Operator

The "-" operator is also available as unary operator and negates the value of the given operand. It supports int- and float-values.



-(3 * 5);


The "+" operator may also be used as unary operator, but it does not make sense because it does not change the result of an expression.


Binary Operations

The operators +, -, * and / support int and float values and cause a usual arithmetical addition, subtraction, multiplication, and division of the two operands.

i = 4 + 4 * 5;

i = (4 + 4) * 5;

i = 4 / 5;


Operator precedence rules are considered. Note that operations with integer values result in integer values and are done as integer operations. So 3 / 4 does not result in 0.75 as may be expected, but in 0. To get a result of 0.75, at least one of the operators must be a float value. An example would be 3.0 / 4, where the 4 is also converted into a float-value.


Concatenating Strings

The "+" operator can also be used to concatenate strings together.

string s = "Hallo " + "Welt"; 


If one of the two operands is of the type string, the other one is converted and the two strings are concatenated together.

int i = 4;

string s = 4 + "th run";


This example results in "4th run". Note that the following example may be misinterpreted since the first part of the expression is of the type integer and will result in an integer. It is then converted and concatenated into one string.

string s = 3 + 4 + "th run";


The resulting string is "7th run" and not, as perhaps expected "34th run".


Modulo Operation

The modulo operation calculates the integer remainder of an integer division. The operator in MADRIX Script for modulo operations is %.

10 % 2;


The operator % is only defined for int values.


Additional Assignment Operations

To have less code and increase readability, there are additional assign operators: +=, -=, *=, /=, %=.

i += 3 + 4


is the same as

i = i + (3 + 4)


and so on.

The resulting source code is much more easy to read.


Operations Of Comparison

With operations of comparison you can test two expressions for a certain relation. Possible comparisons (and operators) are "less than" (<), "less equal" (<=), "more than" (>), "more equal" (>=), "equal" (==) and "not equal" (!=). Make sure that you can distinguish the meaning of a single equal sign (assignment operator) and a double equal sign (comparison operator). Operators of comparison always return a bool value, the comparison is either true or false.

i > 4

3 < j


Logical Operations

The '!' Operator

The !-operator is an unary operator, which logical negates the value of the given expression. false becomes true and vice versa.


!(3 > 4)

!"Hallo Welt"



'And'/'Or' Operator

The and and or-operators are logical operators. They need operands of the data type bool and always result in a bool value. In MADRIX Script the and-operator is expressed via "&&" and the or-operator is declared with "||".

Unlike C/C++, in MADRIX Script both operands are always evaluated. So, even if the first operand of an "&&" - operator results in false, this means that the whole expression will be false. But the second operand will be calculated, too. Here some examples for using those operators:

int i = i || j

int i = (3 < 4) || (4 > 3)


Those operations are usually used within statements, which require to make a decision like the »if statement as described later on.


Using Operands Of Different Data Types

Operands are automatically converted, e.g. from bool to int, when making an assignment (see also »Conversion Between Data Types).

int i = GetMatrixWidth() > GetMatrixHeight();

int j = (2 * sin(PI) * i) + (2 * cos(PI) * !i);  


In the first line, the resulting bool-value of the ">"-operator is implicitly converted into int and results in 0 or 1. In the second line, the 2 in both expression parts is converted to float since the sin and the cos functions result in float. The same holds true for the operands i and !i.


Full Example

The following example for the MAS Script Effect uses different expressions to calculate the coordinates on which the next pixel is to be set. It also calculates the color of the next pixel.

int g_point[];

persistent color g_color;

void InitEffect()


    g_point[0] = 0;

    g_point[1] = 0;

    color c = {random(0, 255), random(0, 255), random(0, 255), random(0, 255)};

    g_color = c;




void RenderEffect()


    //calculate the color for the next pixel

    g_color.r += (int)(255.0 * (0.5 + 0.5 * sin(g_point[0] * g_point[0])));

    g_color.g += (int)(255.0 * (0.5 + 0.5 * cos(g_point[0] * g_point[1])));

    g_color.b += (int)(255.0 * (0.5 + 0.5 * sin(g_point[0] - g_point[1])));

    g_color.w += (int)(255.0 * (0.5 + 0.5 * sin(g_point[0] + g_point[1])));


    //make sure, colors are only between 0 and 255

    g_color.r %= 256;

    g_color.g %= 256;

    g_color.b %= 256;

    g_color.w %= 256;                                        



    //setup the next point, x++

    //if x > MatrixWidth x = 0 and y++

    //if y > MatrixHeight y = 0

    g_point[0] = (g_point[0] + 1) % GetMatrixWidth();

    g_point[1] = ((int)(g_point[0] == 0) * 1 + 

                        g_point[1]) % GetMatrixHeight();



void setPixel(int pt[])


    SetPixel(g_color, pt[0], pt[1]);



MADRIX Version: 3.6f | Script Version: 2.19
[Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100%
Print   Previous   Next