Sunday, 16 May 2010

Repeat Statement in Delphi ( Delphi Tutorial # 12 )

The syntax of repeat statement is :

  repeat

    first statement

    .

    .

    last statement

  until boolean expression

The first statement until the last statement will be executed once. If the boolean expression is false, all statements will be executed again until the boolean expression is true.

Example 1 ( writing numbers from 1 to 10 ) :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

var
  number : integer;
begin
  number := 1;
  repeat
   writeln(number);
   number := number + 1;
  until number = 11;
  readln;
end.

Example 2 ( adding numbers from 1 to 100 ) :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

var
  number, sum : integer;
begin
  number := 1;
  sum := 0;
  repeat
   sum := sum + number;
   number := number + 1;
  until number = 101;
  writeln(sum);
  readln;
end.

Example 3 ( adding odd numbers from 1 to 10 ) :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

var
  number, sum : integer;
begin
  number := 1;
  sum := 0;
  repeat
   if Odd(number) then sum := sum + number;
   number := number + 1;
  until number = 101;
  writeln(sum);
  readln;
end.

Example 4 ( adding even numbers from 1 to 100 ) :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

var
  number, sum : integer;
begin
  number := 1;
  sum := 0;
  repeat
   if not Odd(number) then sum := sum + number;
   number := number + 1;
  until number = 101;
  writeln(sum);
  readln;
end.

You can download the complete code here.

Go to Previous Tutorial or Next Tutorial

Looping Statement in Delphi ( Delphi Tutorial # 11 )

Looping statement is used for repeating a process. For example, if we want to write numbers from 1 to 100, we can use looping statement instead of writing a hundreds statements. In Pascal, there are three looping statements :

-         for  statement

-         repeat  statement

-         while statement

We have break statement and continue statement related to repetition process.

For statement

The syntax of for statement :

-         for counter :=  first value to last value do

-         for counter :=  first value downto last value do

Counter is an ordinal type variable (see Delphi Tutorial # 3 : Identifier and Data Type). First value and last value are expressions which data type is the same as counter’s data type. Counter’s value will change from the first value to the last value. And in every step of counter, the statement will be executed.

Example 1 :

program Project1;
{$APPTYPE CONSOLE}
uses
  SysUtils;

var
  number : integer;
  letter : char;
begin
  for number:=1 to 5 do
   writeln(number);
  for letter:='A' to 'E' do
   writeln(letter);
  for number:=5 downto 1 do
   writeln(number);
  for letter:='E' downto 'A' do
   writeln(letter);
  readln;
end.

Example 2 (adding numbers between 1 to 100) :

program Project1;
{$APPTYPE CONSOLE}
uses
  SysUtils; 

var
  i, sum : integer;
begin
  sum := 0;
  for i:=1 to 100 do
   begin
    sum := sum + i;
   end;
 writeln(sum);
 readln;
end.

Example 3 (adding odd numbers between 1 to 100) :

program Project1;
{$APPTYPE CONSOLE}
uses
  SysUtils;

var
  i, sum : integer;
begin
  sum := 0;
  for i:=1 to 100 do
   begin
    if Odd(i) then
     sum := sum + i;
   end;
  writeln(sum);
  readln;
end.

Example 4 (adding even numbers between 1 to 100) :

program Project1;
{$APPTYPE CONSOLE}
uses
  SysUtils;

var
  i, sum : integer;
begin
  sum := 0;
  for i:=1 to 100 do
   begin
     if not Odd(i) then
       sum := sum + i;
   end;
  writeln(sum);
  readln;
end.

You can download the complete code here

Go to Previous Tutorial or Next Tutorial

Multiple Conditional Statements in Delphi ( Delphi Tutorial # 10 )

You can use if statement for multiple conditional statement but it will be little bit messy.

For example :

program multiple_cond_if;
{$APPTYPE CONSOLE}
uses
 SysUtils;

var
 mark : integer;
begin
 write('Your mark : ');
 readln(mark);
 write('Your grade is ');
 if mark >= 90 then
  writeln('A')
 else
  if mark >= 70 then
   writeln('B')
  else
   if mark >= 60 then
    writeln('C')
   else
    if mark >= 40 then
     writeln('D')
    else
     writeln('E');
 readln;
end.

Instead of using if … then statement, it will be easier using case … of statement :

program multiple_cond_case;
{$APPTYPE CONSOLE}
uses
 SysUtils;

var
 mark : integer;
begin
 write('Your mark : ');
 readln(mark);
 write('Your grade is ');
 case mark of
  0..39   : writeln('E');
  40..59  : writeln('D');
  60..69  : writeln('C');
  70..89  : writeln('B');
  90..100 : writeln('A');
 end;
readln;
end.

Download the complete code here.

Go to Previous Tutorial or Next Tutorial

Thursday, 13 May 2010

Using DLL file in Delphi

After we created a DLL file in the previous tutorial, now we’re going to use this DLL file in an application. First, you have to create a user interface like picture below :

Or you can download this project here.

Before you write some codes in this project, you have to make sure that the DLL file you’re going to use and your project are in the same directory (folder). Or you can put the DLL file you’re going to use in directory : c:\windows\system.

To use functions in a DLL file we’ve created, you must type the function after {$R *.DFM}

{$R *.DFM}
{ place functions from DLL here }
function area(length, width : integer): integer; stdcall; external 'area_volume.dll';
function volume(length, width, height : integer): integer; stdcall; external 'area_volume.dll';

external clause means that the functions we use (area and volume) are in an external file (area_volume.dll)

To call area function and volume function see the codes below :

procedure TForm1.CalculateAreaButtonClick(Sender: TObject);
var
  L, W : integer;
begin
  L := StrToInt(Edit1.Text);
  W := StrToInt(Edit2.Text);
  Label8.Caption := Label8.Caption + ' ' + IntToStr(area(L,W));
end;

procedure TForm1.CalculateVolumeButtonClick(Sender: TObject);
var
  L, W, H : integer;
begin
  L := StrToInt(Edit3.Text);
  W := StrToInt(Edit4.Text);
  H := StrToInt(Edit5.Text);
  Label9.Caption := Label9.Caption + ' ' + IntToStr(volume(L,W,H));
end;

area(L,W) and volume(L,W,H) will return integer type values, so if you want to put these values into label.caption, you have to convert them into string using IntToStr() procedure.

You can download the complete code here.


if Statement in Delphi ( Delphi Tutorial # 9 )

There are two kinds of conditional statement : if statement and case statement. This both statements are for making decisions that involve two or more alternative choices or conditions.

In this tutorial, we will learn about if statement. There are two kinds of if statement :

-         if ….. then

-         if ….. then …. else

The syntax of if …. then statement is :

     If boolean expression then statement;

Boolean expression can be true or false. If it is true, computer will execute the statement. If it is false, computer will not execute the statement.

The syntax of if …. then …. else statement is :

     If boolean expression then first_statement else second_statement;

If boolean expression is true, computer will execute the first statement. If it is false, computer will execute the second statement.

The flowchart of if statement is below :

Example for if …. then statement :


program Project1;

{$APPTYPE CONSOLE}

uses SysUtils;

var

 score : integer;

begin

  write('Your score : ');

  readln(score);

  if score < 60 then writeln('You did not pass the exam');

  if score >= 60 then writeln('You passed the exam');

  readln;

end.


Example for if …. then …. else statement :


program Project2;

{$APPTYPE CONSOLE}

uses

  SysUtils;

var

 score : integer;

begin

  write('Your score : ');

  readln(score);

  if score < 60 then

    writeln('You did not pass the exam')

   else

    writeln('You passed the exam');

  readln;

end.

 

Both projects above will give you the same result.

Go to Previous Tutorial or Next Tutorial

Tuesday, 11 May 2010

Creating DLL file using Delphi

There are two kinds of  executable file in Windows, EXE file and DLL file. DLL (Dynamic Link Library) is an executable file with some procedures or functions in it. These procedures or functions can be used by some applications at the same time. Because, once DLL is loaded to memory, every program or application can use it. The other advantage of using DLL is our application becomes modular. It will be easier to change the application by replacing the DLL.

To create a DLL file, choose File – New – DLL. And you’ll get some codes below :


library Project1;

{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }

uses

  SysUtils,

  Classes;

{$R *.RES}

begin

end.


You can delete the unnecessary comment after library clause and add exports clause before begin….end block.

One thing you have to concern about is that all variables in DLL are private. Your application and DLL can not use the variables at the same time. You have to send values of the variables and receive a result.

Now we’re going to insert some functions into DLL :


library area_volume;

uses

  SysUtils,

  Classes;

{$R *.RES}

function area(length, width : integer): integer; stdcall;

begin

 area := length*width;

end;

function volume(length, width, height : integer): integer; stdcall;

begin

 volume := length*width*height;

end;

exports

 area, volume;

begin

end.


You can download this source code here.

Now save the project as area_volume.dpr. Remember that the project name and the DLL name have to be the same, in this case we name it area_volume. Then build the DLL file by choosing Project – Build … And you’ll get area_volume.dll file. Then you can create an application using this DLL.


Monday, 10 May 2010

Square Root and Exponential Function in Delphi ( Delphi Tutorial # 8 )

Sqrt() function

In this tutorial we’re going to write some mathematic formulas in Delphi. The first formula is :

To write the above formula in Delphi, we use sqrt() function :

Sqrt((b*b-4*a*c)/(2*a))

Exp() function

There’s no power operator in Pascal Object. But it does not mean that we can not calculate the formula above. Before we calculate the formula, we have to describe it into an exponential function :

So we can write the formula using exp() function and ln() function :

                                                   Exp(b*ln(a))

Example :

program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;

var
x, y : real;
a, b, c : integer;

begin
  // Insert user code here
  a := 2;
  b := 5;
  c := 3;
  x := sqrt((b*b-4*a*c)/(2*a));
  writeln(x:0:1);
  y := exp(b*ln(a));
  writeln(y:0:0);
  readln;
end.

Go to Previous Tutorial or Next Tutorial

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.