Here are the operators you can use in the Quby language.
Standard mathematical operators are supported in Quby.
Used for addition and string concatenation. If both values are a number, then the values are added together. If one of the values is a string, then the other is converted to a string, and they are concatenated into a new string.
age = 20 + 43
// string concatenation
description = "John is " + age + " years old!"
The minus symbol is used for subtraction between two numbers.
startX = getScreenWidth() - 100
endX = startX - 200
The forward slash is used for division. The left side is divided by the right, and the result is returned.
x = getScreenWidth() / 2
y = getScreenHeight() / 3
The star symbol is used for multiplication between numbers. The left side is multiplied against the right, and the result is returned.
total = 10 * 0.75
doubleTotal = total * 2
This is shorthand as the 'power' operator, and raises the left side to the power of the right side.
// x is set to 16
x = 2 ** 4
// hp is set to 1000
hp = 10 ** 3
The logical boolean operators do not return true or false, instead returning the left or right side as appropriate. This is because all objects equate to true, except for false and null.
The 'logical or' first evaluates the left side. If it is true, then it is returned, and the program moves on. However if it is false, then the right side is evaluated, and returned.
/* Calls 'showTitle' when the mouse is clicked,
* or the keyboard is pressed. Either can happen. */
c = getControls()
if c.isLeftClick() || c.isKeyPressed()
Note that 'logical or' does not strictly return true or false, instead returning the left or right side. As a result this operator can be used for initializing default values.
def setX( x )
$x = x || 0
// $x is set to 0
setX( null )
// $x is set to 49
setX( 49 )
Either 'or' or '||' can be used for the logical or. These are interchangeable, and denote exactly the same operation, with no differences in precedence.
The 'logical and' operator first evaluates the first side. If this equates to false, then it is returned, making the whole operator return false. If the left side is true (or something that equates to true), then it will evaluate the right side, and return the value.
Note that the logical and does not strictly return 'true' or 'false', but instead returns the left or right side depending on if they are the equivalent of being true or false.
if hp <= 0 && lives > 0
if hp <= 0 and lives > 0
Both 'and' and '&&' are the logical and operators, and they are entirely interchangeable. There is no difference between them.
The 'not' operator is used for 'negating' a value. This finds out what boolean value it equates to, and then returns the opposite of this.
If the value is null or false, then performing ! will return true. Otherwise the not will always return false, as all other values (such as numbers, string, arrays and objects) all equate to true.
The double equals is used for equality, to check if two values match. They match if they are same object, number, string or value.
If the two values match, true is returned, and otherwise false.
a = 4
b = rand( 0, 10 ).round()
if a == b
console( 'lucky match!' )
The greater than operator, >, is used for testing if the left hand value is larger then the other. If it is, then true is returned, and otherwise false.
playerY = player.getY()
otherY = other.getY()
// draw player and other back to front
// so the one with the smaller 'y' value is drawn first
if otherY > playerY
Greater equals works the same, but returns true if the two values are also equal.
The less than operator, <, is similar to greater than, but works in the opposite direction. It returns true if the left side is less then the right side.
Like greater then, there is also a 'less than or equal' version of the less than operator:<=.
Quby supports bitwise operators for bitwise and and or. These are useful for working with values packed into a single number, such as some colours.
Be aware that bitwise operators require an understanding of how numbers are represented on computers, as a series of bits.
The single bar symbol is used for the bitwise or operator. The value on the left and right are converted to numbers, and then each bit in turn is compared against each other, and set into a new number.
In essence, this combines the 1 bits from two numbers into one number.
// colours set to their hex numbers
red = 0xaa0000
green = 0xff00
blue = 0x93
// colour is set to 0xaaff93
color = red | green | blue
The bitwise and with convert each side to a number, and go through them bit by bit. If both bits are a 1, then the bit in the resulting number is also a 1, and otherwise it is set to a 0.
This particularly useful for grabbing out specific sections of a number.
color = 0xffaabb
// blue is equal to just: 0xbb, the end of color
blue = color & 0xff
The left shift operator will shift all values in 'a', by the number of times denoted by 'b'.
red = 0xff
green = 0xbb
blue = 0xaa
// All values or'd together,
// after they have been 'shifted' into position.
// so color is: 0xffbbaa
color = ( red << 16 ) | ( green << 8 ) | blue
Bits shifted will wrap around the number, off the left hand side and come back onto the right.
The right shift is the same as the left shift, but works in the opposite direction.
color = 0xffbbaa
red = ( color >> 16 ) & 0xff
green = ( color >> 8 ) & 0xff
blue = color & 0xff