Object Pascal - Introduction

Object Pascal Fundamentals

 

Introduction to Borland Delphi

 

Overview

Borland Delphi offers a practical and easy means of creating computer applications for the Microsoft Windows operating systems. It uses the Object Pascal computer language as its core syntax and programming logic. There are various ways you can launch the program. The most common way consists of clicking.

To start Borland Delphi, click Start -> (All) Programs -> Borland Delphi -> Delphi

Object Pascal Fundamentals

A program or an application in Delphi is called a project. A project in Object Pascal is organized in files. When Delphi starts, it creates a project called Project1. If you want to change the name of the project, you must save it and give the desired name to the project.

Every program in Pascal starts with the program keyword followed by the name of the project. By default, the first project that Delphi creates is called Project1. In order to create your application, Pascal needs to have a foundation that would direct it to how to execute your instructions. Here is an example:

program Project2;

Normally, a program written in the strict Pascal sense is referred to as console. Such a program displays as text in a special window created by the operating system. To specify that you want to create a console application, you should include a line as {$APPTYPE CONSOLE} under the program line. This would be done as follows:

program Project2;

{$APPTYPE CONSOLE}

The fundamental instructions you will give to the computer are in various files that were supplied to you. To use one of these files, you must let Pascal know. This is done with the uses keyword. The uses keyword specifies the list of external files that will be needed to run the program. If you plan to use only one file, simply type its name. For example, the most fundamental file used in an Object Pascal is called SysUtils. You can add it as follows:

program Project2;

{$APPTYPE CONSOLE}

uses
SysUtils;

If you will use more than one file, type them, separated by a comma. The list of uses files must end with a semi-colon.

A program is made of instructions given to the computer. These instructions start with the begin keyword and stop with the end keyword. The whole program (or file) ends with a period. Everything between the (first) begin and the (last) end. is part of the program’s instructions. Eventually, we will learn what types of things can be included between these two keywords.

Practical Learning: Starting a Console Application

  1. To create an application, start Delphi and on the main menu, click File -> New -> Other…
  2. From the New Items dialog box, click Console Application and click OK
  3. To save the application, on the main menu, click File -> Save All
  4. Locate or specify the main folder that will contain your exercises. Then click the Create New Folder button. Type SimpleApplication1 and press Enter twice to display the new folder in the Save In combo box
  5. Replace the name of the project with SimpleApp
  6. Click Save
     
    program SimpleApp;
    
    {$APPTYPE CONSOLE}
    
    uses
        SysUtils;
    
    begin
        { TODO -oUser -cConsole Main : Insert code here }
    
    end.

     

Fundamental Instructions

A procedure is an assignment you ask the program to perform. This assignment is destined to be used by another section of your program. In the future, we will learn how to create procedures. Meanwhile, many procedures have already been created for you. This allows you to simply include them in your application. The fundamental syntax of using a procedure is:

ProcedureName(WhatToDo);

A procedure has a name. We will learn the rules to observe for names used in Object Pascal. A procedure is followed by parentheses. Inside of the parentheses is a special sentence that specifies what the procedure needs in order to carry its assignment. In order to use a procedure in your program, you must “call” it. Calling a procedure consists of typing its name. If the procedure will not use anything inside its parentheses, you do not need to include the parentheses. For example, if a procedure is called Voila and you want to call it, you can just type

Voila;

If the procedure needs something (we will later call it a parameter or argument), then you must type the parentheses and include that “something” in them.

The most fundamental procedure of your programs will consist of displaying something on the screen. This is done with the write procedure. If you just type write, nothing much will happen. If you want to display a sentence on the screen, include that sentence in the parentheses. The sentence must be included between two single quotes. Here is an example:

write(‘Pascal is a wonderful computer language.’);

The sentence you include in single-quotes is called a string.

The second most fundamental procedure you can call in your program is to accept a key pressed by the user from the keyboard. This is done using the readln procedure (readln stands for “Read Line”). This time, because the readln procedure is expecting something from the user, if you do not specify what kind of thing the user should type, the readln procedure will still wait but whatever the user types would be fine.

After writing a program, you should test it. Testing a program is equivalent to executing or running it. To execute a program in Delphi, on the main menu, you can click Run -> Run. The shortcut is F9. You can also click the Run button on the Debug toolbar.

Practical Learning: Using the write and readln Procedures

  1. Change the content of your program as follows:
     
    program SimpleApp;
    
    {$APPTYPE CONSOLE}
    
    uses
        SysUtils;
    
    begin
        { TODO -oUser -cConsole Main : Insert code here }
        write('Press any key to continue...');
        readln;
    end.
  2. To test your program, on the main menu, click Run -> Run. Notice that a DOS window displays
  3. Press Enter to return to Delphi

