Arithmatic and String Operators

Numeric operators are for addition, subtraction, multiplication, division, and other arithmetic operations. The following operators can be used in the Toolset:


Arithmatic Operators (perform operations on int and float data types)

Symbol Operation Description
+ Addition Simple addition.
-- Decrement Decrements the value of a variable by one.
/ Division Simple division. Division by 0 results in the classic "Divide by zero" error.
++ Increment Increments the value of a variable by one.
* Multiplication Simple product. Multiplication by zero always results in 0, multiplying by 1 always returns the number being multiplied.
% Modulo Calculates the remainder of a division statement (not the result; works on integers only).
- Subtraction Simple subtraction.

String Operators (perform operations on string data type)

Symbol Operation Description
+ Concatenation Concatenates, or appends, one string to another.

The '=' assignment is used to assign the value of an expression on its left side to a variable on the right.

int nSomeNumber = 23;
int nAge = nSomeNumber;

If the data type of nSomeNumber does not evaluate to the data type of nAge, then nSomeNumber will need to be cast using the appropriate function (IntToString(), FloatToInt(), or any other type casting/conversion function).


When doing numeric assignments it's important to make sure that the result is stored in the right data type. This is mainly an issue with division in which the decimal value needs to be preserved. If assigned to an int variable, the result will be truncated (everything after the decimal place will be lopped off).


int nResult = 10 / 3;

After the above expression nResult will equal 3.

float nResult = 10 / 3.0;

After this expression nResult will equal 3.333333254


The only operator that will work on string data is the concatenation operator ("+").

string sFullName = "John" + " " + "Doe";
// sFullName will equal "John Doe"
Data casting (converting one data type to another) is important when concatenating string and numeric data. The concatenation operator will only work on string variables. For instance:
// incorrect
int nTime = GetTimeHour();
string sTime = "The time is: " + nTime;

This example will not compile, and will return the error 'ARITHMETIC OPERATION HAS INVALID OPERANDS', because nTime is of type int.

// correct
int nTime = GetTimeHour();
string sTime = "The time is: " + IntToString(nTime);

nTime was cast to string using the IntToString() function - this time it will compile correctly.


Assignment operators do something to the variable being assigned:

int nDays = 4;
// the following is functionally equivalent to
// nDays = nDays + 7;
nDays += 7;
// nDays now equals 11
string sName = "Tom";
sName += " Cassiotis";
// sName now equals "Tom Cassiotis"

Note that the ++ and -- operators affect variables differently depending upon where they are placed; i.e. variable++ uses the variable's value first and then increments it, while ++variable increments the value and then uses it (likewise with the -- operator). Confused? Look at the example below:

void main()
{
    int nTest = 1;
    SpeakString(IntToString(nTest));          // display 1
    SpeakString(IntToString(++nTest));        // display 2
    SpeakString(IntToString(nTest));          // display 2
    SpeakString(IntToString(--nTest));        // display 1
    SpeakString(IntToString(nTest));          // display 1
    SpeakString(IntToString(nTest++));        // display 1
    SpeakString(IntToString(nTest));          // display 2
    SpeakString(IntToString(nTest--));        // display 2
    SpeakString(IntToString(nTest));          // display 1
} 




 author: Ryan Hunt, editor: Charles Feduke, additional contributor(s): Gerry Raban, Michael Nork, Grimlar