Slash: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Slash
| Old | New | Line | |
|---|---|---|---|
| 1 | + | <div style="float:right; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;"> | |
| 2 | + | ||
| 3 | + | / | |
| 4 | + | ||
| 5 | + | </div> | |
| 6 | + | ||
| 7 | + | A single slash, surrounded by non-slash characters is regarded as the division operator. Two consecutive slashes are regarded as comment introducers. | |
| 8 | + | ||
| 9 | + | ## division | |
| 10 | + | ||
| 11 | + | 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 | + | ||
| 13 | + | ``` pascal | |
| 14 | + | A := 3 / 4; | |
| 15 | + | ``` | |
| 16 | + | ||
| 17 | + | 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 | + | ||
| 19 | + | ### related exceptions | |
| 20 | + | ||
| 21 | + | 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 | + | ||
| 23 | + | ``` pascal | |
| 24 | + | program divZeroDemo(input, output, stderr); | |
| 25 | + | ||
| 26 | + | // ObjFPC mode for exceptions | |
| 27 | + | {$mode objfpc} | |
| 28 | + | ||
| 29 | + | uses | |
| 30 | + | // make exception EDivByZero known | |
| 31 | + | sysutils; | |
| 32 | + | ||
| 33 | + | const | |
| 34 | + | dividend = 1.1; | |
| 35 | + | ||
| 36 | + | resourcestring | |
| 37 | + | enterDivisorPrompt = 'Enter divisor:'; | |
| 38 | + | divisionOperationExceptionless = 'Division did not cause an exception.'; | |
| 39 | + | zeroDivisionFailure = 'Error: Attempted to divide by zero.'; | |
| 40 | + | ||
| 41 | + | var | |
| 42 | + | divisor, quotient: single; | |
| 43 | + | ||
| 44 | + | begin | |
| 45 | + | writeLn(enterDivisorPrompt); | |
| 46 | + | readLn(divisor); | |
| 47 | + | ||
| 48 | + | try | |
| 49 | + | quotient := dividend / divisor; | |
| 50 | + | writeLn(divisionOperationExceptionless); | |
| 51 | + | except on EDivByZero do | |
| 52 | + | writeLn(zeroDivisionFailure); | |
| 53 | + | end; | |
| 54 | + | end. | |
| 55 | + | ``` | |
| 56 | + | ||
| 57 | + | > **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 | + | ||
| 59 | + | ## comment | |
| 60 | + | ||
| 61 | + | 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 | + | ||
| 63 | + | ```delphi | |
| 64 | + | while (buf^ in [' ', #9, #10]) do // kill separators | |
| 65 | + | ``` | |
| 66 | + | ||
| 67 | + | <small>[example source](https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/rtl/inc/system.inc?view=markup#l1345)</small> | |
| 68 | + | ||
| 69 | + | ## see also | |
| 70 | + | ||
| 71 | + | - [[Round|round]] | |
| 72 | + | - [[Trunc|trunc]] | |
| 73 | + | ||
| 74 | + | --- | |
| 75 | + | *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).* |
