"Hello, World !"
A Classic Approach to Begin the Lesson

 

 

Welcome !

Start your Borland Pascal 7.0 first. Feel it. Type some words then delete it. Once you will get used to it. Let's begin our lesson! Oh, by the way, you may download the entire lesson, chapter 1 to 14, in here, just a mere 83KB (compressed size) complete with source codes and examples.

Pascal is a structured programming language. It means that everything you program, must be structured and in orderly. No more free gotos and jumps. It has a format. Yeah... right. You don't have to obey it anyway since you COULD extend it to complex ones.

A format of very simple Pascal programs :


uses ....

var
...

begin
  .... (Your program is here)
end.

The word end here should end with stop ('.'). Every Pascal program blocks begins with the word begin and ends with the word end. Some of the end word ends with semicolon ';' or period '.' or just nothing. That will be described later (not in this lesson).

For example :
Type in these words in your BP, and run it by pressing Ctrl+F9


begin
  Writeln('Hello, World !');
end.

In this case we don't need the word uses and var. Try to run it. It should display 'Hello, World !' on screen. But perhaps it's too fast. But, how can I view it anyway ? Press Alt+F5.

What is the word uses for ? It's a kind of terms to declare that we use specific library. Some are standard ones. At first, we use the standard ones that come with every Pascal compiler. The one we will use most is crt unit, since it contains various commands that is suitable for us for this moment.

What is Writeln for ? Writeln is a command to write something into the screen. If you want to write a sentence, just wrap it with quote ('). And don't forget the pair of brackets and the semicolon. Example :


begin
  Writeln('I learn Pascal');
  Writeln('Hi, there !');
end.

You will see another messages on screen. View it with Alt+F5 after running it by pressing Ctrl+F9.

So, you want to clear the screen when the program starts. You need to add the word Clrscr; just after the word begin. But it needs uses crt since Clrscr is one of the commands included in crt library. (Pascal terms for library is unit) So, the above example would become :


uses crt;

begin
  Clrscr;
  Writeln('I learn Pascal');
  Writeln('Hi, there !');
end.

Try and run it !

Pascal has a command Write instead of Writeln. Why don't you just try that ? Just replace all the Writeln into Write (of the program above). Run it and see what happens. See the difference ?

It'll output I learn PascalHi, there !. Stick !

Sometime, we need to leave a line blank. Suppose we want a blank line between I learn Pascal and Hi, there !. Just insert Writeln; between, and bingo ! Try inserting the Writeln in correct order so it yields :


I learn Pascal

Hi, there !

If you do have a good grip about how to write something on screen, please proceed. Otherwise, you should repeat it once again.

Now the input parts.

When we ask computer to receive input from users, we need something to store the input before it is being processed. That was variable. We need it to corporate our program. There are various types of variable and with various range too. Here are some frequently used variable types :

Pascal Variable Name, Range, and Type
Type nameRangeType
Shortint-128 to +127integer
Byte0 to 255integer
Integer-32768 to +32767integer
Word0 to 65535integer
Longint-2146473648 to +2146473647integer
Real-??????? to +???????fractional
Stringup to 255 lettersnon-numeric
Char1 letter onlynon-numeric

There are many more, but that's all we need for now.

How to declare our variables ? As we seen in our format previously that we have var section there. That's where we suppose to declare our variables.
Here is some example :


var
  MyAge    : Byte;
  Comments : String;

To assign variables with values, you need to write this syntax :

    var_name := value;

This should be written anywhere within begin ... end block, in appropriate location.

Example :


var
  MyAge    : Byte;
  Comments : String;
begin
  MyAge:=19;
  Comments:='Hi, there ! I'm learning Pascal';
  Writeln('I am ',MyAge,' years old');
  Writeln(Comments);
end.

Write it down, run it an view it on screen !

How to get the user's input ? Yeah, that's easy actually. Similar to Write or Writeln, Pascal uses Read and Readln. The difference is that in Read or Readln, we cannot write anything on screen but to get user's input.

Example :


var
  MyAge    : Byte;
  Comments : String;
begin
  Write('How old are you ? '); Readln(MyAge);
  Write('Give us comments: '); Readln(Comments);
  Writeln;
  Writeln('I am ',MyAge,' years old');
  Writeln(Comments);
end.

Write it down, run it an view it on screen !

Now, you know how to get user's input. But wait ! If I enter 23.5 in 'How old are you ?' it spouts error ! Yeah, you right ! The value on that question is actually stored in MyAge. MyAge is a Byte variable. So it only accept integer values between 0 to 255. That's the range of it ! It cannot go further. If you want computer to accept fractional values in MyAge, try using Real instead of Byte.

But why when I change it to Real, it outputs erratical numbers ? No, it actually doesn't throw garbage on the screen. It just exponential numbers. For ordinary people -- non technical -- it would be more convenient to view fractional number at 3 or 4 place after the decimal point. Try changing :

  Writeln('I am ',MyAge,' years old');

to :

Writeln('I am ',MyAge:2:4,' years old');

What does it mean ? Why some :2:4 attached to MyAge ? For real numbers, it means : Show 2 place before decimal point and 4 place after decimal point So, when we enter 23.5 for MyAge, it will output :

I am 23.5000 years old

Try changing :2:4 with other values. Be creative !

Can we apply it to the string ? Nope ! But it only accepts just :x. It means that 'I only grant this variable x place on the screen to express its value'. Suppose you change :

  Writeln(Comments);

to

  Writeln(Comments:5);

Try writing long comments when the program is running. See what happens. If you enter Programming as the Comment, you will see output :

Progr

Ok ! I'm tired of pressing Alt+F5 to view this tiresome tutorial result ! No problem. Just add Readln; just before end. Try it yourself !

That's all for this time. Refer to the quiz to evaluate your understanding. Any questions ? Write to me.


Now, select your way
Back to main page
Back to Pascal Tutorial Lesson 1 contents
To the quiz
To Chapter 2, about exploring crt units
My page of programming link


By : Roby Joehanes, © 1996, 2000