2. Variables and arithmetic
To store a value under a name and calculate with it, and to tell integer division from fractional division.
Explanation
Section titled “Explanation”- A variable is a name you give to a value:
الطول = 7. - Every value has a type:
7is an integer of typeعدد64(64-bit integer),3.5is 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 / 2is3. - Meem does not mix an integer and a float without a conversion, so it never hides which one you meant: to get
3.5from the variableالطول, convert it first,كسر64(الطول) / 2. A number written directly takes the type it needs, so7 / 2.0gives3.5.
Example
Section titled “Example”الطول = 7العرض = 2اطبع(الطول * العرض, " ", الطول / العرض, " ", كسر64(الطول) / 2)Common mistakes
Section titled “Common mistakes”Changing a variable’s type after its first assignment:
س = 1س = "واحد"Exit code: 2
In the error catalog: E0408
س = 1وصف = "واحد"اطبع(س, " ", وصف)Other mistakes:
- Dividing an integer variable by a float without a conversion:
الطول / 2.0is error E0458 «لا نوع يسعهما» (no type holds both); writeكسر64(الطول) / 2. - Naming a variable after a builtin function, because that name is reserved:
طول = 7is syntax error E0474 «‘طول’ اسم دالة مدمجة ولا يكون اسم متغير أو معامل» (‘طول’ is a builtin function name and cannot be a variable or parameter name).
Exercise
Section titled “Exercise”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
الطول = 7العرض = 3اطبع("المساحة: ", الطول * العرض)اطبع("المحيط: ", 2 * (الطول + العرض))اطبع("7 / 2 = ", 7 / 2, " و7 / 2.0 = ", 7 / 2.0)