|
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. |
|
|
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
No comments:
Post a Comment