Skip to content
Kavachकवच

Operators: Precedence

● Beginner3 min

* and / come before + and -, as in school. When in doubt, use parentheses.

fn main() {
    print(2 + 3 * 4)
    print((2 + 3) * 4)
    print(-2 ** 2)
    print(2 ** 3 ** 2)
}

The program ran and printed:

kavach run precedence.kav

1420-4512
precedence.kav: Kavach code, then what running it prints
  • 2 + 3 * 4 is 14: the multiplication goes first.
  • -2 ** 2 is -4, as in mathematics: the power goes before the minus.
  • 2 ** 3 ** 2 is 2 ** 9: powers go right to left.

kavach fmt never removes parentheses that help a reader.