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
2 + 3 * 4is 14: the multiplication goes first.-2 ** 2is-4, as in mathematics: the power goes before the minus.2 ** 3 ** 2is2 ** 9: powers go right to left.
kavach fmt never removes parentheses that help a reader.