## Overview

In programming, you need to perform operations such as arithmetic and logical operations on specific variables. Cava provides a series of operators to meet the preceding needs. The operators are classified into the following types based on different operation scenarios:

Arithmetic operators

Relational operators

Bitwise operators

Logical operators

Assignment operators

## Arithmetic operators

Arithmetic operators are used for mathematical calculation. Their meanings in Cava are the same as those in mathematics. The following table describes all arithmetic operators supported by Cava.

Arithmetic operator | Description | Example (int left = 20, right = 10) |
---|---|---|

+ | Addition: adds the operands on both sides of the operator. | int a = left + right: a is 30. |

- | Subtraction: subtracts the operand on the right side of the operator from the operand on the left side. | int a = left - right: a is 10. |

* | Multiplication: multiplies the operand on one side of the operator by the operand on the other side. | int a = left * right: a is 200. |

/ | Division: divides the operand on the left side of the operator by the operand on the right side. | int a = left / right: a is 2. |

% | Modulo: returns the remainder of the operand on the left side of the operator divided by the operand on the right side. | int a = left % right: a is 0. |

++ | Increment: adds 1 to an operand. | int a = left++: a is 20, and left is 21. |

-- | Decrement: subtracts 1 from an operand. | int a = left--: a is 20, and left is 19. |

**Note:** The variable on the right side of the division operator cannot be 0. Otherwise, a division-by-zero error is triggered. int b = a++ means to assign a to b, and then add 1 to a. int b = ++a means to add 1 to a, and then assign a to b.

## Relational operators

The result of the relational operation in Cava is of the BOOLEAN type. Relational operators are used in if statements. The following table describes the relational operators supported by Cava.

Relational operator | Description | Example (int left = 20, right = 10) |
---|---|---|

> | Greater than: checks whether the operand on the left side of the operator is greater than the operand on the right side. If yes, the result is true. Otherwise, the result is false. | boolean a = left > right: a is true. |

< | Less than: checks whether the operand on the left side of the operator is less than the operand on the right side. If yes, the result is true. Otherwise, the result is false. | boolean a = left < right: a is false. |

>= | Greater than or equal to: checks whether the operand on the left side of the operator is greater than or equal to the operand on the right side. If yes, the result is true. Otherwise, the result is false. | boolean a = left >= right: a is true. |

<= | Less than or equal to: checks whether the operand on the left side of the operator is less than or equal to the operand on the right side. If yes, the result is true. Otherwise, the result is false. | boolean a = left <= right: a is false. |

== | Equal to: checks whether the operand on the left side of the operator is equal to the operand on the right side. If yes, the result is true. Otherwise, the result is false. | boolean a = left == right: a is false. |

!= | Not equal to: checks whether the operand on the left side of the operator is not equal to the operand on the right side. If yes, the result is true. Otherwise, the result is false. | boolean a = left != right: a is true. |

## Bitwise operators

Values are expressed in binary in computers. Cava provides a series of bitwise operators and supports bitwise operations on variables of data types such as INT, LONG, SHORT, CHAR, and BYTE. The following table describes the bitwise operators supported by Cava.

Bitwise operator | Description | Example (int left = 1, right = 2) |
---|---|---|

& | Bitwise AND: returns 1 in each bit position when both the bits of two operands in the same bit position are 1. | int a = left & right: a is 0. |

| | Bitwise OR: returns 1 in each bit position when one or both of the bits of two operands in the same bit position are 1. | int a = left | right: a is 3. |

^ | Bitwise XOR: returns 1 in each bit position when the bits of two operands in the same bit position are different. | int a = left ^ right: a is 3. |

~ | Bitwise NOT: inverts the bits of an operand. | int a = ~left: a is -2. |

<< | Left shift: shifts an operand left a specified number of bits. | int a = left << 2: a is 4. |

>> | Right shift: shifts an operand right a specified number of bits. | int a = right >> 1: a is 1. |

## Logical operators

Logical operators in Cava are used to connect variables of the BOOLEAN type to implement more complex logic. The following table describes the logical operators supported by Cava.

Logical operator | Description | Example (boolean left = true, right = false) |
---|---|---|

&& | Logical AND: returns true when the operands on both sides of the operator are true. | boolean a = left && right: a is false. |

|| | Logical OR: returns true when one or both of the operands on both sides of the operator are true. | boolean a = left || right: a is true. |

! | Logical NOT: returns false if an operand is true or returns true if the operand is false. | boolean a = !left: a is false. |

## Assignment operators

An assignment operator is used to assign a variable to another variable. The following table describes the assignment operators supported by Cava.

Assignment operator | Description | Example (int left = 1, right = 2) |
---|---|---|

= | Simple assignment: assigns the value of the operand on the right side of the operator to the operand on the left side. | left = right: left is 2. |

+= | Addition assignment: adds the operands on both sides of the operator and assigns the sum to the operand on the left side. | left += right: left is 3. |

-= | Subtraction assignment: subtracts the operand on the right side of the operator from the operand on the left side and assigns the difference to the operand on the left side. | left -= right: left is -1. |

*= | Multiplication assignment: multiplies the operand on the left side of the operator by the operand on the right side and assigns the product to the operand on the left side. | left *= right: left is 2. |

/= | Division assignment: divides the operand on the left side of the operator by the operand on the right side and assigns the quotient to the operand on the left side. | left /= right: left is 0. |

%= | Modulus assignment: calculates the remainder of the operand on the left side of the operator divided by the operand on the right side and assigns the remainder to the operand on the left side. | left %= right: left is 1. |

&= | Bitwise AND assignment: performs a bitwise AND operation on the operands on both sides of the operator and assigns the result to the operand on the left side. | left &= right: left is 0. |

|= | Bitwise OR assignment: performs a bitwise OR operation on the operands on both sides of the operator and assigns the result to the operand on the left side. | left |= right: left is 3. |

^= | Bitwise XOR assignment: performs a bitwise XOR operation on the operands on both sides of the operator and assigns the result to the operand on the left side. | left ^= right: left is 3. |

<<= | Left shift assignment: shifts the operand on the left side of the operator left the number of bits specified by the operand on the right side and assigns the result to the operand on the left side. | left <<= right: left is 4. |

>>= | Right shift assignment: shifts the operand on the left side of the operator right the number of bits specified by the operand on the right side and assigns the result to the operand on the left side. | left >>= right: left is 0. |

## Conditional operator (? :)

Cava does not support the conditional operator. You can use if statements as an alternative.

## Operator precedence

You may use multiple operators together when you write code. The following table describes the precedence of the operators supported by Cava.

Precedence | Operator | Associativity |
---|---|---|

1 | () [] . | Left-to-right |

2 | ! + (unary plus) - (unary negation) ~ ++ -- | Right-to-left |

3 | * / % | Left-to-right |

4 | + (addition) -(subtraction) | Left-to-right |

5 | << >> | Left-to-right |

6 | < <= > >= | Left-to-right |

7 | == !==. | Left-to-right |

8 | & | Left-to-right |

9 | ^ | Left-to-right |

10 | | | Left-to-right |

11 | && | Left-to-right |

12 | || | Left-to-right |

13 | = += -= *= /= %= &= |= ^= ~= <<= >>= | Right-to-left |

The preceding table describes the precedence of the operators from highest to lowest. 1 indicates the highest precedence, and 14 indicates the lowest precedence. However, when you write code, the precedence of each operator is difficult to be predicted. Therefore, the best way is to specify the operation order of each operator by using parentheses (), as shown in the following code:

```
int a = 1;
int b = 2;
int c = a * (b + 2); // Use parentheses () to specify that b + 2 takes precedence.
```