After learning how to create and run Pascal code, this time we'll look at the basic structure of the Pascal language.
Like a letter that runs from the letterhead to the signature, Pascal code has writing rules and a structure it has to satisfy. Let's dig in.
Pascal Language Structure
Structured writing is one of Pascal's strengths. With structure, programmers can read a program easily.
We'll identify the structure of the earlier Hello World program. The code looks roughly like this.
program HelloWorld;
uses crt;
begin
clrscr;
writeln('Hello World');
readln;
end.
1. The Program Title Section
The first part is the program title. A title always sits at the top. Short stories, news, even this tutorial, all have their own title at the top.
Do you know which part is the program title? It seems clear enough.
In Pascal the program title also sits at the very start of the program, in this code:
program HelloWorld;
The program title always starts with the word program. After that word comes the
program title you want, and it ends with a semicolon (;).
program <Judul_Program>;
Writing a program title has a few rules. Break them and the program errors out. The rules are as follows.
- The program title must start with a letter (a-z / A-Z)
- No spaces are allowed inside the program title
- The underscore character can replace a space
- Digits can only appear in the middle or end of the title
The program title is actually optional. Still, it's better to include it.
2. The Declaration Section
Next comes the program declaration. You can think of this section as the Tools and Materials page of a recipe book.
The declaration section tells the computer what we need in order to run the program.
It sits right below the program title and above the begin keyword.
In the Hello World example above, the declaration section is this code.
uses crt;
That code orders the computer to use a "library" called crt. Think of a library as a
"tool" for doing something. We'll do that "something" in the next section.
In the declaration section we can also type the "materials" we need, like constant,
variable, type, procedure, and function.
We'll cover
constantandvariablein the next tutorial, whiletype,procedure, andfunctioncome later in the series.
3. The Main Program Section
The last section, and the most important one. This is where you write the program logic, using all the "tools" and "materials" from the declaration section.
Everything between begin and end is the main program section.
begin
clrscr;
writeln('Hello World');
readln;
end.
Let's take this code from top to bottom.
begin: orders the computer to start the program logic.clrscr: short for Clear Screen. This command clears the screen.clrscris one of the uses of thecrt"tool" from the declaration section.writeln('Hello World'): obvious, right?writemeans write, and ln is short for line. It orders the computer to write whatever sits inside the parentheses, in this case Hello World.readln: as before,readmeans read and ln means line. It orders the computer to read a line. So until you press enter, the computer waits here, and without it the output flashes past in an instant.end: the end of the program, stating that execution is complete. Then the program exits. Note the period (.), a sign used only at the very end of a program.
writelnandreadlnare the commands behind the input and output system. There are alsowriteandread. We'll cover the output system in the next tutorial.
Using the Semicolon
From the program title to the main section, a semicolon always shows up. So what does the sign actually do?
In Pascal a semicolon marks the end of a statement. A statement is code that does one
specific thing, like writing (writeln), reading (readln), or declaring.
"What happens if we drop the semicolon?"
The answer is that the program errors out at once. The computer can only read one block of commands at a time.
Drop the semicolon and the computer reads the first command and the second as a single command.
With the semicolon we can write long code on one line. This is valid code:
program HelloWorld; uses crt;
begin
clrscr; writeln('Hello World'); readln;
end.
In that program clrscr, writeln, and readln are read as 3 separate statements, so no
error occurs. The same holds for the title and declaration sections.
Writing code on one line is strongly discouraged, because it makes the program hard to read.
What's Next
That's all for this one, hope it helps.
For more on Pascal structure, head to the free pascal wiki page or search around this wide internet.
Next we'll cover Writing Comments in Pascal.
See ya 👋