Data Display Techniques

Object Pascal uses two procedures to perform data display: write and writeln. The write procedure is used to display a string on one line on the console screen. You can use as many write procedures as you want. Each string is added to the end of the previous string, if any:

write('Sydney is in Australia');
write('Programming in Pascal is fun');
write('Press any key to continue...');

The writeln procedure is used to display a string, including an empty string, and then jump to the next line. In other words, after displaying the string, if any, the cursor is moved to the subsequent line. If you use a writeln procedure without a string in the parentheses, an empty line would be displayed. This technique would allow you to segment paragraphs or to display an end of line. For example, in the above code, you can type writeln between any two lines of code to display an empty line. Here is an example:

write('Sydney is in Australia');
writeln;
write('Programming in Pascal is fun');
write('Press any key to continue...');

On the other hand, you can use the writeln procedure to express an end of line to move the cursor to the next line after displaying a string. You can use as many writeln procedures as you need to display strings on the screen. The above three lines could be written:

writeln('Sydney is in Australia');
writeln('Programming in Pascal is fun');
writeln('Press any key to continue...');

This time, each string would display on its own line. You can also use a combination of write and writeln procedures as you see fit. Simply remember that the write procedure is used to display a string on its own line and the writeln procedure displays a string and jumps to the next line. The above three lines could be rewritten as follows:

writeln('Sydney is in Australia');
writeln('Programming in Pascal is fun');
write('Press any key to continue...');

If you want to enclose a word or part of a string between two special characters, simply type the special character on both sides of the word or string. Here is an example:

write('Programming in Pascal is fun <Really Fun>');

If you want to quote a word or a string, you have two alternatives. To quote a word or string with single quotes, type two single quotes on both parts of the word or string. Here are example:

program Project2;

{$APPTYPE CONSOLE}

uses
    SysUtils;

begin
    { TODO -oUser -cConsole Main : Insert code here }
    writeln('William Jefferson ''Bill'' Clinton');
    writeln('Programming in Pascal is fun ''Really Fun''');
    writeln;
    write('Press any key to continue...');
    readln;
end. 

Alternatively, you can use a double-quote to quote a word or portion of a string. To do that, type the double-quote on both parts of the word or string. The above program could be rewritten:

program Project2;

{$APPTYPE CONSOLE}

uses
    SysUtils;

begin
{ TODO -oUser -cConsole Main : Insert code here }
    writeln('William Jefferson "Bill" Clinton');
    writeln('Programming in Pascal is fun "Really Fun"');
    writeln;
    write('Press any key to continue...');
    readln;
end.

Comments

A comment is line or paragraph or text that is considered as part of the code used to create an executable. A comment helps you and other people who read your code to figure out what you were doing. Comments are not read by the compiler while executing your program. This means that you write them in everyday language.

There are three fundamental ways of including comments in your program. To comment the contents of one line, you start it with double forward slashes like this //

Here is an example:

program Project1;

{$APPTYPE CONSOLE}

uses
    SysUtils;

begin
    // Let the user know that the program ends here
    write('Press any key to continue...');
    readln;
end.

You can include many lines of comments in your program. To do that, comment each line with the double slashes. An alternative is to start the beginning of the commented paragraph with (* and end the commented section with *)

Here is another commented version of our program:

program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

begin
    (* If your compiler does not display a prompt message to the user,
       then you should always include the following two lines.
       This displays a prompt message to the user and accepts
       an empty character as the user presses any key *)
    // Let the user know that the program ends here
    write('Press any key to continue...');
    readln;
end.

The (* and *) combination allows you to create as many lines of comments as necessary in a section of the program. Alternatively, you can start the section of code with an opening curly bracket “{“ and end it with a closing curly bracket “}”. The above comment could have been written as:

program Project1;

{$APPTYPE CONSOLE}

uses
    SysUtils;

begin
    { If your compiler does not display a prompt message to the user,
      then you should always include the following two lines.
      This displays a prompt message to the user and accepts
      an empty character as the user presses any key }
    write('Press any key to continue...'); 
    { Stop the program when the user press a key }
    readln;
end.

Object Pascal Help

 

Online Help

There are two main sources of help available for Pascal and Object Pascal. The first source of help is provided with the compiler you are using. This help is mainly electronic. To access online help, while the application is opened, on the main menu, you can click Help and select from the list.

Internet Help

Internet help consists of requesting help from web sites that teach or support the Pascal language. To do this, you can open a search engine site and do a search on Pascal or Object Pascal. You should also be able to find some help on the Borland web site at http://www.borland.com 



Copyright © 2004 FunctionX, Inc Next