< Pascal Programming

Here is a basic program that does absolutely nothing:

program first;
begin
   (*comment*)
end.

Here is a line by line description of what the different lines do.

  1. program first; - is the "program header". Program headers are for dealing with multiple programs and "units". For most dialects this is optional.
  2. begin - tells the compiler where the program begins. In more complex programs this statement might be preceded by statements that declare variables, set up functions and handle other preparation before the main program begins.
  3. (*comment*) - is a comment that is ignored by the compiler. The (* and *) tell the compiler to ignore what's in between them, almost all dialects of pascal also use braces ( { and }) for the same purpose. In the original Pascal, these can't be embedded like this: (*(*example*)*). However, in almost all dialects that use braces also use precedence, so this works: {(*example*)}. In some dialects such as Free Pascal's default mode comments can be embedded.
  4. end. - (notice the period) tells the compiler to stop compiling. In fact, anything after that is completely ignored. As we shall see, there may be many begin and end statements in a program, but the end statement that terminates the program is the only statement that ends in a period.

While programs that do absolutely nothing may compile, they aren't generally that exciting. It would be much better to have a program that introduced itself to the world. These programs are very popular, especially when introducing a new language, and if you've ever learned another programming language you may be familiar with such "Hello World" examples.

program HelloWorld;
begin (*This is where the program begins*)
   writeln('Hello world!');
end. {This is where the program ends.}

Now instead of doing nothing our program contains one line, namely "writeln('Hello World!');". The command writeln prints its argument to the terminal. In the current case its argument, 'Hello World!' is a string. If you are familiar with other programming languages it would be useful to note that in Pascal strings and characters are contained between single quotes and not double quotes. The last but important thing to notice is the ; at the end of the writeln statement. Almost all statements in Pascal should end with a semicolon. This is because the compiler doesn't care about the white space you put, including line breaks. So our program would have been just as functional as

program HelloWorld;
begin (*This is where the program begins*)
                                  writeln('Hello world!');



end. {This is where the program ends.}

or

program HelloWorld;begin (*This is where the program begins*)writeln('Hello world!');end.{This is where the program ends.}

To the compiler they are exactly the same. But the compiler still needs to know where one statement ends and the next begins. For this reason Pascal uses the semicolon to mark where lines end. The observant reader will notice that we only said that "almost all" lines need to end in a semi-colon. We shall point out the exceptions as they arise, there are three Exceptions in this program. The first exceptions is the line "begin", and the second is the line end. (since this terminates the program it ends in a period instead of a semicolon.). The last exception is that the line that comes before an "end" doesn't need to end in a semicolon. So really in this program the line "writeln('Hello World!');" didn't actually need a semicolon despite all the discussion about it. On the other hand, there is no harm in including the semicolon. It also makes the life easier later if you decide to add a line after the "writeln" command, because then you no don't need to remember to add it later.

Unlike many programming languages the Pascal language is not case sensitive! For example consider the following program:

program HelloWorld;
begin (*This is where the program begins*)
   writeln('Hello world!');
   WriteLn('It is nice to meet you.')
end. {This is where the program ends.}

Here both "writeln" and "WriteLn" refer to the same function. We could have also changed any of the other keywords in the program to as we liked. One of the benefits of ignoring white space and case is that it allows us to structure the program in which ever way is most readable to us.

As a final comment, depending on your operating system and how you start any of these programs you have compiled, you may find that the window closes the instant the program is finished, which for these programs is almost instantly. One useful trick to avoid this is to ask the program to wait for input from the user. This can be done with the "readln" command. So our simple HelloWorld program becomes:

program HelloWorld;
begin (*This is where the program begins*)
   writeln('Hello world!');
   readln;                     {This reads a line of input from the keyboard}
                               {So now the program waits for us to press enter :)}
end. {This is where the program ends.}
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.