Cava operators

更新时间:
复制 MD 格式

Cava supports five categories of operators for arithmetic, comparison, bitwise, logical, and assignment operations. This page describes each operator's behavior, supported types, and precedence rules.

Cava does not support the conditional (ternary) operator (?:). Use if statements instead.

Arithmetic operators

All arithmetic operators work with numeric types. Example variables: int left = 20, right = 10.

OperatorNameDescriptionExampleResult
+AdditionAdds both operandsleft + right30
-SubtractionSubtracts the right operand from the leftleft - right10
*MultiplicationMultiplies both operandsleft * right200
/DivisionDivides the left operand by the rightleft / right2
%ModuloReturns the remainder of the divisionleft % right0
++IncrementAdds 1 to the operandleft++left becomes 21
--DecrementSubtracts 1 from the operandleft--left becomes 19
Note: The right operand of / cannot be 0. A division-by-zero error is triggered if it is.

Post-increment vs. pre-increment: The position of ++ (and --) controls when the increment happens relative to the assignment. int b = a++ assigns a to b first, then increments a. int b = ++a increments a first, then assigns a to b.

Relational operators

Relational operators compare two operands and return a boolean result. Use them in if conditions. Example variables: int left = 20, right = 10.

OperatorNameDescriptionExampleResult
>Greater thantrue if left > rightleft > righttrue
<Less thantrue if left < rightleft < rightfalse
>=Greater than or equal totrue if left >= rightleft >= righttrue
<=Less than or equal totrue if left <= rightleft <= rightfalse
==Equal totrue if both operands are equalleft == rightfalse
!=Not equal totrue if operands differleft != righttrue

Bitwise operators

Bitwise operators work at the binary level and are supported on INT, LONG, SHORT, CHAR, and BYTE types. Example variables: int left = 1 (binary: 01), right = 2 (binary: 10).

OperatorNameDescriptionExampleResult
&Bitwise ANDReturns 1 for each bit position where both operands have 1left & right (01 & 10)0 (00)
|Bitwise ORReturns 1 for each bit position where either operand has 1left | right (01 | 10)3 (11)
^Bitwise XORReturns 1 for each bit position where the operands differleft ^ right (01 ^ 10)3 (11)
~Bitwise NOTInverts all bits of the operand~left (~01)-2
<<Left shiftShifts bits left by the specified number of positionsleft << 2 (01 << 2)4 (0100)
>>Right shiftShifts bits right by the specified number of positionsright >> 1 (10 >> 1)1 (01)

Logical operators

Logical operators combine boolean expressions. Example variables: boolean left = true, right = false.

OperatorNameDescriptionExampleResult
&&Logical ANDtrue if both operands are trueleft && rightfalse
||Logical ORtrue if at least one operand is trueleft || righttrue
!Logical NOTInverts the boolean value!leftfalse

Assignment operators

Assignment operators assign a value to a variable. Compound assignment operators combine an operation with assignment. Example variables: int left = 1, right = 2.

OperatorNameMeaningExampleleft after
=Assignmentleft = rightleft = right2
+=Addition assignmentleft = left + rightleft += right3
-=Subtraction assignmentleft = left - rightleft -= right-1
*=Multiplication assignmentleft = left * rightleft *= right2
/=Division assignmentleft = left / rightleft /= right0
%=Modulus assignmentleft = left % rightleft %= right1
&=Bitwise AND assignmentleft = left & rightleft &= right0
|=Bitwise OR assignmentleft = left | rightleft |= right3
^=Bitwise XOR assignmentleft = left ^ rightleft ^= right3
<<=Left shift assignmentleft = left << rightleft <<= right4
>>=Right shift assignmentleft = left >> rightleft >>= right0

Operator precedence

When an expression contains multiple operators, Cava evaluates them in the following order (highest to lowest precedence).

PrecedenceNameOperatorAssociativity
1 (highest)Grouping, member access() [] .Left-to-right
2Unary, increment, decrement! + (unary) - (unary) ~ ++ --Right-to-left
3Multiplicative* / %Left-to-right
4Additive+ -Left-to-right
5Shift<< >>Left-to-right
6Relational< <= > >=Left-to-right
7Equality== !=Left-to-right
8Bitwise AND&Left-to-right
9Bitwise XOR^Left-to-right
10Bitwise OR|Left-to-right
11Logical AND&&Left-to-right
12Logical OR||Left-to-right
13 (lowest)Assignment= += -= *= /= %= &= |= ^= ~= <<= >>=Right-to-left

Precedence rules can be non-obvious in complex expressions. Use parentheses () to make the evaluation order explicit:

int a = 1;
int b = 2;
int c = a * (b + 2); // b + 2 is evaluated first, then multiplied by a