begin
Statements
end
Description
The Begin keyword is fundamental to Delphi - it starts statement blocks.
The begin-end pair fence in a set of statements. You may place such a block anywhere in your code.
It is particularly sensible in if and for statements, even if only one statement is required. It means that adding an additional statement in the future is easy.
For example :
if a = 7 then do
Inc(b, a);
Is better written :
if a = 7 then do
begin
Inc(b, a);
end;
for maintenance purposes.
Related commands
End Keyword that terminates statement blocks
For Starts a loop that executes a finite number of times
Function Defines a subroutine that returns a value
Procedure Defines a subroutine that does not return a value
Repeat Repeat statements until a ternmination condition is met
While Repeat statements whilst a continuation condition is met
Example code : Some examples of the begin statement
var
myChars : array[1..2] of char;
myBytes : array[1..2] of Byte;
i : Integer;
// The begin statement always starts the code part of a subroutine
Begin
// Use a for block to assign to both arrays
for i := 1 to 2 do
Begin
myChars[i] := Chr(i+64);
myBytes[i] := i+64;
end;
// Use a for block to observe the contents
for i := 1 to 2 do
Begin
ShowMessage('myChars['+IntToStr(i)+'] = '+myChars[i]);
ShowMessage('myBytes['+IntToStr(i)+'] = '+IntToStr(myBytes[i]));
end;
end;
Show full unit code
myChars[1] = A
myBytes[1] = 65
myChars[2] = B
myChars[2] = 66