Programming in Delphi or any other language
would not work without logic. Logic is the glue that holds together the code,
and controls how it is executed. For example, supposing we were writing a word
procesor program.
A D V E R T I S E M E N T
When the user presses the Enter key, we will move the cursor
to a new line. The code would have a logical test for the user hitting the Enter
key. If hit we do a line throw, if not, we continue on the same line.
If then elseIn the above example, we might well use the If
statement to check for the Enter key. Simple if then elseHere is an example of
how the if statement works:
var
number : Integer;
text : String; begin
number :=
Sqr(17);// Calculate the square of
17 if number > 400 then text := '17 squared > 400'//
Action when if condition is true else text := '17 squared <= 400';//
Action when if condition is false end;
text is set to : '17 squared <= 400'
There are a number of things to note about the if statement. First that it spans
a few lines - remember that Delphi allows statements to span lines - this is why
it insists on a terminating ;
Second, that the then statement does not have a terminating ;
-this is because it is part of the if statement, which is finished at the
end of the else clause.
Third, that we have set the value of a text string when the If condition
is successful - the Then clause - and when unsuccessful - the Else
clause. We could have just done a then assignment:
if number > 400 then text := '17 squared > 400';
Note that here, the then condition is not executed (because 17 squared is
not > 400), but there is no else clause. This means that the if
statement simply finishes without doing anything.
Note also that the then clause now has a terminating ; to signify
the end of the if statement. Compound if conditions, and multiple statementsWe
can have multiple conditions for the if condition. And we can have more
than one statement for the then and else clauses. Here are some examples:
if (condition1)
And (condition2) // Both conditions must be
satisfied then begin
statement1;
statement2;
...
end// Notice no terminating ';'
- still part of 'if' else begin
statement3;
statement4;
... end;
We used And to join the if conditions together - both must be satisfied
for the then clause to execute. Otherwise, the else clause will execute. We
could have used a number of different logical primitives, of which And is
one, covered under logical primitives below. Nested if statementsThere is nothing to
stop you using if statements as the statement of an if statement. Nesting can be
useful, and is often used like this:
if condition1 then statement1 elseif condition2 then statement2 else statement3;
However, too many nested if statements can make the code confusing. The Case
statement, discussed below, can be used to overcome a lot of these problems.
Logicial primitivesBefore we introduce these, it is appropriate to
introduce the
Boolean data type. It is an enumerated type, that can have one of only two
values : True or False. We will use it in place of a condition in
the if clauses below to clarify how they work:
begin if false
And false then ShowMessage('false and false = true');
if true
And false then ShowMessage('trueand false = true');
if false
And true then ShowMessage('false and true= true');
if true
And true then ShowMessage('trueand true= true');
if false
Or false then ShowMessage('false orfalse = true');
if true
Or false then ShowMessage('trueorfalse = true');
if false
Or true then ShowMessage('false ortrue= true');
if true
Or true then ShowMessage('trueortrue= true');
if false
Xor false then ShowMessage('false xor false = true');
if true
Xor false then ShowMessage('truexor false = true');
if false
Xor true then ShowMessage('false xor true= true');
if true
Xor true then ShowMessage('truexor true= true');
if
Not false then ShowMessage('not false= true');
if
Not true then ShowMessage('not true= true'); end;
Note that the Xor primitive returns true when one, but not both of the
conditions are true.
Click on the primitives in blue above to learn how they can also be used for
mathematical (bitwise) calculations.