Operators
Arithmetic, comparison, and logical operators — including string concatenation, floor division, and short-circuit boolean logic.
| Category | Operators |
|---|---|
| Arithmetic | + - * / fld %
** |
| Comparison | == != < > <=
>= |
| Logical | and or not |
String Conversion with +
The + operator is used not only for numeric addition but also for string concatenation. When one
of the operands is a string, the other operand is automatically converted to a string, and the two are joined
together. Concatenation does not add spaces automatically — you must include them explicitly.
tag num = 42
tag text = "The answer is " + num # Auto-converted to string
print(text) # "The answer is 42"
Addition with +
When both operands are numbers, + performs numeric addition.
tag num = 30
tag num1 = 40
tag total = num + num1
print(total) # Output: 70
tag sum = 20 + 30 + 10
print(sum) # Output: 60
tag price1 = 9.99
tag price2 = 5.50
tag total_price = price1 + price2
print(total_price) # Output: 15.49
# Mixed Int and Float (Int promoted to Float)
tag int_val = 10
tag float_val = 3.14
tag mixed_sum = int_val + float_val
print(mixed_sum) # Output: 13.14
print(type(mixed_sum)) # Output: "Float"
tag result = 50 + (-20)
print(result) # Output: 30
Subtraction with -
The - operator performs numeric subtraction. Both operands must be numeric (Int or
Float).
tag difference = 100 - 45
print(difference) # Output: 55
tag float_diff = 10.5 - 3.2
print(float_diff) # Output: 7.3
tag negative = 20 - 50
print(negative) # Output: -30
tag complex = 100 - 30 - 20
print(complex) # Output: 50
tag int_num = 15
tag float_num = 4.5
tag mixed = int_num - float_num
print(mixed) # Output: 10.5
print(type(mixed)) # Output: "Float"
tag text = "hello"
tag num = 10
tag error = text - num # COMPILE ERROR: Cannot perform '-' on Strings
Multiplication with *
The * operator performs numeric multiplication. Both operands must be numeric.
tag product = 7 * 8
print(product) # Output: 56
tag area = 5.5 * 3.0
print(area) # Output: 16.5
tag neg_product = -5 * 3
print(neg_product) # Output: -15
tag volume = 2 * 3 * 4
print(volume) # Output: 24
tag int_val = 3
tag float_val = 2.5
tag mixed = int_val * float_val
print(mixed) # Output: 7.5
Division with /
The / operator performs floating-point division. It always returns a Float, even
when dividing two integers.
tag result = 10 / 2
print(result) # Output: 5.0
print(type(result)) # Output: "Float"
tag float_result = 15.0 / 4.0
print(float_result) # Output: 3.75
tag uneven = 7 / 3
print(uneven) # Output: 2.333333...
tag decimal_div = 100 / 2.5
print(decimal_div) # Output: 40.0
# Order of operations
tag complex_div = 10 + 20 / 5
print(complex_div) # Output: 14.0 (20/5=4.0, +10=14.0)
Floor Division with fld
The fld operator performs integer division, discarding any fractional part. It always returns an
Int.
tag result = 10 fld 3
print(result) # Output: 3
print(type(result)) # Output: "Int"
tag even = 20 fld 4
print(even) # Output: 5
# Negative numbers round toward negative infinity
tag negative = -10 fld 3
print(negative) # Output: -4
# Mixed types (Float gets floored first)
tag mixed = 10.9 fld 2.5 # Same as: 10 fld 2
print(mixed) # Output: 5
Modulo with %
The % operator returns the remainder of division. Works with both Int and
Float.
tag remainder = 17 % 5
print(remainder) # Output: 2
tag zero_remainder = 20 % 4
print(zero_remainder) # Output: 0
tag float_remainder = 10.5 % 3.2
print(float_remainder) # Output: 0.9 (approximately)
# Check for even/odd
tag number = 42
if number % 2 == 0:
print("Even number") # Output: "Even number"
else:
print("Odd number")
# Wrapping values (circular buffer)
tag index = 15
tag size = 10
tag wrapped = index % size
print(wrapped) # Output: 5
Exponentiation with **
The ** operator raises a number to a power. It always returns a Float.
tag square = 5 ** 2
print(square) # Output: 25.0
print(type(square)) # Output: "Float"
tag power = 2.5 ** 3
print(power) # Output: 15.625
# Negative exponent (reciprocal)
tag reciprocal = 2 ** -1
print(reciprocal) # Output: 0.5
# Square root (using 0.5 power)
tag root = 16 ** 0.5
print(root) # Output: 4.0
# Compound interest example
tag principal = 1000.0
tag rate = 1.05 # 5% growth
tag years = 3
tag future_value = principal * (rate ** years)
print(future_value) # Output: 1157.625
Equality Comparison with ==
Checks if two values are equal. Returns Bool.
tag is_equal = 10 == 10
print(is_equal) # Output: true
tag str_eq = "hello" == "hello"
print(str_eq) # Output: true
# Case-sensitive string comparison
tag case_eq = "Hello" == "hello"
print(case_eq) # Output: false
# Type mismatch (always false)
tag mismatch = 10 == "10"
print(mismatch) # Output: false
tag score = 85
if score == 100:
print("Perfect!")
elif score == 0:
print("Try again!")
Inequality Comparison with !=
Checks if two values are NOT equal. Returns Bool.
tag not_equal = 10 != 5
print(not_equal) # Output: true
# Input validation example
tag password = ask("Enter password: ")
if password != "secret123":
print("Access denied")
halt
print("Access granted")
Less Than with <
Checks if the left value is less than the right value. Returns Bool. Strings are compared
lexicographically.
tag less = 5 < 10
print(less) # Output: true
tag str_less = "apple" < "banana"
print(str_less) # Output: true (alphabetical order)
# Case matters — uppercase < lowercase in ASCII
tag case_less = "Apple" < "apple"
print(case_less) # Output: true
tag age = 16
if age < 18:
print("Underage") # Output: "Underage"
Greater Than with >
Checks if the left value is greater than the right value. Returns Bool.
tag greater = 15 > 10
print(greater) # Output: true
tag a = 30
tag b = 20
if a > b:
tag max_val = a
else:
tag max_val = b
print(max_val) # Output: 30
Less Than or Equal with <=
tag equal_case = 10 <= 10
print(equal_case) # Output: true
# Array bounds checking
tag index = 9
tag size = 10
if index <= size - 1:
print("Valid index") # Output: "Valid index"
Greater Than or Equal with >=
tag score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B") # Output: "Grade: B"
elif score >= 70:
print("Grade: C")
Logical AND with and
Performs logical conjunction. Returns Bool. Both operands must be boolean.
tag result = true and true
print(result) # Output: true
tag age = 25
if age >= 18 and age <= 65:
print("Working age") # Output: "Working age"
tag a = true
tag b = true
tag c = false
tag multi = a and b and c
print(multi) # Output: false
and. If the left operand is
false, the right operand is not evaluated.Logical OR with or
Performs logical disjunction. Returns Bool. Both operands must be boolean.
tag result = true or false
print(result) # Output: true
tag input = ask("Enter yes or no: ")
if input == "yes" or input == "no":
print("Valid input")
else:
print("Invalid input")
tag option = "C"
if option == "A" or option == "B" or option == "C":
print("Valid option") # Output: "Valid option"
or too. If the left operand is
true, the right operand is not evaluated.Logical NOT with not
Performs logical negation. Returns Bool. The operand must be boolean.
tag result = not true
print(result) # Output: false
tag logged_in = false
if not logged_in:
print("Please log in") # Output: "Please log in"
# Combined with AND/OR
tag a = true
tag b = false
tag combined = not (a and b)
print(combined) # Output: true
# Toggle logic
tag enabled = true
enabled = not enabled # Toggle to false
print(enabled) # Output: false
not has higher precedence than and and or.
Use parentheses when needed to make intent explicit.