Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Delphi Tutorial Part 2

Conditional Statements
So far we have made a program to execute a bunch of fixed commands, so no matter what happens, all of the statements in that program will be carried out in chronological order. There are many cases though where we only want to carry out actions under certain cases or conditions, and this exactly what we're going to do in this part of the tutorial. The statements IF, ELSE and CASE will be demonstrated here to bring out a decision based element in our program. To do this, we will request an input about someones age and provide either a younger or an older user response.

Whenever we want to make a simple decision we use IF. The statements that follow IF are considered to be the conditions or expressions.


If (a=1) then
Begin
writeln('a equals 1');
end;


All of the commands after then are executed if the conditions turn out to be true. Above you can see that if the variable a equals 1, then the actions between begin and end are executed. In this scenario, the message "a equals 1" is output on screen. As this is an If statement though, and not the end of our program, END has to be terminated with a semi-colon.

You can choose to declare more than one condition in an IF statement, by using more than one group of brackets. We also have to separate two or more conditions using operators like OR, AND, XOR. The code below is an example of how we do this, and how much of a difference these operators can make.

Using the OR operator

If (a=1) OR (b=1) then
begin
writeln('either a or b equal 1');
end;


Using the AND operator

If (a=1) AND (b=1) then
begin
writeln('both a and b equal 1');
end;


In the first case, either a or b can be equal to one for the If statement to be true. Although with the second case, both a and b have to be equal to one for the if to be true. If the statement is in fact false, we can also choose to bind some instructions to this false condition using ELSE. ELSE in programming is just like saying, otherwise do this...


If (a=1) AND (b=1) then
begin
writeln('both a and b equal 1');
end
else
begin
writeln('either a or b does not equal 1');
end;


Now you can see that if a or b isn't equal to one, then a message is written on screen declaring that this is the case. However now that we have used the ELSE statement, we also have to shift the semi-colon from the IF clause down to the ELSE clause. Even better, you can choose to remove begin and end from both of these clauses if there is only one line inside them, like so.


If (a=1) AND (b=1) then
writeln('both a and b equal 1')
else
writeln('either a or b does not equal 1');


Here is a list of most of all the possible operators you can use in Delphi, with a small explanation of the purpose of each. Most of these are to be used in actual expressions like an equals sign, unlike the example above where AND/OR are used to join up expressions.

AND: both expressions on either side of this operator must be true to execute the code.
OR: one of the expressions on either side of the operator must be true to execute the code.
XOR: the expression on the left must be true, or the expression on the right must be false.
NOT: the expression that follows must be false for the condition to be true.
=: the left side of an expression or equation must be exactly equal to the right side for it to be true.
: the left side of the equation must be less than the right side for the condition to be true.
: the left side of the equation must be less than or equal to the right side to be true.
>=: the left side of the equation must be larger or equal to the right side to be true.
=!: the left side of the equation must not be equal to the value on the right to be true.
: the left and right side of the equation cannot be equal to each other for the condition to be true.

You can go a step further with the IF and ELSE statements by rooting them off one another.


if (a=1) then
begin
if (b=1) then
writeln('both a and b equal 1')
else
writeln('just a equals 1');
end
else
if (b1) then
writeln('both a and b don't equal 1');


Sometimes you can have a ridiculous number of these statements, and so the CASE statement exists to simplify things greatly.


Case a of
1: writeln('a equals 1'); //case 1
2: writeln('a equals 2'); //case 2
3: writeln('a equals 3'); //case 3
else
writeln('a is less than 1 or larger than 3');


The only problem with the case statement, is that you can't use a string variable. You have to use an ordinal or fixed variant, such as an Integer.

Now that you have got to grips with how conditional statements work, hopefully you should understand the new code we will be adding below to our program.


program MyFirstProgram;

var
nameVar: String; //name variable
age: Integer;

begin
writeln('What is your name? ');
readln(nameVar);
writeln('Hello '+nameVar+' how old are you?');
//new code starts here
readln(age);
if age writeln('Oh, so do you still study?')
else
writeln('What do you do for living?');
end.


We've added a new variable called age to use in our conditional statement, and have classed it as an integer as we only want to store a whole number. We then use IF to see if the age that was input, was less than 23 and then proceed accordingly. If the age was less than 23 we ask if this person is still studying, but otherwise we ask what they do for a living.

The next chapter in this tutorial covers loops, and how a process can be repeated as often as you want without re-typing code.


This post first appeared on The Computer Monkey, please read the originial post: here

Share the post

Delphi Tutorial Part 2

×

Subscribe to The Computer Monkey

Get updates delivered right to your inbox!

Thank you for your subscription

×