main index next page
 

SECTION 1

Introduction

Pascal is an example of a high level computer programming language. There are many other programming languages and they each have their own advantages and disadvantages.

This course aims to teach you the basics of the language. We'll start with some easy examples and work our way up. Don't be afraid to experiment and don't take things at face value; ask questions - it's the best way to learn.

Getting Started

We'll type in a short Pascal program and demonstrate how it works. Pascal recognizes certain instructions and keywords - we'll type these in capital letters, while anything else will be in small letters. Make sure you use spaces and punctuation exactly as shown here in the examples.

Example 1

  • load up your version of Pascal
  • type in this program - use your own name instead of A Programmer!
PROGRAM my_first;        {short demonstration program}
VAR                      {by A Programmer}
  index : INTEGER;
BEGIN
  FOR index := 1 TO 10 DO
  BEGIN
    WRITELN(index)
  END
END.
  • save your program giving it the name my_prog.pas

When you've written a program and typed it in, then the next stage is to compile it. A compiler checks through your program looking for syntax errors - any obvious mistakes that you might have made, or instructions that Pascal can't understand.

If there are syntax errors, you'll need to correct them before going any further. This is sometimes the most difficult part of getting a program to work properly - check spellings and punctuation carefully!

When your program compiles successfully you are ready to run the program and see if it does what it is supposed to...

  • run the program and note what happens

Now for some comments - read them carefully and don't be tempted to skip over this part because it will help you understand what's coming next. We'll look at each line in turn:

PROGRAM my_first;        {short demonstration program}

All Pascal programs start with the keyword PROGRAM followed by a name and then a semicolon. The rest of this line is enclosed by curly brackets. Anything in curly brackets is ignored by the computer - the reason it is there, though, is to act as a comment and to help the reader see what each part of the program does. When you write your own programs you should always include such comments.

VAR                      {by A Programmer}

VAR is short for variable. If your program uses any numbers - or variables - then this is where we say what they are called and what sort of variables they are. As before, the comment in curly brackets is ignored by the computer.

  index : INTEGER;

...our program uses just one variable. It is called index and it is a whole number, or integer.

Now we come to the start of the main program. It begins, surprisingly enough, with...

BEGIN

Notice that the lines are now set in a little way from the left margin. This is called indenting and helps to make the program easier to read.

  FOR index := 1 TO 10 DO

This is the start of what is called a loop. A loop is an instruction (or set of instructions) which is carried out repeatedly - this is exactly what computers are good at!

  BEGIN

...marks the start of the instruction(s) in the loop

    WRITELN(index)

...is the only instruction in the loop. It prints out the value of the variable index. Notice, again, that it has been indented.

  END

...marks the end of the loop and

END.

...marks the end of the program. Note that there is only ever one full stop in a Pascal program, right at the end.

Let's make one or two changes to the program:

  • load my_prog.pas if it isn't already
  • change line 6 to read   FOR index := 1 TO 20 DO
  • compile the program
  • run the program and note what happens
  • change line 8 to read   WRITELN('hello world')
  • compile and run this program
  • change line 8 to read   WRITELN(index, ' times 3 is ', index * 3)
  • compile and run this program

So far, we've seen that a Pascal program is made up of a number of instructions. The file containing these instructions is compiled before the program can be run.

Right now, you shouldn't worry too much if some of this seems a little strange. We'll start with some simple examples and take it from there...

main index next page
Or Click Here!
© 2001 by Mike Hardy