Article...

Jumat, 16 November 2007

Learn Pascal Programming Tutorial Lesson 1 - Introduction to Pascal

About Pascal

The Pascal programming language was created by Niklaus Wirth in 1970. It was named after Blaise Pascal, a famous French Mathematician. It was made as a language to teach programming and to be reliable and efficient. Pascal has since become more than just an academic language and is now used commercially.

What you will need

Before you start learning Pascal, you will need a Pascal compiler. This tutorial uses the Free Pascal Compiler. You can find a list of other Pascal compilers at TheFreeCountry's Pascal compiler list.

Your first program

The first thing to do is to either open your IDE if your compiler comes with one or open a text editor.

We always start a program by typing its name. Type program and the name of the program next to it. We will call our first program "Hello" because it is going to print the words "Hello world" on the screen.

program Hello;

Next we will type begin and end. We are going to type the main body of the program between these 2 keywords. Remember to put the full stop after the end.

program Hello;

begin
end.

The Write command prints words on the screen.

program Hello;

begin
Write('Hello world');
end.

You will see that the "Hello world" is between single quotes. This is because it is what is called a string. All strings must be like this. The semi-colon at the end of the line is a statement separator. You must always remember to put it at the end of the line.

The Readln command will now be used to wait for the user to press enter before ending the program.

program Hello;

begin
Write('Hello world');
Readln;
end.

You must now save your program as hello.pas.

Compiling

Our first program is now ready to be compiled. When you compile a program, the compiler reads your source code and turns it into an executable file. If you are using an IDE then pressing CTRL+F9 is usually used to compile and run the program. If you are compiling from the command line with Free Pascal then enter the following:

fpc hello.pas

If you get any errors when you compile it then you must go over this lesson again to find out where you made them. IDE users will find that their programs compile and run at the same time. Command line users must type the name of the program in at the command prompt to run it.

You should see the words "Hello world" when you run your program and pressing enter will exit the program. Congratulations! You have just made your first Pascal program.

More commands

Writeln is just like Write except that it moves the cursor onto the next line after it has printed the words. Here is a program that will print "Hello" and then "world" on the next line:

program Hello;

begin
Writeln('Hello');
Write('world');
Readln;
end.

If you want to skip a line then just use Writeln by itself without any brackets.

Using commands from units

The commands that are built into your Pascal compiler are very basic and we will need a few more. Units can be included in a program to give you access to more commands. The crt unit is one of the most useful. The ClrScr command in the crt unit clears the screen. Here is how you use it:

program Hello;

uses
crt;

begin
ClrScr;
Write('Hello world');
Readln;
end.

Comments

Comments are things that are used to explain what parts of a program do. Comments are ignored by the compiler and are only there for the people who use the source code. Comments must be put between curly brackets. You should always have a comment at the top of your program to say what it does as well as comments for any code that is difficult to understand. Here is an example of how to comment the program we just made:

{This program will clear the screen, print "Hello world" and wait for the user to press enter.}

program Hello;

uses
crt;

begin
ClrScr;{Clears the screen}
Write('Hello world');{Prints "Hello world"}
Readln;{Waits for the user to press enter}
end.

Indentation

You will notice that I have put 3 spaces in front of some of the commands. This is called indentation and it is used to make a program easier to read. A lot of beginners do not understand the reason for indentation and don't use it but when we start making longer, more complex programs, you will understand.

My Headlines