Comparison

 

Comparison operators are used to specify logical and relational expressions.

 

Logical

The basis of logic is whether statements are true or false. Logical operators determine whether statements are true or false, then execute them appropriately. You can use logical operators with int, float, and vector data types.

 

The values of each type correspond to being either true or false. An int or float value is considered false when its value, converted to type int, is 0. A vector is considered to be false when its magnitude value, converted to type int, is 0. All other converted values are considered to be true.

 

Note the difference

In MAXScript, true and false are values of the BooleanClass. Other values like Float and Integer cannot be considered by logical operations directly. This is a big difference to the MEL logical operations.

 

The table below displays the symbol and logical meaning of the three logical operators:

 

Symbol

Logic

True only if:

||

or

either left-hand or right-hand side is true

&&

and

both left-hand and right-hand sides are true

!

not

right-hand side is false

 

Examples

if (0 || 1) print("true\n"); // True

if (0 && 1) print("true\n"); // False

if (2 && <<3, 7.7, 9>>) print("true\n"); // True

if (! 5.39 && 7) print("true\n"); // False

if (<<0, 0, 0>> || 0) print("true\n"); // False

if (! <<0, 0, 0>>) print("true\n"); // True

 

 

In MAXScript, the logical operators are written as keywords - in fact, they are the same as the ones listed in the column Logic in the above table.

 

Since integers and floats cannot be used in logical operations directly, the above examples cannot be translated to MAXScript directly.

 

if (0 > 0 or 1 > 0) do format "true\n") -- True

if (0 > 0 and 1 > 0) do format "true\n" -- False

if (2 > 0 and [3, 7.7, 9].x > 0 ) format "true\n" -- True

if (not 5.39 > 0 and 7 > 0) format "true\n" -- False

if ([0, 0, 0].x > 0 or 0 > 0) do format "true\n" -- False

if (not [0, 0, 0].x > 0) do format "true\n" -- True

 

Relational

Relational operators compare two values. They are valid with int, float, and vector data types. The truth of the statement depends on the relation of the left-hand side value to the right-hand side. The following table lists the relational operator symbols and the conditions when a statement using them is true.

Symbol

True only if the left-hand side is

<

less than the right-hand side

>

greater than the right-hand side

==

equal to the right-hand side

!=

not equal to the right-hand side

>=

greater than or equal to the right-hand side

<=

less than or equal to the right-hand side

 

Examples

if (-2.5 < 1) print("true\n"); // True

if (16.2 > 16.2) print("true\n"); // False

if (-11 == -11) print("true\n"); // True

if (-11 != -11) print("true\n"); // False

if (-11 >= -11) print("true\n"); // True

if (1 <= 0) print("true\n"); // False

 

MAXScript supports exactly the same relational logical operators, in exactly the same way.

if -2.5 < 1 do format "true\n" -- True

if 16.2 > 16.2 do format "true\n" -- False

if -11 == -11 do format "true\n" -- True

if -11 != -11 do format "true\n" -- False

if -11 >= -11 do format "true\n" -- True

if 1 <= 0 do format "true\n" -- False

Parentheses around the logical expression are not required, although they are allowed and could improve readability, like this:

if (-2.5 < 1) do format "true\n"

 

 

 

When a vector type is operated on by the <, >, > =, or < = operator, its magnitude is used instead of the vector itself. The magnitude of the vector is of type float.

 

Examples

if (<<1, 2, 3>> < <<3, 2, 1>>) print("true"); // False

if (<<1, 2, 3>> <= <<3, 2, 1>>) print("true"); // True

if (<<0, 0, 4>> > <<3, 2, 1>>) print("true"); // True

if (<<0, 5, 0>> <= <<-3, -4, 0>>) print("true"); // True

 

MAXScript does not support the directly comparison of vectors. You would have to explicitly state what should be compared:

if length [1, 2, 3] < length [3, 2, 1] do format "true\n" -- False

if length [1, 2, 3] <= [3, 2, 1] do format "true\n" -- True

if length [0, 0, 4] > [3, 2, 1] do format "true\n" -- True

if length [0, 5, 0] <= [-3, -4, 0] do format "true\n" -- True

 

 

When the == or != operator compares two vectors, the vectors themselves are used. If each component of one vector is the same as the corresponding component of the other vector, the vectors are equal.

 

Examples

if (<<1, 2, 3>> == <<3, 2, 1>>) print("true"); // False

if (<<1, 2.2, 3>> == <<1, 2.2, 3>>) print("true"); // True

if (<<1.2, 2, 3>> != <<0, 0, 0>>) print("true"); // True

 

Same applies to MAXScript:

if [1, 2, 3] == [3, 2, 1] do format "true\n" -- False

if [1, 2.2, 3] == [1, 2.2, 3] do format "true\n" -- True

if [1.2, 2, 3] != [0, 0, 0] do format "true\n" -- True

 

 

  

WARNING

Note the difference between the assignment operator (=) and the equality operator (==). Sometimes the error of using one instead of the other is hard to find. This applies to both languages.

 

For example, using the assignment operator instead of the equality operator produces a value. This value can be used as a logical truth or falsehood. Mixing up these operators the other way is just as dangerous since a variable may be assigned a value when it was not intended to be.

 

In MAXScript, using the assignment operator instead of the equality operator produces a value. This value cannot be used as a logical truth or falsehood, causing an ERROR. 

 

Comparisons evaluated as integers

All comparison operators produce integer results. Although it is unusual to mix comparison operators and arithmetic in the same statement, it is legal. In fact, you can use an int variable as part of a logical statement and to store logical results.

 

Examples

int $sra = (4.7 == 4.7) + 5; // Assigned: 6

int $rra = <<1, 2, 3>> >= <<9, 0, 0>>; // Assigned: 0

int $rrr = 0 || 72.3; // Assigned: 1

int $sar = 3 * (1.2 && ! <<0, 0, 0>>); // Assigned: 3

if ($rra || $rrr) print("true\n"); // True

 

Logical operators only work using integer operands. Although you can compare true values it is dangerous to do so. Consider the following example:

int $xsv = 5;

if ($xsv) print("true\n"); // True

if (true) print("true\n"); // True

if ($xsv == true) print("true\n"); // False

 

Variable $xsv represents a true value because its value, converted to type int, is not 0. However, there are many int values that are not 0. In fact, the keyword true also represents a true value since its value is 1 (see Constants). However, both of these true values are not equal to one another. One int value is 1 and the other int value is 5.

 

Due to this potential problem, you should not compare true values. Even if both values are true values, it does not mean that the values are equal, as the above example illustrates.

 

 

MAXScript does not support numbers as logical operands in place of BooleanClass operands. Trying to use a non-BooleanClass value as logical expression will cause an error:

a = 1

if a do format "True\n"

-- Type error: if-test requires BooleanClass, got: 1

 

The only class that can be explicitly converted to BooleanClass without the use of logical operators is String, when containing the string values "True", "False", "On" and "Off". This only works in 3ds max 7 and higher.

a = "True"

if a as BooleanClass do format "True\n"