This time we'll cover how to write comments in Pascal. Comments help a lot when reading Pascal code. What exactly is a comment?
Anyone learning a programming language meets comments early on. A comment is a block of the program the computer never reads. It works like a note in the margin.
Writing comments isn't mandatory. Even so, comments make the code easier to understand. In later tutorials I'll use them a lot too.
There are several ways to write comments in Pascal. There are single-line comments, and there are multi-line ones.
Single-Line Pascal Comments
In Pascal you write a single-line comment by putting a double slash (//) before the
comment.
So after that double slash, the computer ignores whatever you type.
program HelloWorld; // program title section
uses crt; // declaration
begin // program starts
clrscr; // clear the screen
writeln('Hello World'); // write to the screen
readln; // read a line
end. // program ends
Nice. Type those comments and run the Pascal code. The program still runs fine, because the computer skips everything after the double slash.
Now, as an experiment, delete one of the double slashes in that code. I'll delete the double slash on the first line, for example.
program HelloWorld; bagian judul program
uses crt; // declaration
begin // program starts
clrscr; // clear the screen
writeln('Hello World'); // write to the screen
readln; // read a line
end. // program ends
When you compile it, Free Pascal throws up a red box with an error, because it treats the word "bagian" as an instruction it doesn't recognize.
📷 Illustration: the compilation error dialog in Free Pascal, caused by comment text that is missing its double slash.
Comments also let you disable code without really deleting it, and that's not only useful for notes. Huh, how? Deleting code without deleting it?
The point is this: you remove the code so the computer can't read it, but you as the programmer still can.
program HelloWorld;
uses crt;
begin
clrscr;
// writeln('Hello World');
readln;
end.
If we run that program, the console shows nothing at all, just a black empty screen. The
line writeln('Hello World'); now counts as a comment, so the computer skips it.
📷 Illustration: an empty Free Pascal console window when the program runs with its output line turned into a comment.
Multi-Line Pascal Comments
Comments can also span many lines. You type them between curly braces ({...}) or
star parentheses ((*...*)).
You can use multi-line comments to describe the program.
{
This hello world program prints
the text hello world on the console screen.
}
program HelloWorld;
uses crt;
(* This section can declare
variables, constants, uses, and types. *)
// main program starts
begin
clrscr;
writeln('Hello World');
readln;
end.
Run that code and no error appears, because the computer never reads anything inside the comment markers.
In later tutorials comments will act as labels for the program code. Not writing them is fine, since they never affect the output.
Closing
That's the short tutorial on writing comments in Pascal. Hope it helps :D In the next one we'll cover output in Pascal.