Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operators

All Bop operators, grouped by category. No operator overloading — each operator works on specific types and produces an error on type mismatch.

Arithmetic

OperatorNameTypesNotes
+AddnumberAlso concatenates strings: "a" + "b""ab"
-SubtractnumberAlso unary negation: -x
*Multiplynumber
/DividenumberAlways produces float: 7 / 23.5
%Modulonumber
print(10 + 3)      // 13
print(10 - 3)      // 7
print(10 * 3)      // 30
print(10 / 3)      // 3.333...
print(10 % 3)      // 1

print("Hello" + " " + "world")    // "Hello world"

Comparison

OperatorNameReturns
==Equalbool
!=Not equalbool
<Less thanbool
>Greater thanbool
<=Less or equalbool
>=Greater or equalbool

== and != work on all types. Comparing different types is always false (no implicit coercion). <, >, <=, >= work on numbers and strings (lexicographic).

print(5 == 5)         // true
print(5 == "5")       // false (different types)
print("abc" < "def")  // true (lexicographic)

Boolean

OperatorNameNotes
&&AndShort-circuits
||OrShort-circuits
!NotUnary prefix

Short-circuiting means the second operand isn’t evaluated if the first determines the result:

// && stops at the first false
if x > 0 && x < 100 {
  print("In range")
}

// || stops at the first true
if name == "" || name == none {
  print("No name provided")
}

// ! inverts a boolean
if !found {
  print("Still searching...")
}

Assignment

OperatorEquivalent to
=Assign
+=x = x + ...
-=x = x - ...
*=x = x * ...
/=x = x / ...
%=x = x % ...
let score = 0
score += 10    // 10
score -= 3     // 7
score *= 2     // 14

Conditional expressions

Bop has no ternary operator (?:). Use if/else as an expression instead:

let label = if count > 3 { "lots" } else { "few" }

Both branches are required when if/else is used as an expression. The last expression in each branch is the value.

Precedence

From highest (evaluated first) to lowest:

PriorityOperators
1!, - (unary)
2*, /, %
3+, -
4<, >, <=, >=
5==, !=
6&&
7||
8=, +=, -=, *=, /=, %=

Parentheses override precedence:

let result = (1 + 2) * 3    // 9, not 7