Equal: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Equal
| Old | New | Line | |
|---|---|---|---|
| 1 | + | <div style="float:right; margin: 0 10px 10px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;"> | |
| 2 | + | ||
| 3 | + | = | |
| 4 | + | ||
| 5 | + | </div> | |
| 6 | + | ||
| 7 | + | The symbol `=` (pronounced “equal”) is used to | |
| 8 | + | ||
| 9 | + | - compare two (comparable) values for equality, | |
| 10 | + | - [[Constant|constant]] declarations (including [[Resourcestring|resourcestring sections]]), and | |
| 11 | + | - [[Type|type declarations]], as well as | |
| 12 | + | - specifying [[Default parameter|default values]] for formal parameters in method declarations. | |
| 13 | + | ||
| 14 | + | 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 | + | ||
| 16 | + | ## application | |
| 17 | + | ||
| 18 | + | ``` pascal | |
| 19 | + | program equalDemo(input, output, stderr); | |
| 20 | + | ||
| 21 | + | const | |
| 22 | + | answer = 42; | |
| 23 | + | ||
| 24 | + | resourcestring | |
| 25 | + | prompt = 'What''s the answer?'; | |
| 26 | + | ||
| 27 | + | type | |
| 28 | + | number = longint; | |
| 29 | + | ||
| 30 | + | var | |
| 31 | + | i: number; | |
| 32 | + | ||
| 33 | + | begin | |
| 34 | + | writeLn(prompt); | |
| 35 | + | readLn(i); | |
| 36 | + | ||
| 37 | + | if not (i = answer) then | |
| 38 | + | begin | |
| 39 | + | exitCode := 1; | |
| 40 | + | end; | |
| 41 | + | end. | |
| 42 | + | ``` | |
| 43 | + | ||
| 44 | + | 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 | + | ||
| 46 | + | 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 | + | ||
| 48 | + | ## comparitive remarks | |
| 49 | + | ||
| 50 | + | 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 | + | ||
| 52 | + | ``` pascal | |
| 53 | + | program initializedVariable(input, output, stderr); | |
| 54 | + | ||
| 55 | + | var | |
| 56 | + | i: longint; | |
| 57 | + | response: string = 'Wrong!'; | |
| 58 | + | ||
| 59 | + | begin | |
| 60 | + | writeLn('What''s the answer?'); | |
| 61 | + | readLn(i); | |
| 62 | + | ||
| 63 | + | if i = 42 then | |
| 64 | + | begin | |
| 65 | + | response := 'Right!'; | |
| 66 | + | end; | |
| 67 | + | ||
| 68 | + | writeLn(response); | |
| 69 | + | end. | |
| 70 | + | ``` | |
| 71 | + | ||
| 72 | + | Also, a comparison *always* results in a [[Boolean|boolean value]]. | |
| 73 | + | ||
| 74 | + | ## ASCII value | |
| 75 | + | ||
| 76 | + | In [[ASCII]], the character code decimal `61` (or [[Hexadecimal|hexadecimal]] `3D`) is defined to be `=` (equals sign). | |
| 77 | + | ||
| 78 | + | ## see also | |
| 79 | + | ||
| 80 | + | - [[Becomes|becomes-operator :=]] assigns values to a variable | |
| 81 | + | - [[Not equal|not-equal-operator <>]] checks for inequality | |
| 82 | + | ||
| 83 | + | --- | |
| 84 | + | *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).* |
