2. Values and names¶
Every program works with values: numbers, text and more. To use a value more than once, you give it a name.
ثابت: a name for a value that doesn't change¶
ثابت means «constant». After ثابت المدينة = "القاهرة" («the city is Cairo»), the name
المدينة means that value in the rest of the program. Names can be written in Arabic or
English, and can contain _, as in عدد_الطلاب («number of students»).
متغير: a name for a value that changes¶
متغير means «variable». النقاط += 5 is short for النقاط = النقاط + 5, and so are
-=, *= and /=.
Why two different words? Most values in a program never change, and when you say ثابت,
Ḍād protects you from changing one by mistake. Try it:
The message says that «الاسم» can't change because it is a constant, and suggests declaring
it with «متغير» if you want to change it. Use ثابت by default, and switch to متغير when you
really need to change the value.
Kinds of values¶
Every value has a type. These are the main ones:
| Type | What it is | Examples |
|---|---|---|
صحيح |
a whole number | 0, 42, -7 |
عشري |
a number with a fraction | 3.14, 2.0 |
نص |
text | "مرحبا", "" |
منطقي |
true or false | صح (true), خطأ (false) |
Arithmetic¶
- Division
/always gives a decimal, as in maths:7 / 2is3.5. %is the remainder:7 % 2is1.ك(«as») converts a value to another type:(7 / 2) ك صحيحis3.- A decimal always prints with a decimal point, as in
5.0, so you can tell its type.
Text¶
Put a name between braces {…} inside text, and its value appears there:
ثابت الاسم = "ليلى"
ثابت العمر = 11
اطبع("{الاسم} عمرها {العمر} سنة، وبعد سنة يصير {العمر + 1}")
اطبع(الاسم.طول، الاسم + " وأختها")
الاسم.طول is the number of letters: طول means «length». The dot . asks a value for
something it has, «the length of the name», and you'll see it a lot.
Converting between types¶
The text "12" isn't a number, even though it looks like one. Convert it with ك:
True and false¶
== asks «are they equal?», and != asks «are they different?». A single = gives a name
its value: ثابت س = 1. و, أو and ليس mean «and», «or» and «not».
Exercise¶
- Write a program with two constants,
الطولandالعرض(length and width), for a room 4.5 metres long and 3 metres wide. Print its area like this:مساحة الغرفة 13.5 متراً مربعاً. - You have 50 riyals, and a book costs 12. Print how many books you can buy, and how much is left.
Solution
Next: Decisions and loops