Basic Pascal Tutorial/Chapter 1/Assignment and Operations: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Basic Pascal Tutorial/Chapter 1/Assignment and Operations
| Old | New | Line | |
|---|---|---|---|
| 1 | + | 1E - Assignment and Operations (author: Tao Yue, state: unchanged) | |
| 2 | + | ||
| 3 | + | Once you have declared a variable, you can store values in it. This is called assignment. | |
| 4 | + | ||
| 5 | + | To assign a value to a variable, follow this syntax: | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | variable_name := expression; | |
| 9 | + | ``` | |
| 10 | + | ||
| 11 | + | Note that unlike other languages, whose assignment operator is just an equals sign, Pascal uses a colon followed by an equals sign, similarly to how it's done in most computer algebra systems. | |
| 12 | + | ||
| 13 | + | The expression can either be a single value: | |
| 14 | + | ||
| 15 | + | ``` pascal | |
| 16 | + | some_real := 385.385837; | |
| 17 | + | ``` | |
| 18 | + | ||
| 19 | + | or it can be an arithmetic sequence: | |
| 20 | + | ||
| 21 | + | ``` pascal | |
| 22 | + | some_real := 37573.5 * 37593 + 385.8 / 367.1; | |
| 23 | + | ``` | |
| 24 | + | ||
| 25 | + | The arithmetic operators in Pascal are: | |
| 26 | + | ||
| 27 | + | | Operator | Operation | Operands | Result | | |
| 28 | + | |----------|-------------------------------|-----------------|-----------------| | |
| 29 | + | | \+ | Addition or unary positive | real or integer | real or integer | | |
| 30 | + | | \- | Subtraction or unary negative | real or integer | real or integer | | |
| 31 | + | | * | Multiplication | real or integer | real or integer | | |
| 32 | + | | / | Real division | real or integer | real | | |
| 33 | + | | div | Integer division | integer | integer | | |
| 34 | + | | mod | Modulus (remainder division) | integer | integer | | |
| 35 | + | ||
| 36 | + | **`div`** and **`mod`** only work on *integers*. **`/`** works on both *reals* and *integers* but will always yield a *real* answer. The other operations work on both *reals* and *integers*. When mixing *integers* and *reals*, the result will always be a *real* since data loss would result otherwise. This is why Pascal uses two different operations for division and integer division. `7 / 2 = 3.5` (real), but `7 div 2 = 3` (and `7 mod 2 = 1` since that's the remainder). | |
| 37 | + | ||
| 38 | + | Each variable can only be assigned a value that is of the same data type. Thus, you cannot assign a real value to an integer variable. However, certain data types will convert to a higher data type. This is most often done when assigning integer values to real variables. Suppose you had this variable declaration section: | |
| 39 | + | ||
| 40 | + | ``` pascal | |
| 41 | + | var | |
| 42 | + | some_int : integer; | |
| 43 | + | some_real : real; | |
| 44 | + | ``` | |
| 45 | + | ||
| 46 | + | When the following block of statements executes: | |
| 47 | + | ||
| 48 | + | ``` pascal | |
| 49 | + | some_int := 375; | |
| 50 | + | some_real := some_int; | |
| 51 | + | ``` | |
| 52 | + | ||
| 53 | + | `some_real` will have a value of `375.0`. | |
| 54 | + | ||
| 55 | + | Changing one data type to another is referred to as typecasting. Modern Pascal compilers support explicit typecasting in the manner of C, with a slightly different syntax. However, typecasting is usually used in low-level situations and in connection with object-oriented programming, and a beginning programming student will not need to use it. Here is [information on typecasting from the GNU Pascal manual](http://www.gnu-pascal.de/gpc-es/Type-Casts.html). | |
| 56 | + | ||
| 57 | + | In Pascal, the minus sign can be used to make a value negative. The plus sign can also be used to make a value positive, but is typically left out since values default to positive. | |
| 58 | + | ||
| 59 | + | Do not attempt to use two operators side by side, like in: | |
| 60 | + | ||
| 61 | + | ``` pascal | |
| 62 | + | some_real := 37.5 * -2; | |
| 63 | + | ``` | |
| 64 | + | ||
| 65 | + | This may make perfect sense to you, since you're trying to multiply by negative-2. However, Pascal will be confused — it won't know whether to multiply or subtract. You can avoid this by using parentheses to clarify: | |
| 66 | + | ||
| 67 | + | ``` pascal | |
| 68 | + | some_real := 37.5 * (-2); | |
| 69 | + | ``` | |
| 70 | + | ||
| 71 | + | The computer follows an order of operations similar to the one that you follow when you do arithmetic. Multiplication and division (`* / div mod`) come before addition and subtraction (`+ -`), and parentheses always take precedence. So, for example, the value of: `3.5*(2+3)` will be `17.5.` | |
| 72 | + | ||
| 73 | + | Pascal cannot perform standard arithmetic operations on Booleans. There is a special set of Boolean operations. Also, you should not perform arithmetic operations on characters. | |
| 74 | + | ||
| 75 | + | --- | |
| 76 | + | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 1/Assignment and Operations", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
