Difference between revisions of "Slash"
m (→comment: line number) |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{Slash}} | {{Slash}} | ||
+ | |||
+ | <div style="float:right; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">/</div> | ||
A single slash, surrounded by non-slash characters is regarded as the division operator. | A single slash, surrounded by non-slash characters is regarded as the division operator. | ||
Line 5: | Line 7: | ||
== division == | == division == | ||
− | The slash <syntaxhighlight lang="pascal" | + | The ASCII slash <syntaxhighlight lang="pascal" inline>/</syntaxhighlight> is used in a [[Pascal]] [[Program|program]] to perform division (<syntaxhighlight lang="pascal" inline>∕</syntaxhighlight> U+2215 “division slash”). |
The results are ''always'' real values. | The results are ''always'' real values. | ||
− | If you want to perform integer division the [[Div|<syntaxhighlight lang="pascal" | + | If you want to perform integer division the [[Div|<syntaxhighlight lang="pascal" inline>div</syntaxhighlight> operator]] has to be used. |
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
A := 3 / 4; | A := 3 / 4; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | After this operation the variable <syntaxhighlight lang="pascal" | + | After this operation the [[Variable|variable]] <syntaxhighlight lang="pascal" inline>A</syntaxhighlight> holds the value <syntaxhighlight lang="pascal" inline>0.75</syntaxhighlight> (assuming <syntaxhighlight lang="pascal" inline>A</syntaxhighlight> is declared as a real value [[Type|type]], otherwise the [[Compiler|compiler]] generates an incompatible type error). |
=== related exceptions === | === related exceptions === | ||
The value on the right side of the slash must not be zero, or a division by zero error occurs. | The value on the right side of the slash must not be zero, or a division by zero error occurs. | ||
− | In modes where exceptions are available (e.g. [[Mode ObjFPC|ObjFPC]] and [[Mode Delphi|Delphi]] mode) this condition can be caught by using a [[Try|<syntaxhighlight lang="pascal" | + | 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|<syntaxhighlight lang="pascal" inline>try</syntaxhighlight>]] … [[Except|<syntaxhighlight lang="pascal" inline>except</syntaxhighlight>]] [[Frame|frame]]. |
− | Otherwise a [[run-time error]] occurs ( | + | Otherwise a [[runtime error|run-time error]] occurs (RTE 200). |
<syntaxhighlight lang="pascal" highlight="25-30">program divZeroDemo(input, output, stderr); | <syntaxhighlight lang="pascal" highlight="25-30">program divZeroDemo(input, output, stderr); | ||
Line 49: | Line 51: | ||
end; | end; | ||
end.</syntaxhighlight> | end.</syntaxhighlight> | ||
− | Note | + | {{Note| |
Exception handling is expensive. | Exception handling is expensive. | ||
− | A plain test whether the user input is non-zero would have been in the above example more sophisticated. | + | A plain test whether the user input is non-zero would have been in the above example more sophisticated.}} |
== comment == | == comment == | ||
Line 62: | Line 64: | ||
== see also == | == see also == | ||
− | * [[Round|<syntaxhighlight lang="pascal" | + | * [[Round|<syntaxhighlight lang="pascal" inline>round</syntaxhighlight>]] |
− | * [[Trunc|<syntaxhighlight lang="pascal" | + | * [[Trunc|<syntaxhighlight lang="pascal" inline>trunc</syntaxhighlight>]] |
{{Symbols}} | {{Symbols}} | ||
− | |||
− | |||
− | |||
− |
Latest revision as of 02:00, 26 January 2021
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
A single slash, surrounded by non-slash characters is regarded as the division operator. Two consecutive slashes are regarded as comment introducers.
division
The ASCII slash /
is used in a Pascal program to perform division (∕
U+2215 “division slash”).
The results are always real values.
If you want to perform integer division the div
operator has to be used.
A := 3 / 4;
After this operation the variable A
holds the value 0.75
(assuming A
is declared as a real value type, otherwise the compiler generates an incompatible type error).
The value on the right side of the slash must not be zero, or a division by zero error occurs.
In modes where exceptions are available (e.g. ObjFPC and Delphi mode) this condition can be caught by using a try
… except
frame.
Otherwise a run-time error occurs (RTE 200).
program divZeroDemo(input, output, stderr);
// ObjFPC mode for exceptions
{$mode objfpc}
uses
// make exception EDivByZero known
sysutils;
const
dividend = 1.1;
resourcestring
enterDivisorPrompt = 'Enter divisor:';
divisionOperationExceptionless = 'Division did not cause an exception.';
zeroDivisionFailure = 'Error: Attempted to divide by zero.';
var
divisor, quotient: single;
begin
writeLn(enterDivisorPrompt);
readLn(divisor);
try
quotient := dividend / divisor;
writeLn(divisionOperationExceptionless);
except on EDivByZero do
writeLn(zeroDivisionFailure);
end;
end.
Note: Exception handling is expensive.
A plain test whether the user input is non-zero would have been in the above example more sophisticated.comment
Two slashes back to back introduce comments till the end of line. This is also known as “Delphi-style comment”.
1345while (buf^ in [' ', #9, #10]) do // kill separators
see also
single characters |
|
character pairs |
|