စံကား

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

၁၈
၂၄
၂၅
၃၅
ZakaReads asPart
အသက်"age"the variable to change
ကို(object)
အသက် + ၁age + 1the 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:

  1. Work out the new value, အသက် + ၁, using the current value of အသက် (24).
  2. 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.