Becomes: what changed
Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | := | |
| 2 | 2 | ||
| 3 | 3 | The character pair `:=`, that are a [[Colon|colon]] and an [[Equal|equal sign]] back to back, is pronounced as “becomes” and used by [[Pascal]] as the assignment operator. | |
| 4 | 4 | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | 5 | ## assignment | |
| 14 | 6 | ||
| 15 | 7 | For valid assignments `:=` is surrounded by a _single_ [[Variable|variable]] [[Identifier|identifier]] on the left hand (possibly doing a [[Typecast|variable typecast]]), and an [[expression]] evaluating to the [[Data type|data type]] the variable is declared as on the right hand. | |
| 16 | 8 | ``` | |
| 17 | 9 | program assignmentDemo(input, output, stderr); | |
| 18 | 10 | ||
| 19 | 11 | const | |
| 20 | 12 | diameter = 6; | |
| 21 | 13 | ||
| 22 | 14 | var | |
| 23 | 15 | n: integer; | |
| 24 | 16 | area: real; | |
| 25 | 17 | givenName: string; | |
| 26 | 18 | ||
| 27 | 19 | begin | |
| 28 | 20 | n := 42; | |
| 29 | 21 | area := pi() * diameter; | |
| 30 | 22 | givenName := 'Smith'; | |
| 31 | 23 | n := 1000 - n div 2; | |
| 32 | 24 | end. | |
| 33 | 25 | ``` | |
| 34 | 26 | ||
| 35 | - | ||
| 27 | + | Assignments to variables of a [[subrange types|subrange type]] should be handled with care. The [[Compiler|compiler]] can only yield out-of-range errors for _[[Constant|constant]]_ expressions, i. e. not depending on any [[runtime|run-time]] data. With [[sRangechecks|`{$rangeChecks}`]] enabled a [[runtime error|run-time error]] can be generated. | |
| 36 | 28 | ``` | |
| 37 | 29 | program assignmentRange(input, output, stderr); | |
| 38 | 30 | ||
| 39 | 31 | type | |
| 40 | 32 | naturalNumber = 1..high(longword); | |
| 41 | 33 | ||
| 42 | 34 | var | |
| 43 | 35 | n: naturalNumber; | |
| 44 | 36 | ||
| 45 | 37 | begin | |
| 46 | 38 | {$rangechecks on} | |
| 47 | 39 | n := 1; // is OK | |
| 48 | 40 | n := -42 + n; // will cause RTE 201 | |
| 49 | 41 | end. | |
| 50 | 42 | ``` | |
| 51 | 43 | ||
| 52 | 44 | ## handling of special data types | |
| 53 | 45 | ||
| 54 | 46 | Where simple data types like [[Integer|`integer`s]] and [[Char|`char`acters]] are realized as `mov` instructions or alike, data types that require initialization and finalization such as [[Ansistring|`ansistring`s]] or [[Class|`class`es]] need special care. | |
| 55 | 47 | ||
| 56 | 48 | The compiler will generate appropriate code _copying_ data for following data types. | |
| 57 | 49 | ||
| 58 | 50 | * [[String|`string`]] | |
| 59 | 51 | * [[Object|`object`]] | |
| 60 | 52 | * [[Record|`record`]] | |
| 61 | 53 | ||
| 62 | 54 | You don’t have to iterate over all components copying each element by hand as it is required in other programming languages. | |
| 63 | 55 | ||
| 64 | 56 | ## syntax justification | |
| 65 | 57 | ||
| 66 | 58 | The rationale of using two characters for assignment instead of just one, say the `=` sign, is to distinguish between assigning values and comparing for equality. It’s got its roots in the field of mathematics where a single equal sign is read as an expression, but has no imperative connotation. | |
| 67 | 59 | ||
| 68 | 60 | In comparison other languages than Pascal allow to write | |
| 69 | 61 | ``` | |
| 70 | 62 | n = m = x; | |
| 71 | 63 | ``` | |
| 72 | 64 | ||
| 73 | 65 | The semantics of this line of code varies among every programming language. For instance, in [[Fortran]] and in Basic this line means “Compare the values `m` and `x`, and if they are equal to each other `n` becomes `true`, `false` otherwise.” In contrast to that the C programming language will assign `m` the value of `x`, and subsequently assign the `n` the value of `m`, so `n` and `m` both have the value `x`. This sort of code has been a common source of errors, compilers nowadays even emit warnings when encountering multiple assignment in a single line. | |
| 74 | 66 | ||
| 75 | 67 | In Pascal the code excerpt above is illegal. Non-productive statements are not allowed, i. e. _something_ has to be _done_. | |
| 76 | 68 | ||
| 77 | 69 | ## index specification | |
| 78 | 70 | ||
| 79 | 71 | In declarations of enumerated data types, `:=` can be used to explicitly specify the ordinal value of a member. See [[Enum Type|enumeration types § “Indices”]] for details. | |
| 80 | 72 | ||
| 81 | 73 | ## see also | |
| 82 | 74 | ||
| 83 | 75 | * [[Single assignment|single assignment]], a compiler optimization | |
| 84 | 76 | ||
| 85 | 77 | ||
| 86 | 78 | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 79 | + | **navigation bar: topic: Pascal symbols** | |
| 80 | + | ||
| 81 | + | _single characters:_ [[Plus|`+` (plus)]] • [[Minus|`-` (minus)]] • [[*|`*` (asterisk)]] • [[Slash|`/` (slash)]] • [[Equal|`=` (equal)]] • [[Greater than|`>` (greater than)]] • [[Less than|`<` (less than)]] • [[period|`.` (period)]] • [[Colon|`:` (colon)]] • [[;|`;` (semi colon)]] • [[^|`^` (hat)]] • [[@|`@` (at)]] • [[Dollar sign|`$` (dollar sign)]] • [[&|`&` (ampersand)]] • [[Hash|`#` (hash)]] • [['|`'` (single quote)]] | |
| 82 | + | ||
| 83 | + | _character pairs:_ [[Not equal|`<>` (not equal)]] • [[Less than or equal|`<=` (less than or equal)]] • `:=` (becomes) • [[Greater than or equal|`>=` (greater than or equal)]] • [[symmetric difference|`><` (symmetric difference)]] • [[Slash|`//` (double slash)]] | |
| 98 | 84 | ||
| 99 | 85 | --- | |
| 100 | 86 | *From the Free Pascal wiki, page "Becomes", as the Internet Archive kept it on 2021-06-06, last changed on 2021-02-04. 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).* |
