Difference between revisions of "Colon"

From Free Pascal wiki
Jump to navigationJump to search
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Colon}}
 
{{Colon}}
  
<div style="float:left; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">:</div>
+
<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>
  
  
The symbol : (pronounced "colon") is used in [[Pascal]] in several ways.
+
The symbol <syntaxhighlight lang="pascal" inline>:</syntaxhighlight>, pronounced “colon”, is used in [[Pascal]] in several ways:
  
* It is used to indicate the [[Type|type]] of an [[Identifier|identifier]] in a [[Var|var]] or [[Const|const]] declaration.
+
* in an [[Identifier|identifier]] declaration it separates the [[Type|type]]
 +
** most notably in [[Var|<syntaxhighlight lang="pascal" inline>var</syntaxhighlight>]] sections
 +
** but also in [[Const|<syntaxhighlight lang="pascal" inline>const</syntaxhighlight>]] sections (in case of ''explicitely'' typed constants)
 +
** [[Procedure|<syntaxhighlight lang="pascal" inline>procedure</syntaxhighlight>]] and [[Function|<syntaxhighlight lang="pascal" inline>function</syntaxhighlight>]] parameters have a type, too
 +
** and return values of functions (including operator overloads)
 +
** as well as [[Property|properties]]
 +
** in general, everywhere where a new identifier associated with some memory, is introduced (also custom structured type definitions)
 +
* in [[Case|<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>]] selectors it closes a match-list
 +
* in [[Label|label]] definitions the colon seperates the instruction from the label name
 +
* also some routines, especially the [[Str|<syntaxhighlight lang="pascal" inline>Str</syntaxhighlight>]], [[Write|<syntaxhighlight lang="pascal" inline>write</syntaxhighlight>]] and <syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> procedures accept further parameters via colon separated arguments
 +
* Offset / segment separation under DOS <syntaxhighlight lang="pascal" inline>Mem[$B800:$0000]</syntaxhighlight>
  
* It is used to indicate a [[Case|case]] statement entry.
+
The following example shows the most prevalent usage scenarios:
 +
<syntaxhighlight lang="pascal" line highlight="3,6,10,13,23">
 +
program colonDemo(input, output, stderr);
  
* It separate a [[Function|function]] declaration with the function type.
+
procedure numberReport(const i: int64);
 +
begin
 +
case i of
 +
low(i)..-1:
 +
begin
 +
writeLn('Your number is negative. ☹');
 +
end;
 +
1..high(i):
 +
begin
 +
// right aligns to a width of 24 characters
 +
writeLn(i:24);
 +
end;
 +
else
 +
begin
 +
writeLn('You''ve entered zero.');
 +
end;
 +
end;
 +
end;
  
* It is used to indicate a [[Label|label]].
+
var
 +
i: int64;
  
* It is used to separate the optional field width specifier from the value for a [[Write]] or Writeln parameter.
+
begin
 +
writeLn('Enter a number:');
 +
readLn(i);
 +
numberReport(i);
 +
end.
 +
</syntaxhighlight>
  
The colon [[ASCII]] or the unicode value is 58.
+
== other remarks ==
 +
* Colon appears in the assignment operator [[Becomes|<syntaxhighlight lang="pascal" inline>:=</syntaxhighlight>]].
 +
* In [[ASCII]] the character colon <syntaxhighlight lang="pascal" inline>:</syntaxhighlight> has the value <syntaxhighlight lang="pascal" inline>58</syntaxhighlight>.
  
  
 
{{Symbols}}
 
{{Symbols}}
 +
 +
[[Category:Code]]

Latest revision as of 16:15, 6 August 2022

English (en) suomi (fi) français (fr) русский (ru) 中文(中国大陆)‎ (zh_CN)

:


The symbol :, pronounced “colon”, is used in Pascal in several ways:

  • in an identifier declaration it separates the type
    • most notably in var sections
    • but also in const sections (in case of explicitely typed constants)
    • procedure and function parameters have a type, too
    • and return values of functions (including operator overloads)
    • as well as properties
    • in general, everywhere where a new identifier associated with some memory, is introduced (also custom structured type definitions)
  • in case selectors it closes a match-list
  • in label definitions the colon seperates the instruction from the label name
  • also some routines, especially the Str, write and writeLn procedures accept further parameters via colon separated arguments
  • Offset / segment separation under DOS Mem[$B800:$0000]

The following example shows the most prevalent usage scenarios:

 1program colonDemo(input, output, stderr);
 2
 3procedure numberReport(const i: int64);
 4begin
 5	case i of
 6		low(i)..-1:
 7		begin
 8			writeLn('Your number is negative. ☹');
 9		end;
10		1..high(i):
11		begin
12			// right aligns to a width of 24 characters
13			writeLn(i:24);
14		end;
15		else
16		begin
17			writeLn('You''ve entered zero.');
18		end;
19	end;
20end;
21
22var
23	i: int64;
24
25begin
26	writeLn('Enter a number:');
27	readLn(i);
28	numberReport(i);
29end.

other remarks

  • Colon appears in the assignment operator :=.
  • In ASCII the character colon : has the value 58.



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)