Difference between revisions of "Plus"

From Lazarus wiki
Jump to navigationJump to search
(Created page with " {{Plus}} The symbol '''''+''''' (pronounced "plus") is used to : * add two numbers, the result is a number * or concatenate two strings, the result is a string. A := 1 + 3...")
 
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{Plus}}
 +
 +
<div style="float:right; margin: 0 10px 10px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">+</div>
 +
 +
The symbol <syntaxhighlight lang="pascal" inline>+</syntaxhighlight> (pronounced “plus”) is used to:
 +
 +
* explicitly indicate the positive sign of a number,
 +
* add two numbers resulting to a number,
 +
* form a union of [[Set|sets]],
 +
* (FPC) concatenate two strings (or characters; except [[PChar|<syntaxhighlight lang="pascal" inline>pchar</syntaxhighlight>]]).
 +
* if <syntaxhighlight lang="delphi" inline>{$modeSwitch arrayOperators+}</syntaxhighlight> (default in [[Mode Delphi|<syntaxhighlight lang="delphi" inline>{$mode Delphi}</syntaxhighlight>]]), concatenate arrays (since FPC 3.2.0)
 +
* enable a feature in a <syntaxhighlight lang="delphi" inline>{$modeSwitch}</syntaxhighlight>
  
{{Plus}}
+
<syntaxhighlight lang="pascal" line>
 +
program plusDemo(input, output, stderr);
 +
 
 +
var
 +
x: longint;
 +
g: set of (foo, bar);
 +
m: string;
 +
begin
 +
// unary operator: positive sign
 +
x := +7;                    // x becomes positive 7
 +
x := +$100;                // x becomes 256
 +
                            // (dollar sign denotes hexadecimal base)
 +
 +
// addition
 +
x := 7 + 7;                // x becomes 14
 +
x := 7 + 7 + 7 + 7 + 7 + 7; // x becomes 42
 +
 +
// union of sets
 +
g := [foo] + [bar];        // g becomes [foo, bar]
 +
 +
// concatenation of strings and/or characters (FPC/Delphi extension)
 +
m := 'Hello ' + 'world!';  // m becomes 'Hello world!'
 +
end.
 +
</syntaxhighlight>
  
The symbol '''''+''''' (pronounced "plus") is used to :
+
The plus sign is also a unary operator.
* add two numbers, the result is a number
+
One can write such stupid expressions as <syntaxhighlight lang="pascal" inline>++++++++++++++42</syntaxhighlight> which will evaluate to positive 42.
* or concatenate two strings, the result is a string.
 
  
A := 1 + 3; // -> A = 4
+
In [[ASCII]], the character code decimal <syntaxhighlight lang="pascal" inline>43</syntaxhighlight> (or [[Hexadecimal|hexadecimal]] <syntaxhighlight lang="pascal" inline>2B</syntaxhighlight>) is defined to be <syntaxhighlight lang="pascal" inline>+</syntaxhighlight> (plus sign).
B := 'two strings ' + 'concatenated'; // -> B = 'two strings concatenated'
 
  
 +
== see also ==
 +
* {{Doc|package=RTL|unit=system|identifier=.op-add-variant-ariant-ariant|text=<syntaxhighlight lang="pascal" inline>system.add</syntaxhighlight>}}
 +
* {{Doc|package=RTL|unit=system|identifier=concat|text=<syntaxhighlight lang="pascal" inline>system.concat</syntaxhighlight>}} returns concatenation of strings
 +
* {{Doc|package=RTL|unit=strings|identifier=strcat|text=<syntaxhighlight lang="pascal" inline>strings.strcat</syntaxhighlight>}} returns concatenation of [[PChar|<syntaxhighlight lang="pascal" inline>pchar</syntaxhighlight>]] strings
  
 
{{Symbols}}
 
{{Symbols}}
{{Stub}}
 
[[Category:Pascal]]
 
[[Category:Symbols]]
 

Latest revision as of 00:41, 17 October 2019

English (en) suomi (fi) français (fr) русский (ru)

+

The symbol + (pronounced “plus”) is used to:

  • explicitly indicate the positive sign of a number,
  • add two numbers resulting to a number,
  • form a union of sets,
  • (FPC) concatenate two strings (or characters; except pchar).
  • if {$modeSwitch arrayOperators+} (default in {$mode Delphi}), concatenate arrays (since FPC 3.2.0)
  • enable a feature in a {$modeSwitch}
 1program plusDemo(input, output, stderr);
 2
 3var
 4	x: longint;
 5	g: set of (foo, bar);
 6	m: string;
 7begin
 8	// unary operator: positive sign
 9	x := +7;                    // x becomes positive 7
10	x := +$100;                 // x becomes 256
11	                            // (dollar sign denotes hexadecimal base)
12	
13	// addition
14	x := 7 + 7;                 // x becomes 14
15	x := 7 + 7 + 7 + 7 + 7 + 7; // x becomes 42
16	
17	// union of sets
18	g := [foo] + [bar];         // g becomes [foo, bar]
19	
20	// concatenation of strings and/or characters (FPC/Delphi extension)
21	m := 'Hello ' + 'world!';   // m becomes 'Hello world!'
22end.

The plus sign is also a unary operator. One can write such stupid expressions as ++++++++++++++42 which will evaluate to positive 42.

In ASCII, the character code decimal 43 (or hexadecimal 2B) is defined to be + (plus sign).

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)