Version 0.59.0

Basic Operators

Operators are needed to do calculations on values and variables. They are single characters or short character sequences that act on one or more variables or values to their left or to their right. An example is the addition operator '+' that combines the two numbers (of data type int or float) on its left and on its right to a third number. The example shows how the numbers 3 and 13 are added together by the '+' operator, the result is assigned to a variable and the variable value is returned:

Example

int i = 3 + 13
echo( i )

Output

16

The variable can be used itself as value that is added to another number. The result is directly returned in the second example:

Example

int i = 3 + 13
echo( i + 5 )

Output

21

Here is a list of the different groups of basic operator of trCAD that are discussed in this section:

Basic Arithmetic Operators

The four basic arithmetic binary operators (i.e. they act on two values) are

+ - * / %

and they represent the mathemtical operations "addition", "subtraction", "multiplication", "division" and "modulo". Like in the example above they are placed in between the two values they operate on.

Closely related to these are the four arithmetic assignment operators

+= -= *= /= %=

that are used to add/subtract/multiply/divide/get modulo from one value with the variable on the left of the operator. That means that the two following examples have an equal effect:

Example

i = i * 5

Example

i *= 5

Negation Operator

The unary (i.e. its working only on one value) operator '-' can stand in front of a numerical data type and flips it sign from positive to negative value or vice versa:

Example

int i = 5
echo( -i )

Output

-5

Parantheses

We want to introduce the function of simple parantheses '()' here. In the code they act as structuring method when applying multiple operators in one expression. Everything inside two associated parantheses is evaluated first before the result is applied to other operators outside the parantheses. The following example depicts this behavior:

Example

int i1 = 10 - 5 - 3       // results in 2
int i2 = 10 - ( 5 - 3 )   // results in 8

Incrementor and Decrementor

The incrementor and decrementor operators come in two flavours: as prefix and as postfix operators:

++i i++ --i i--

Their function is to add (++) or subtract (--) a value of 1 to the numeric variable i. The difference between the prefix and the postfix version is that the prefix version first increments/decrements the variable and then returns the new value for further operations while the postfix version first returns the variable's former value and then increments it for later use. This becomes clearer in the following examples:

Example

int i = 5;
echo( i++ )
echo( i )

Output

5
6

Example

int i = 5;
echo( ++i )
echo( i )

Output

6
6

Comparison Operators

The comparison operators are used to compare two values and return a boolean value that represents the result of the comparison. All comparison operators are binary operators that stand inbetween the two values they compare. There are the following comparison operators:

== != > < >= <=

The first operator '==' checks for equality of the two values and returns a value 'true' if they are equal and 'false' if they differ. Similarly operator '!=' checks for inequality. Operators '>' and '<' check two number for beeing greater than and less than each other and the operators '>=' and '<=' do the same for the greater than or equal to and the less than or equal to relations.

Example

echo( 5 == 3 )      // outputs 'false'
echo( 5 != 3 )      // outputs 'true'
echo( 5 > 3 )       // outputs 'true'
echo( 5 > 5 )       // outputs 'false'
echo( 5 < 3 )       // outputs 'false'
echo( 5 >= 3 )      // outputs 'true'
echo( 5 >= 5 )      // outputs 'true'

Logical Operators

Logical operators perform operations on boolean values. They are

&& || !

The first two are binary operators representing the logical "AND" and "OR" operations (see https://en.wikipedia.org/wiki/Boolean_algebra ). The third is the logical negation operator that flips values true into false and vise versa.

Example

echo( true && false )      // outputs 'false'
echo( true || false )      // outputs 'true'
echo( !false )             // outputs 'true'

Text Concatenation Operator

The concatenation operator uses the same symbol '+' as the addition operator. The trCAD script compiler can distinguish between the two by checking what data types are applied to the left of the operator. If this value is not a number but a string, the concatenation operation is automatically applied.

The concatentation operator concatenates two strings to one. This is an easy way to build up texts from smaller snippets. It expects a string value on its right hand side but since all of the data types are convertible to a string, one may put any variable at the right hand side: automatic convertion will turn it into a string. The following example builds up string from different values by concatentation and outputs the result:

Example

int i = 5
string s1 = " has the value "
string s2 = "i " + s1 + i
echo( s2 )

Output

i has the value 5

Since the echo command actually only accepts a single string as argument, we can use the concatenation operator directly inside its argumental parantheses:

Example

int i = 5
echo( "The value of i is " + i )

Output

The value of i is 5

Note

String concatenation is performed by the '+' operator in a trCAD script.

🗙

Search results for