Expressions

  Previous  Next

Introduction

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 operations 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 Operation

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; 
 

Arithmetical Operations

'++'/'--' Operator

The "++" and "--" operators are unary operators and only defined for usage with variables of the type int. These operators are known from C/C++ and can be used in two different ways, as prefix and as suffix operators (i++ and ++i).

The prefix operator increments/decrements the current value of the given variable and results in the new value afterwards.

int i = 4;
i = ++i * 2;
 
 

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

The suffix operator results in the current value of the given variable and increments/decrements it afterwards.

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.

-4;
-i;
-(3.5 * 4.2);
 
 

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 Operators

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.

 

'%' Operator

The "%" operator calculates the integer remainder of an integer division. It is called the modulo operator.

10 % 2;
 
 

The operator % is only defined for int values.

 

Concatenating Strings

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

string s = "Hello " + "World!";
 
 

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 = i + "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".

 

Logical Operations

'!' Operator

The "!" operator is a unary operator, which logically negates the value of the given expression. false becomes true and vice versa.

!3
!(3 > 4)
!"Hello World!"
 
 

'&&'/'||' Operator

The "&&" and "||" operators are logical operators. They need operands of the data type bool and always result in a bool value. "&&" is called the logical AND operator, "||" is called the logical OR operator.

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 are some examples for using those operators:

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

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

 

Comparison Operators

With comparison operators you can test two expressions for a certain relation. Possible comparisons (and operators) are "less than" (<), "less equal" (<=), "greater than" (>), "greater 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). Comparison operators always return a bool value, the comparison is either true or false.

i > 4
3 < j
 

Bit Operations

Bit operations manipulate integer values bitwise. In MADRIX Script integer values always consist of 32 bits. The functionality of the following operators becomes clearer if you imagine a decimal number as a series of 32 bits, for example: (5)10 = (0000 0000 0000 0000 0000 0000 0000 0101)2 = (101)2. The leftmost (most significant) bit specifies the sign of an integer value, for example: (-5)10 = (1111 1111 1111 1111 1111 1111 1111 1011)2.

'~' Operator

The "~" operator is a unary operator, which negates each bit of the given value. 0 becomes 1 and vice versa.

~5        // (5)  = (0000 0000 0000 0000 0000 0000 0000 0101) -> (1111 1111 1111 1111 1111 1111 1111 1010) = (-6)
~(-5)        // (-5) = (1111 1111 1111 1111 1111 1111 1111 1011) -> (0000 0000 0000 0000 0000 0000 0000 0100) =  (4)
 
 

'^'/'&'/'|' Operator

The "^", "&" and "|" operators are bit operators. "^" is called the bitwise XOR operator, "&" is called the bitwise AND operator, "|" is called the bitwise OR operator. Here are some examples:

int i = 6 & 5;                // i = (110) & (101) = (100) = (4)
int j = 3 | i;                // j = (011) | (100) = (111) = (7)
int k = i ^ 5;                // k = (100) | (101) = (001) = (1)
 

Make sure that you can distinguish the meaning of a single ampersand (bitwise AND operator) and a double ampersand (logical AND operator). Bit operators combine the corresponding bits of integer values while logical operators combine bool values.

int i = 6 & 5;                // i is 4 (see example above)
int j = 6 && 5;                // j is true (1)
 

The difference is the same for "|" and "||".

Shifting Bits

The shifting operators "<<", ">>" and ">>>" move the bits of an integer value by a specified offset to the left or right.

int i = 6 << 1;                // i = (110) << (1) = (1100) = (12)
int j = 6 >> 2;                // j = (110) >> (2) = (1)
 

The right shifting operators ">>" and ">>>" cause the same results for positive values, but for negative numbers only ">>>" preserves the sign.

int i = -6 >> 2;        // i = (1111 1111 1111 1111 1111 1111 1111 1010) >>  (2) = (0011 1111 1111 1111 1111 1111 1111 1110) = (1073741822)
int j = -6 >>> 2;        // j = (1111 1111 1111 1111 1111 1111 1111 1010) >>> (2) = (1111 1111 1111 1111 1111 1111 1111 1110) = (-2)
 

Again, pay attention to the difference between the shifting operators ("<<", ">>", ">>>") and the comparison operators ("<", ">").

 

Additional Assignment Operations

To have less code and increase readability, there are additional assignment operators: +=, -=, *=, /=, %=, ^=, |=, &=, <<=, >>=, >>>=.

i *= 3 + 4
 

is the same as

i = i * (3 + 4)
 

and so 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;
 SetAsync(true);
 SetBpm(600.0);
}
 
void RenderEffect()
{
 //calculate the color for the next pixel
 g_color.r += (int)(255.0 * (0.5 + 0.5 * sin((float)(g_point[0] * g_point[0]))));
 g_color.g += (int)(255.0 * (0.5 + 0.5 * cos((float)(g_point[0] * g_point[1]))));
 g_color.b += (int)(255.0 * (0.5 + 0.5 * sin((float)(g_point[0] - g_point[1]))));
 g_color.w += (int)(255.0 * (0.5 + 0.5 * sin((float)(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;
 
 setPixel(g_point);
 //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: 5.6 | Script Version: 3.18
[Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100%
 Previous   Next

 


Enable automatic translation | Activer la traduction automatique |