Skip to content
العربية

2. Variables and arithmetic

To store a value under a name and calculate with it, and to tell integer division from fractional division.

  • A variable is a name you give to a value: الطول = 7.
  • Every value has a type: 7 is an integer of type عدد64 (64-bit integer), 3.5 is a float of type كسر64 (64-bit float), and "ميم" is text of type نص (string). The number in the type name is its size in bits. You do not write the type yet: Meem knows it from the value.
  • A variable’s type is set by the first value you give it, or you write it after the name: س عدد64 = 3.
  • Division / between two integers gives an integer: it drops what comes after the decimal point (rounding toward zero): 7 / 2 is 3.
  • Meem does not mix an integer and a float without a conversion, so it never hides which one you meant: to get 3.5 from the variable الطول, convert it first, كسر64(الطول) / 2. A number written directly takes the type it needs, so 7 / 2.0 gives 3.5.
Example
الطول = 7
العرض = 2
اطبع(الطول * العرض, " ", الطول / العرض, " ", كسر64(الطول) / 2)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
14 3 3.5

Try this example in the playground

Changing a variable’s type after its first assignment:

Causes the error
س = 1
س = "واحد"
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
​[خطأ دلالي E0408] سطر 2:1 - لا يمكن تعيين قيمة من النوع 'نص' للمتغير 'س' من النوع 'عدد64'   2 | س = "واحد"     | ^  تلميح: النصوص لا تتحول إلى أعداد تلقائياً.

Exit code: 2

In the error catalog: E0408

Try this example in the playground

Fixes it
س = 1
وصف = "واحد"
اطبع(س, " ", وصف)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
1 واحد

Try this example in the playground

Other mistakes:

  • Dividing an integer variable by a float without a conversion: الطول / 2.0 is error E0458 «لا نوع يسعهما» (no type holds both); write كسر64(الطول) / 2.
  • Naming a variable after a builtin function, because that name is reserved: طول = 7 is syntax error E0474 «‘طول’ اسم دالة مدمجة ولا يكون اسم متغير أو معامل» (‘طول’ is a builtin function name and cannot be a variable or parameter name).

A rectangle is 7 long and 3 wide: print its area and perimeter, then the results of 7 / 2 and 7 / 2.0, and notice the difference.

Show the solution
Solution
الطول = 7
العرض = 3
اطبع("المساحة: ", الطول * العرض)
اطبع("المحيط: ", 2 * (الطول + العرض))
اطبع("7 / 2 = ", 7 / 2, " و7 / 2.0 = ", 7 / 2.0)
Output recorded from the compilermeem 02_حل.م · meem 0.17.3+434e13f
المساحة: 21المحيط: 207 / 2 = 3 و7 / 2.0 = 3.5

Types · Expressions