Delphi Programming/Flow control
Contents
Structures[edit]
The if-structure[edit]
The if-structure executes a block of commands if a boolean expression returns True. A more simple to understand explanation is: It executes a block or a single command if a condition is true. Example:
-
begin
-
if a = False then
-
WriteLn('a is false')
-
else WriteLn('a is true');
-
end.
Never write "if a = True" but simply write "if a". Writing "if a = False" is correct, but you can also write "if not a" or (with brackets) "if (((((a)))))" (or as many brackets you want), also "(if (not(a)))".
Structure:
-
begin
-
if CONDITION then
-
DO_ANYTHING
-
else DO_THIS;
-
end.
or (for more than one command to execute):
-
begin
-
if CONDITION then
-
begin
-
DO_ANYTHING;
-
end
-
else begin
-
DO_THIS;
-
end;
-
end.
Or without the else:
-
begin
-
if CONDITION then
-
begin
-
DO_THIS;
-
end;
-
end.
Except the last end there's always a semicolon behind the end. There is never a semicolon before an "Else"!
Example:
-
var
-
_Answer: string;
-
begin
-
WriteLn('Do you want to order a pizza?');
-
ReadLn(_Answer);
-
if _Answer = 'Yes' then
-
WriteLn('You decided for yes!')
-
else WriteLn('Don''t want to have a pizza?');
-
end.
You can start and end a string with a quote ('
) or a double quote ("
). How to write a quote or double quote in a string? It would end the string in the middle! If you have to write a quote in the text, you can start and end your string with a double quote or write your quote twice as it has been done at line 8. Do the same thing for a double quote.
The case structure[edit]
The Case structure is quite similar to the if structure with the following difference: You can more easily ask for several cases!
Structure:
-
case VARIABLE_NAME of
-
VALUE_1:
-
DO_THIS;
-
VALUE_N:
-
DO_THIS
-
else
-
DO_THIS
-
end;
-
end;
But with a case-structure you can only ask for Integers and chars.
Operators[edit]
Expanding the condition[edit]
You can expand your condition with a few operators:
- AND (like
&&
in C): logical 'and':if (a = 1) and (b = 2)
. The value of the expression "(a = 1) and (b = 2)
" is TRUE if a is 1 and b is 2. Else, the value is FALSE and the ELSE-part will be executed (and not the part after THEN). Don't forget the brackets!
- OR (like
||
in C): 'or':if (a = 1) or (b = 1)
. If a is 1, b is 1 or both is 1, the value of the expression is TRUE. - XOR: If only one of the conditions is true:
if (a = 1) xor (b = 2)
. The expression is true if a is 1 or b is 2. If a is 1 AND b is 2, the value will be FALSE! - NOT: The opposite of the expression.
It's also possible to interlink that operators. But then don't forget the brackets!
By the way: Every condition returns a boolean value. If it is TRUE, the then-part will be executed. If not, the processor goes to the else-part.
Operators such as 'equals'[edit]
Operators such as '=' are:
- = - equals
- > - greater than
- < - less than
- <= - less or equals
- >= - greater or equals
- <> - less or greater (but not the same)
Operators for calculating[edit]
- You can use ( and ) as brackets.
- / means 'divided by', the result is a float
- div means 'divided by', the result is a rounded integer
- * means 'times'
- + means 'plus'
- - means 'minus'
+ also means linking of strings or chars:
- string + string : string
- string + char : string
- char + char : string
- char + string : string
- string + number : error
- number + string : error
- number + number : number
- number + char : error
- char + number : error
Loops[edit]
Loop means: A block will be executed many times. There are four types of loops:
For-to[edit]
-
for [var] := [start] to [end] do
-
begin
-
[execute the following code]
-
end;
The var will count from [start] to [end] and after every counting step the code will be executed. Normally the [var] is defined as i, j or k, but you can also choose counting_var_with_this_name or any name.
For-downto[edit]
-
for [var] := [start] downto [end] do
-
begin
-
[execute the following code]
-
end;
The var will count down from [start] to [end] and after every counting step the code will be executed.
While-do[edit]
-
while [condition] do
-
begin
-
[code]
-
end;
While the condition is true, the code will be executed. Whether the condition is TRUE or FALSE will be checked BEFORE executing the code.
Repeat-until[edit]
-
repeat
-
[code]
-
until [condition];
The code will be executed until the condition is true. Whether the condition is TRUE or FALSE will be checked AFTER executing the code.
Setting values[edit]
The operator for setting values is :=
-
a := b;
By executing, a will get the value of b.
EXAMPLE:
a equals 1; b equals 3
After executing:
a equals 3; b equals 3
and not:
a equals 1; b equals 1
Be careful with the order!