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 ExpressionAn 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 OperationTo 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); Arithmetical Operations'++'/'--' OperatorThe "++" 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; 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; 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.
'-' OperatorThe "-" operator is also available as unary operator and negates the value of the given operand. It supports int and float values. -4; 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 OperatorsThe 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; 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.
'%' OperatorThe "%" 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 StringsThe "+" 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; 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'!' OperatorThe "!" operator is a unary operator, which logically negates the value of the given expression. false becomes true and vice versa. !3 '&&'/'||' OperatorThe "&&" 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 Those operations are usually used within statements, which are required to make a decision like the »if statement as described later on.
Comparison OperatorsWith 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 Bit OperationsBit 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. '~' OperatorThe "~" 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) '^'/'&'/'|' OperatorThe "^", "&" 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) 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) The difference is the same for "|" and "||". Shifting BitsThe 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) 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) Again, pay attention to the difference between the shifting operators ("<<", ">>", ">>>") and the comparison operators ("<", ">").
Additional Assignment OperationsTo 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 TypesOperands are automatically converted, e.g. from bool to int, when making an assignment (see also »Conversion Between Data Types). int i = GetMatrixWidth() > GetMatrixHeight();
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 ExampleThe 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[]; |
MADRIX Version: 5.7 | Script Version: 3.23 |
[Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100% |
Previous
Next
|
Enable automatic translation | Activer la traduction automatique | |