Delphi Programming/Flow control


The if-structureEdit

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:

1  begin
2    if a = False then
3      WriteLn('a is false')
4    else WriteLn('a is true');
5  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)))".


1  begin
2    if CONDITION then
4    else DO_THIS;
5  end.

or (for more than one command to execute):

1  begin
2    if CONDITION then
3    begin
5    end
6    else begin
7      DO_THIS;
8    end;
9  end.

Or without the else:

1  begin
2    if CONDITION then
3    begin
4      DO_THIS;
5    end;
6  end.

Except the last end there's always a semicolon behind the end. There is never a semicolon before an "Else"!


1  var
2    _Answer: string;
3  begin
4    WriteLn('Do you want to order a pizza?');
5    ReadLn(_Answer);
6    if _Answer = 'Yes' then
7      WriteLn('You decided for yes!')
8    else WriteLn('Don''t want to have a pizza?');
9  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 structureEdit

The Case structure is quite similar to the if structure with the following difference: You can more easily ask for several cases!


1  case VARIABLE_NAME of
2    VALUE_1:
3      DO_THIS;
4    VALUE_N:
5      DO_THIS
6    else
7      DO_THIS
8    end;
9  end;

But with a case-structure you can only ask for Integers and chars.


Expanding the conditionEdit

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
  • <> not equal (less or greater, not the same)

and conjunction or disjunction xor exclusive disjunction

Operators for calculatingEdit

  • 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


Loop means: A block will be executed many times. There are four types of loops:


1  for [var] := [start] to [end] do
2  begin
3    [execute the following code]
4  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.


1  for [var] := [end] downto [start] do
2  begin
3    [execute the following code]
4  end;

The var will count down from [end] to [start] and after every counting step the code will be executed.


1  while [condition] do
2  begin
3    [code]
4  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.


1  repeat
2    [code]
3  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 valuesEdit

The operator for setting values is :=

1  a := b;

By executing, a will get the value of b.


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!