Mathematical, boolean and comparing operators

The title of this section might sound very difficult, yet it is not as difficult as it sound.

Mathematical operators

These are the basic math symbols known as: add (+), subtract (-), multiply (*), divide (/) and power (^).

Here a small example of the mathematical operators you can use in TurtleScript:

$add      = 1 + 1
$subtract = 20 - 5
$multiply = 15 * 2
$divide   = 30 / 30
$power    = 2 ^ 2

The values resulting from the mathematical operations get assigned to various variables. Using the inspector you can see the values.

If you just want a simple calculation to be done you can do something like this:

print 2010-12

Now an example with parentheses:

print ( ( 20 - 5 ) * 2 / 30 ) + 1

The expressions inside parentheses will be calculated first. In this example, 20-5 will be calculated, then multiplied by 2, divided by 30, and then 1 is added (giving 2). Parentheses can also be used in other cases.

KTurtle also has more advanced mathematical features in the form of commands. Have a look at the following commands but be aware that it concerns advanced operations: round, random, sqrt , pi, sin, cos, tan, arcsin, arccos, arctan.

Boolean (true/false) operators

Where mathematical operators are mainly for numbers, boolean operators are for boolean values (true and false). There are only three boolean operators, namely: and, or, and not. The following piece of TurtleScript shows how to use them:

$and_1_1 = true and true    # -> true
$and_1_0 = true and false   # -> false
$and_0_1 = false and true   # -> false
$and_0_0 = false and false  # -> false

$or_1_1 = true or true    # -> true
$or_1_0 = true or false   # -> true
$or_0_1 = false or true   # -> true
$or_0_0 = false or false  # -> false

$not_1 = not true   # -> false
$not_0 = not false  # -> true

Using the inspector you can see the values, yet we also supply these results as little comments at the end of the lines. and evaluates true only if both sides are true. or evaluates true if either side is true. And not turns a true into false and a false into true.

Boolean operators are highlighted with pink.

Some more advanced examples

Consider the following example with and:

$a = 1
$b = 5
if (($a < 10) and ($b == 5)) and ($a < $b) {
  print "hello"
}

In this piece of TurtleScript the result of three comparing operators are merged using and operators. This means that all three have to evaluate "true" in order for the "hello" to be printed.

An example with or:

$n = 1
if ($n < 10) or ($n == 2) {
  print "hello"
}

In this piece of TurtleScript the left side of the or is evaluating to 'true', the right side to 'false'. Since one of the two sides of the or operator is 'true', the or operator evaluates 'true'. That means "hello" gets printed.

And finally an example with not which changes 'true' into 'false' and 'false' into 'true'. Have a look:

$n = 1
if not ($n == 3) {
  print "hello"
} else {
  print "not hello ;-)"
}

Comparing operators

Consider this simple comparison:

$answer = 10 > 3

Here 10 is compared to 3 with the 'greater than' operator. The result of this comparison, the boolean value true is stored in the variable $answer.

All numbers and variables (that contain numbers) can be compared to each other with comparing operators.

Here are all possible comparing operators:

Table 4.1. Types of questions

$A == $Bequalsanswer is true if $A equals $B
$A != $Bnot-equalsanswer is true if $A does not equal $B
$A > $Bgreater thananswer is true if $A is greater than $B
$A < $Bsmaller thananswer is true if $A is smaller than $B
$A >= $Bgreater than or equalsanswer is true if $A is greater than or equals $B
$A <= $Bsmaller than or equalsanswer is true if $A is smaller than or equals $B


Please note that $A and $B have to be numbers or variables that contain numbers.