Slash: 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 | 2 | ||
| 5 | - | ||
| 6 | - | ||
| 7 | 3 | A single slash, surrounded by non-slash characters is regarded as the division operator. Two consecutive slashes are regarded as comment introducers. | |
| 8 | 4 | ||
| 9 | 5 | ## division | |
| 10 | 6 | ||
| 11 | 7 | The ASCII slash `/` is used in a [[Pascal]] [[Program|program]] to perform division (`∕` U+2215 “division slash”). The results are *always* real values. If you want to perform integer division the [[Div|div operator]] has to be used. | |
| 12 | 8 | ||
| 13 | 9 | ``` pascal | |
| 14 | 10 | A := 3 / 4; | |
| 15 | 11 | ``` | |
| 16 | 12 | ||
| 17 | 13 | After this operation the [[Variable|variable]] `A` holds the value `0.75` (assuming `A` is declared as a real value [[Type|type]], otherwise the [[Compiler|compiler]] generates an incompatible type error). | |
| 18 | 14 | ||
| 19 | 15 | ### related exceptions | |
| 20 | 16 | ||
| 21 | 17 | The value on the right side of the slash must not be zero, or a division by zero error occurs. In [[Compiler Mode|modes]] where [[Exceptions|exceptions]] are available (e.g. [[Mode ObjFPC|ObjFPC]] and [[Mode Delphi|Delphi]] mode) this condition can be caught by using a [[Try|try]] … [[Except|except]] [[Frame|frame]]. Otherwise a [[runtime error|run-time error]] occurs (RTE 200). | |
| 22 | 18 | ||
| 23 | 19 | ``` pascal | |
| 24 | 20 | program divZeroDemo(input, output, stderr); | |
| 25 | 21 | ||
| 26 | 22 | // ObjFPC mode for exceptions | |
| 27 | 23 | {$mode objfpc} | |
| 28 | 24 | ||
| 29 | 25 | uses | |
| 30 | 26 | // make exception EDivByZero known | |
| 31 | 27 | sysutils; | |
| 32 | 28 | ||
| 33 | 29 | const | |
| 34 | 30 | dividend = 1.1; | |
| 35 | 31 | ||
| 36 | 32 | resourcestring | |
| 37 | 33 | enterDivisorPrompt = 'Enter divisor:'; | |
| 38 | 34 | divisionOperationExceptionless = 'Division did not cause an exception.'; | |
| 39 | 35 | zeroDivisionFailure = 'Error: Attempted to divide by zero.'; | |
| 40 | 36 | ||
| 41 | 37 | var | |
| 42 | 38 | divisor, quotient: single; | |
| 43 | 39 | ||
| 44 | 40 | begin | |
| 45 | 41 | writeLn(enterDivisorPrompt); | |
| 46 | 42 | readLn(divisor); | |
| 47 | 43 | ||
| 48 | 44 | try | |
| 49 | 45 | quotient := dividend / divisor; | |
| 50 | 46 | writeLn(divisionOperationExceptionless); | |
| 51 | 47 | except on EDivByZero do | |
| 52 | 48 | writeLn(zeroDivisionFailure); | |
| 53 | 49 | end; | |
| 54 | 50 | end. | |
| 55 | 51 | ``` | |
| 56 | 52 | ||
| 57 | 53 | > **Note:** Exception handling is expensive. A plain test whether the user input is non-zero would have been in the above example more sophisticated. | |
| 58 | 54 | ||
| 59 | 55 | ## comment | |
| 60 | 56 | ||
| 61 | 57 | Two slashes back to back introduce [[Comments|comments]] till the [[End of Line|end of line]]. This is also known as “Delphi-style comment”. | |
| 62 | 58 | ||
| 63 | 59 | ```delphi | |
| 64 | 60 | while (buf^ in [' ', #9, #10]) do // kill separators | |
| 65 | 61 | ``` | |
| 66 | 62 | ||
| 67 | - | ||
| 63 | + | [example source](https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/rtl/inc/system.inc?view=markup#l1345) | |
| 68 | 64 | ||
| 69 | 65 | ## see also | |
| 70 | 66 | ||
| 71 | 67 | - [[Round|round]] | |
| 72 | 68 | - [[Trunc|trunc]] | |
| 73 | 69 | ||
| 74 | 70 | --- | |
| 75 | 71 | *From the Free Pascal wiki, page "Slash", as it stood on 2021-01-26 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).* |
