Tutorial: Operators
How to use +, -, *, /, and other operators with various types.What are Operators?
In computer science, we often want to ask the computer to do certain mathematical operations. For example, we may want the computer to compare a set of numbers to see if they are equal or we may want to add, subtract, multiply, or divide numbers. In this section, we discuss this concept, operators, including which how they work in the Quorum programming language.
integer and number types
Integers are those numbers, positive or negative, without decimal points, while numbers are any numbers, including those with decimal points. Both integers and numbers have the same operators defined.
Plus
The plus operator adds together numbers and concatenates, or puts together text. The following code would give the variable a the value of 10 and the variable b the value of "firefox.":
integer a = 5 + 5
text b = "fire" + "fox"
output a
output b
Minus
The minus operator subtracts numbers. The following code would give the variable a the value of 0:
integer a = 5 - 5
output a
Times
The plus (star) operator multiples together numbers. The following code would give the variable a the value of 25:
integer a = 5 * 5
output a
Divide
The divide (forward slash) operator divides numbers. The following code would give the variable a the value of 1:
integer a = 5 / 5
output a
Modulus
The modulus (mod) operator returns the remainder in an integer division. The following code would give the variable a the value of 0:
integer a = 5 mod 5
output a
Less than
The less than operator tests to see if the number to the left of the symbol is smaller than the number on the right. The following code would give the variable a the value of true:
boolean a = 5 < 6
output a
Less than or equal to
The less than operator tests to see if the number to the left of the symbol is smaller than or equal to the number on the right. The following code would give the variable a the value of true:
boolean a = 5 <= 6
output a
Greater than
The greater than operator tests to see if the number to the left of the symbol is bigger than the number on the right. The following code would give the variable a the value of false:
boolean a = 5 > 6
output a
Greater than or equal to
The the greater than or equal to operator tests to see if the number to the left of the symbol is bigger or equal to the number on the right. The following code would give the variable a the value of false:
boolean a = 5 >= 6
output a
boolean types
Boolean types are those that have only two values: true and false. Because they only allow two possible values, only equals and not equals can be used.
Equals
The equals sign determines whether two values are the same The following code would give the variable a the value of true:
boolean a = true = true
output a
Not Equals
The not equals (not=) sign determines whether two values are not the same. The following code would give the variable a the value of false:
boolean a = true not= true
output a
And
The 'and' keyword combines boolean expressions and is true if both the left and right hand side is true. Otherwise, it gives false. The following code would give us the value of true for a and false for b:
boolean a = true and true
output a
boolean b = true and false
output b
Or
The 'or' keyword combines boolean expressions and is true if both the left or right hand side is true. If both are false, it gives false. The following code would give us the value of true for a and false for b:
boolean a = false or true
output a
boolean b = false or false
output b
text types
The text type allows you to type a list of characters that the computer can process. Strings have only one operation that is allowed on them, the plus operator, which adds two strings together. This is called concatenation. The following code would give the variable 'combined' the value of "Hello, World!."
text a = "Hello, "
text b = " world!"
text combined = a + b
output combined
Concatenate
Text values can also be combined with other types, like integers, numbers, or booleans using the same operator, plus. For example, we could create an integer and add it to a text field. The following code would give the variable a the value of "My favorite number is 5."
text a = " My favorite number is " + 5
output a
Next Tutorial
In the next tutorial, we will discuss Lab 2.2, which describes how work math in Quorum..