စံကား

04Variables

Constants

Values that never change, declared with ကိန်းသေ.

Some values never change while a program runs: the number π, the length of a week, the pass mark of a test. Declare these as constants with ကိန်းသေ ("fixed number") instead of ကိန်းရှင် ("living number", a variable).

စံကား

Expected output

၃.၁၄၁၅၉
၁၄
ZakaReads asMakes
ကိန်းရှင်"living number"a variable: its value can change
ကိန်းသေ"fixed number"a constant: its value never changes

Constants make a program simpler to read (တစ်ပတ် is clearer than ၇) and simpler to change: fix the value in one place and each use follows.

Rules for constants

A constant must be given its value when it is declared:

စံကားZK0308

Expected error

error[ZK0308]: constant "တစ်ပတ်" needs an initial value

After that, it can never be given a new value. The next page shows how to change a variable; attempting the same on a constant fails:

စံကားZK0306

Expected error

error[ZK0306]: "တစ်ပတ်" is a constant and cannot be assigned

Hands on

  • Declare a constant for the number of minutes in an hour, and use it to print the number of minutes in 24 hours.

Next: Changing a variable.