04Variables
Changing a variable
Give a variable a new value with ... ကို ... ဟု ထားပါ။
A variable can be given a new value later on. This is called assignment. In Zaka the
sentence reads "set x to v": x ကို v ဟု ထားပါ။
စံကား
Expected output
၁၈
၂၄
၂၅
၃၅
| Zaka | Reads as | Part |
|---|---|---|
အသက် | "age" | the variable to change |
ကို | (object) | |
အသက် + ၁ | age + 1 | the new value |
ဟု | "so", "as" | marks the new value |
ထားပါ | "please put", "please set" | the verb: assign |
How အသက် ကို အသက် + ၁ ဟု ထားပါ။ works
To a maths student, "age = age + 1" looks impossible. To a computer it is two steps:
- Work out the new value,
အသက် + ၁, using the current value ofအသက်(24). - Put the result (25) into
အသက်, replacing the old value.
Storing a true/false answer
A sentence that is true or false can be stored too:
စံကား
Expected output
မှန်
The new value must fit
The new value must suit the variable's kind, just as the initial value did:
စံကားZK0401
Expected error
error[ZK0401]: expected isize, found f64
Hands on
- Start a variable at
၁and double it four times, printing it after each change. - Make a variable
ကျန်(remaining) of၁၀၀, then subtract၃၅from it.
Next: Names.