05Operators
Grouping and order
Which operator goes first, how word phrases nest, and when to use parentheses.
In ၂ + ၃ * ၄, which comes first, the addition or the multiplication? As in school
maths, Zaka does the multiplication first, so the answer is 14. Parentheses change the
order: (၂ + ၃) * ၄ is 20.
Expected output
၁၄
၂၀
Order of operators
From first to last (tightest to loosest):
-,အနုတ်,~,!in front of a value*,/,%+,-<<,>>&^|<,>,<=,>===,!=- word phrases (
... ၏ ပေါင်းလဒ်,... ထက် ကြီး, and so on) မဟုတ်(not)ဖြစ်၍,&&(and)သို့မဟုတ်,||(or)
Operators on the same line go left to right: ၁၀ - ၃ - ၂ is (၁၀ - ၃) - ၂. The
Operator precedence page has the full table.
Word phrases take whole sums
A part of a word phrase can be a whole sum written with signs. In
က + ခ နှင့် ဂ ၏ မြှောက်လဒ်, the first part is က + ခ. It reads "the product of (k + kh) and g":
Expected output
၂၀
Word phrases inside word phrases
Word phrases can nest. Read from left to right. Each verb takes the parts just before it that hold the particles it needs, and its result joins the sentence:
Expected output
၁၄
၂၀
In the first line, မြှောက်လဒ် takes ခ နှင့် and ဂ ၏, the two parts just before it.
A ၏ follows its result, so ပေါင်းလဒ် then takes က နှင့် and that result.
Parentheses move a phrase into a sum
A word phrase cannot sit straight inside a sum written with signs. To use one there, put it in parentheses:
Expected error
error[ZK0203]: this statement does not end in a statement verb, found "*"
Expected output
၁၀
Parentheses also wrap a logical expression used as a value, as in
(က ဖြစ်၍ ခ) ကို ပြသပါ။
Needless parentheses
Parentheses around a whole printed or stored value change nothing. Zaka warns about them, much as an editor flags a double space:
Expected warning
warning[ZK0507]: these parentheses are not needed
Hands on
- Write
(၂ + ၃) * ၄using words alone. - Print
၁၀ - ၃ - ၂and၁၀ - (၃ - ၂). Do the two match?
Next: Control flow.