05Operators
Logic
Combine true/false answers with and, or, and not.
Real questions often have parts: "Is it raining and have I no umbrella?" The logical operators join or flip booleans.
Expected output
မမှန်
မှန်
မှန်
မမှန်
မှန်
မှန်
| Sign | Words | Reads as | True when |
|---|---|---|---|
a && b | a ဖြစ်၍ b | "a, and b" | both are true |
a || b | a သို့မဟုတ် b | "a or b" | at least one is true |
!a | a မဟုတ် | "not a" | a is false |
Notice that မဟုတ် ("not") comes after what it negates, as it does in Burmese.
Parentheses when printing
A logical expression must be wrapped in parentheses before ကို or ဟု. Without them,
Zaka cannot tell where the expression ends and the print statement begins:
Expected error
error[ZK0204]: statement verb "ပြသပါ" cannot stand inside an expression
Joining comparisons
The most common use is to join comparisons. Is the age between 18 and 60?
Expected output
မှန်
မှန်
မမှန်
မှန်
Which goes first
"Not" binds tightest, then "and", then "or", just as × goes before + in maths.
က သို့မဟုတ် ခ ဖြစ်၍ ဂ means က သို့မဟုတ် (ခ ဖြစ်၍ ဂ). Add parentheses when a
different grouping is meant, or when it helps the reader.
Skipping the right side
ဖြစ်၍ and သို့မဟုတ် stop as soon as the answer is known. If the left side of
"and" is false, the whole thing is false, so the right side is never worked out. If the
left side of "or" is true, the right side is skipped. This is called short-circuit
evaluation. It matters once the right side does work, such as calling a
function.
Hands on
- Print whether both 3 and 5 divide 15 (
%gives၀when the division is clean). - Print whether a character
'ခ'is between'က'and'င'.
Next: Bitwise operators.