Skip to content
Kavachकवच

Operators

Also known as: operators arithmetic plus minus multiply divide modulo remainder power exponent assignment && || ! not and or logical boolean

● Beginner6 minBefore this: VariablesSpec 1.8

Operators compute with values: + adds, == compares, and combines two conditions, ?? picks a default.

Every operator in Kavach, group by group: what it is called, what it does in plain words, and an example with its result. The results below were printed by the real compiler.

Arithmetic
OperatorNameWhat it doesExampleResult
+AdditionAdds two numbers7 + 29
-SubtractionTakes the second number from the first7 - 25
*MultiplicationMultiplies two numbers7 * 214
/DivisionDivides; two whole numbers give a whole number, the fraction cut off7 / 23
/DivisionWith floats, the exact result7.0 / 2.03.5
%RemainderWhat is left over after dividing7 % 21
**PowerRaises the first number to the power of the second2 ** 101024
-NegationTurns a number to its opposite sign-(7 + 2)-9
Comparison
OperatorNameWhat it doesExampleResult
==Equal toTrue when both sides are the same5 == 5true
!=Not equal toTrue when the sides differ5 != 3true
<Less thanTrue when the left is smaller3 < 5true
<=Less than or equal toTrue when the left is smaller or the same5 <= 5true
>Greater thanTrue when the left is bigger7 > 9false
>=Greater than or equal toTrue when the left is bigger or the same9 >= 9true
a < b < cChainBoth comparisons at once, as in mathematics1 < 5 < 10true
Chained comparisonsResults with a = 5, b = 5, c = 3, d = 7, e = 2, f = 9, g = 9.
ChainMeansResult
a == b != cThree values: a == b and b != ctrue
c < a <= bThree values: c < a and a <= btrue
a == b != c < dFour values: a == b and b != c and c < dtrue
a == b != c < d > eFive values: a == b and b != c and c < d and d > etrue
e < c < a == b < dAny order: e < c and c < a and a == b and b < dtrue
a != b == cOnly neighbours: a != b and b == c: a and c are never comparedfalse
a != c != bOnly neighbours: a != c and c != b: true, although a and b are equaltrue
e < c < a == b < d < f == gSeven values: e < c and c < a and a == b and b < d and d < f and f == gtrue
(a == b) != (c == d)Grouped: Each bracket is worked out first, to true or false; then the two answers are comparedtrue
(c < a) == (e < d)Grouped: true == true: both brackets give the same answertrue
(a < b) == (d > f) == (e > c)Grouped chain: Three answers, compared as a chain of their owntrue
a == b == c or d == e == dChains with or: True when either chain is true; here neither isfalse
a == b == 5 or d == e == dChains with or: The first chain is true, so the whole is truetrue
c < a == b and d > e < cChains with and: (c < a == b) and (d > e < c): both chains must be truetrue
not (a == b == c)A chain turned around: a == b == c is false, so not makes it truetrue
Logical
OperatorNameWhat it doesExampleResult
andAndTrue only when both sides are truetrue and falsefalse
orOrTrue when at least one side is truetrue or falsetrue
notNotTurns true into false, and false into truenot truefalse
AssignmentEach result is x afterwards, starting from 10.
OperatorNameWhat it doesExampleResult
=AssignGives the variable a new valuex = 44
+=Add and assignx += 3 is x = x + 3x += 313
-=Subtract and assignx -= 3 is x = x - 3x -= 37
*=Multiply and assignx *= 3 is x = x * 3x *= 330
/=Divide and assignx /= 3 is x = x / 3x /= 33
%=Remainder and assignx %= 3 is x = x % 3x %= 31
**=Power and assignx **= 3 is x = x ** 3x **= 31000
Optional values
OperatorNameWhat it doesExampleResult
??DefaultThe value if there is one, otherwise the one on the rightnickname ?? "friend"friend
?.Read throughReads a field when the value is there, and gives none when it isn'tfound?.nameAsha
RangeEach result is the numbers a for loop over it visits.
OperatorNameWhat it doesExampleResult
..RangeFrom the first number up to, but not including, the last0..3[0, 1, 2]
..=Range, end includedFrom the first number up to and including the last0..=3[0, 1, 2, 3]
Bits
OperatorNameWhat it doesExampleResult
&AndKeeps the bits set in both numbers12 & 108
|OrKeeps the bits set in either number12 | 1014
^Exclusive orKeeps the bits set in exactly one of the numbers12 ^ 106
~NotFlips every bit~0-1
<<Shift leftMoves the bits left: each step doubles the number1 << 416
>>Shift rightMoves the bits right: each step halves the number, keeping its sign32 >> 28

Comparisons chain with any mix of == != < <= > >= and any number of values: each part compares its two neighbours, every value is worked out once, and the chain stops at the first part that is false. Brackets group as usual: (a == b) is worked out first, to true or false, and that answer is then compared as one value. Whole chains join with and, or and not, with no brackets needed, since comparisons go first: a == b == c or d == e == d. There is no === or !==: == and != already compare contents, and types never mix.

Because a bracket gives a Bool, comparing it with a number is refused: (a < b) < c would ask whether true is less than 3.

fn main() {
    const a = 5
    const b = 5
    const c = 3
    print((a < b) < c)
}

The compiler refused the program:

kavach run group-chain.kav

error: expected `Bool`, found `Int`
group-chain.kav: Kavach code, then what running it prints

Logic is written in words, and, or, not, so a condition reads like a sentence. When operators mix, precedence decides which goes first; when in doubt, use brackets.

One page for each form