Difference between revisions of "Slash"

From Free Pascal wiki
m
(review: add exception handling example; add section comment)
Line 1: Line 1:
 
 
{{Slash}}
 
{{Slash}}
  
<div style="float:left; margin: 0 10px 10px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;"><nowiki>/</nowiki></div>
+
A single slash, surrounded by non-slash characters is regarded as the division operator.
 +
Two consecutive slashes are regarded as comment introducers.
  
The '''''/''''' or '''slash''' is used in a [[Pascal]] program to perform division. The results are a real value. If you want integer type value then use the [[Div|div]] operator, [[Round|round]] or [[Trunc|trunc]] function . If you want know remainder use [[Mod|mod]].
+
== division ==
 +
The slash <syntaxhighlight lang="pascal" enclose="none">/</syntaxhighlight> is used in a [[Pascal]] program to perform division.
 +
The results are a real values.
 +
If you want to perform integer division the [[Div|<syntaxhighlight lang="pascal" enclose="none">div</syntaxhighlight> operator]] has to be used.
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
A := 3 / 4;
+
A := 3 / 4;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
After this operation the variable <syntaxhighlight lang="pascal" enclose="none">A</syntaxhighlight> holds the value <syntaxhighlight lang="pascal" enclose="none">0.75</syntaxhighlight> (assuming <syntaxhighlight lang="pascal" enclose="none">A</syntaxhighlight> is delared as a real value type, otherwise the compiler generates an incompatible type error).
  
returns 0.75 if a is real (if a is integer-type then get error:Incompatible types).
+
=== related exceptions ===
 +
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" enclose="none">try</syntaxhighlight>]] …  [[Except|<syntaxhighlight lang="pascal" enclose="none">except</syntaxhighlight>]] block.
 +
<syntaxhighlight lang="pascal" highlight="25-30">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.</syntaxhighlight>
 +
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|comments]] till the [[End of Line|end of line]].
 +
This is also known as “Delphi-style comment”.
 +
<syntaxhighlight lang="delphi">
 +
while (buf^ in [' ', #9, #10]) do // kill separators
 +
</syntaxhighlight>
 +
<small>[https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/rtl/inc/system.inc?view=markup#l1345 example source]</small>
  
The value on the right side of the slash must not be zero, or a division by zero error occurs. This can be caught through use of a [[Try|try]] .. [[Finally|finally]] or [[Try|try]] .. [[Except|except]] block.
+
== see also ==
 +
* [[Round|<syntaxhighlight lang="pascal" enclose="none">round</syntaxhighlight>]]
 +
* [[Trunc|<syntaxhighlight lang="pascal" enclose="none">trunc</syntaxhighlight>]]
  
 
{{Symbols}}
 
{{Symbols}}

Revision as of 18:23, 20 March 2018

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 slash / is used in a Pascal program to perform division. The results are a 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 delared as a real value type, otherwise the compiler generates an incompatible type error).

related exceptions

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 block.

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”.

while (buf^ in [' ', #9, #10]) do // kill separators

example source

see also


navigation bar: topic: Pascal symbols
single characters

+ (plus)  •  - (minus)  •  * (asterisk)  •  / (slash)
= (equal)  •  > (greater than)  •  < (less than)
. (period)  •  : (colon)  •  ; (semi colon)
^ (hat)  •  @ (at)
$ (dollar sign)  •  & (ampersand)  •  # (hash)
' (single quote)

character pairs

<> (not equal)  •  <= (less than or equal)  •  := (becomes)  •  >= (greater than or equal)

 •  >< (symmetric difference)  •  // (double slash)