Delphi Programming/Operators, structures and loops
Contents |
Structures [edit]
The if-structure [edit]
The if-structure executes a block of commands if a boolean expression returns True. A more simle 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.
With '' in a string you can put ' out (because of a single ' would close the string). For '' Write '''' (or with the start and end chars '''''').
The case structure [edit]
The Case structure is quite similar to the if structure with the following difference: You can easier 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 (C: &): bitwise '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 (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!
This page may need to be