Equal: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | 1 | = | |
| 4 | - | ||
| 5 | - | ||
| 6 | 2 | ||
| 7 | 3 | The symbol `=` (pronounced “equal”) is used to | |
| 8 | 4 | ||
| 9 | 5 | - compare two (comparable) values for equality, | |
| 10 | 6 | - [[Constant|constant]] declarations (including [[Resourcestring|resourcestring sections]]), and | |
| 11 | 7 | - [[Type|type declarations]], as well as | |
| 12 | 8 | - specifying [[Default parameter|default values]] for formal parameters in method declarations. | |
| 13 | 9 | ||
| 14 | 10 | Furthermore, if [[Enum Type|enumeration data types]] are defined with explicit indices, [[FPC]]’s Delphi compatibility modes require an equal sign instead of [[Becomes|:=]] when specifying the indices. | |
| 15 | 11 | ||
| 16 | 12 | ## application | |
| 17 | 13 | ||
| 18 | 14 | ``` pascal | |
| 19 | 15 | program equalDemo(input, output, stderr); | |
| 20 | 16 | ||
| 21 | 17 | const | |
| 22 | 18 | answer = 42; | |
| 23 | 19 | ||
| 24 | 20 | resourcestring | |
| 25 | 21 | prompt = 'What''s the answer?'; | |
| 26 | 22 | ||
| 27 | 23 | type | |
| 28 | 24 | number = longint; | |
| 29 | 25 | ||
| 30 | 26 | var | |
| 31 | 27 | i: number; | |
| 32 | 28 | ||
| 33 | 29 | begin | |
| 34 | 30 | writeLn(prompt); | |
| 35 | 31 | readLn(i); | |
| 36 | 32 | ||
| 37 | 33 | if not (i = answer) then | |
| 38 | 34 | begin | |
| 39 | 35 | exitCode := 1; | |
| 40 | 36 | end; | |
| 41 | 37 | end. | |
| 42 | 38 | ``` | |
| 43 | 39 | ||
| 44 | 40 | Be aware of *what* you compare: When you compare two references, i.e. [[Class|class]] or [[Pointer|pointer]] variables, without dereferencing them first, you usually compare two memory addresses, *not* the actual content at those addresses. Operator overloading may alter this behavior, though. For instance the content of [[Ansistring|ansistrings]] can be compared without any special treatment, though *internally* (transparently) they are realized as pointers. | |
| 45 | 41 | ||
| 46 | 42 | Note, you usually do not use the equal sign comparison in conjunction with [[IEEE 754 formats|floating-point numbers]]. Instead one uses [`math.compareValue`](https://www.freepascal.org/docs-html/rtl/math/comparevalue.html). | |
| 47 | 43 | ||
| 48 | 44 | ## comparitive remarks | |
| 49 | 45 | ||
| 50 | 46 | Unlike other programming languages, the symbol is *not* used to assign a value, for that the character pair [[Becomes|:=]] is used. An exception of this are “initialized variables”, where you specify an initial value inside the [[Var|var section]] alongside your [[Variable|variable]] declarations: | |
| 51 | 47 | ||
| 52 | 48 | ``` pascal | |
| 53 | 49 | program initializedVariable(input, output, stderr); | |
| 54 | 50 | ||
| 55 | 51 | var | |
| 56 | 52 | i: longint; | |
| 57 | 53 | response: string = 'Wrong!'; | |
| 58 | 54 | ||
| 59 | 55 | begin | |
| 60 | 56 | writeLn('What''s the answer?'); | |
| 61 | 57 | readLn(i); | |
| 62 | 58 | ||
| 63 | 59 | if i = 42 then | |
| 64 | 60 | begin | |
| 65 | 61 | response := 'Right!'; | |
| 66 | 62 | end; | |
| 67 | 63 | ||
| 68 | 64 | writeLn(response); | |
| 69 | 65 | end. | |
| 70 | 66 | ``` | |
| 71 | 67 | ||
| 72 | 68 | Also, a comparison *always* results in a [[Boolean|boolean value]]. | |
| 73 | 69 | ||
| 74 | 70 | ## ASCII value | |
| 75 | 71 | ||
| 76 | 72 | In [[ASCII]], the character code decimal `61` (or [[Hexadecimal|hexadecimal]] `3D`) is defined to be `=` (equals sign). | |
| 77 | 73 | ||
| 78 | 74 | ## see also | |
| 79 | 75 | ||
| 80 | 76 | - [[Becomes|becomes-operator :=]] assigns values to a variable | |
| 81 | 77 | - [[Not equal|not-equal-operator <>]] checks for inequality | |
| 82 | 78 | ||
| 83 | 79 | --- | |
| 84 | 80 | *From the Free Pascal wiki, page "Equal", as it stood on 2020-11-10 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).* |
