Delphi programming tutorial 2

In tutorial 1 I explained how to create a simple program with some buttons, editboxes and how to
handle strings and integers. I think now it's time for some programming basics and how to use them
in Delphi.

In the next part I will explain an IF THEN ELSE structure and also show you how to give some messages
to the user of your program.

First of all start with a new application and setup your form with two editboxes and a button. Now
doubleclick on the button to edit the source code. Change the code into this:

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
 If StrToInt(Edit1.Text) < StrToInt(Edit2.Text) then Showmessage(Edit1.Text + ' is smaller than ' + Edit2.Text);
 If StrToInt(Edit1.Text) > StrToInt(Edit2.Text) then Showmessage(Edit1.Text + ' is larger than ' + Edit2.Text);
 If StrToInt(Edit1.Text) = StrToInt(Edit2.Text) then Showmessage(Edit1.Text + ' is equal to ' + Edit2.Text);
end;
This is a simple IF THEN structure. If you want a simple IF THEN ELSE structure then change the
source into this:

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
 If StrToInt(Edit1.Text) = StrToInt(Edit2.Text) then Showmessage(Edit1.Text + ' is equal to ' + Edit2.Text) else Showmessage(Edit1.Text + ' is larger or smaller than ' + Edit2.Text);
end;
Now it's time for a FOR NEXT loop. Start again with a new application, go to the component palette,
select a Memo (it's under the standard tab) and place it on the form, also place a button on the form.
To make a FOR NEXT loop you will need a variable that counts for you, in order to use this variable,
you'll need to declare it somewhere in your source code. We will make a private variable, beacause it's
only used as a counter variable in this procedure. You must declare a private variable allways before
the beginning of the procedure, like this:

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
 i: Integer;
begin
To make the FOR NEXT loop working go to the source code of the project by double clicking on the button
and change the code into this:

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
 i: Integer; // This is comment
begin
 For i:=0 to 10 do Memo1.Lines.Add(IntToStr(i));
end;
This program counts from 0 to 10 with variable i, and displays the different values of i in the Memobox.
As you can see you can add a line in a memobox with the Memo.Lines.Add statement, this always has to be
a string, that's why we are transforming the variable i into a string with InToStr.

After the variable declaration you can see I added some text, this is a comment line, in Delphi there
are two different ways to add comment to your program the first one is this:

Code:
// This is comment
and the second one is this:

Code:
{ This is comment }
Why use comment? If you are writing very large and complex programs it's wise to use comment, because
if you have written the source and after a couple of days you are working again on your project it's
much easier to read the comment and understand the source than going thru the source over and over
again to understand it, BTW this is not only for yourself but also for others if you are working in
a team.

Now it's time for the CASE statement. With a large amount of data we don't want to use the IF THEN statement
over and over again, therefore we use the CASE statement. Start with a new application and make sure
you have an editbox and a button on your form. Doubleclick on the button and change the source into this:

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
 case StrToInt(Edit1.Text) of
  1 : Showmessage('This is ONE');
  2 : Showmessage('This is TWO');
  3 : Showmessage('This is THREE');
  4 : Showmessage('This is FOUR');
  5 : Showmessage('This is FIVE');
 end;
end;
When you run the program, fill in the Editbox with a value between one and five and press the button,
the program will show a message with the number in capital letters. What the program does is this, it
transforms the input from Edit1 into an integer value and checks which value it is. When the user value
and the value in the source are equal to each other, the program will display the right message.

Enough for today I'm going downstairs to make myself a lunch. I hope you'll get a little furter with
delphi by using this tutorial. After the weekend I'll write another one so you can read it next week.