Sign Guestbook
View Guestbook
Invite Your Friends
Website Awards
Message Forum
Source Codes
Downloads
Links
About Me
 
     
Lesson 1 - The first few steps in Pascal Programming
Lesson 2 - Variables, Constants and the Assignment  Statement
Lesson 3 - Special Functions: ClrScr(), GotoXy(), etc...
Lesson 4 - Program Control
Lesson 5 - The CASE-OF Statement
Lesson 6 - Logical Operators and Boolean Expressions
Lesson 7 - Procedures and Functions
Lesson 8 - BGI Graphics
Lesson 9 - File Handling
Lesson 10 - Arrays
Lesson 11 - Record Data Structure
Lesson 12 - Strings

Add this site to your Favourites

   



 


Lesson 1
- The First Few steps in Pascal Programming

Programming Basics

In a program, you must always obey the rules of the language, in our case, the Pascal language. A natural language has its own grammar rules, spelling and sentence construction. The Pascal programming language is a high level language that has its own syntax rules and grammar rules. As you go along with the lessons, you must note what you can do and what you cannot do in writing a Pascal program. A very simple program is shown below: 

Program Lesson1_Program1;   
Begin    
 Write('Hello World. Prepare to learn PASCAL!!');
 Readln;   
End.

The program is written only to display the message : 'Hello World. Prepare to learn PASCAL!!' - an introductory message that is displayed to you whenever you are going to learn a new programming language. This is simply shown on the screen. So, to display any message on the screen, you should use 'write' (or 'writeln'). The 'readln' statement, here is used as to 'stop' the program and wait until the user presses enter. If the 'readln' statement is missing in this program, then the message is displayed on the screen without giving any chance for the user to read it and obviously halts! Try running this program with and without the 'readln' statement and notice the difference. I suggest you see it!! Now, look at this:

Program Lesson1_Program2;begin 
Write('Hello World. Prepare to learn PASCAL!!');Readln;End.

This program also runs perfectly as the previous one. The only difference is: neatness and friendliness. 

This first program is, what is commonly referred to in programming, as 'indented'. Indentation is a must in writing programs as it aids in the way the code is written ie. neater. Indentation also helps with debugging and code presentation. You will note how I indent programs.

A program in Pascal always starts by the reserved word 'Program' following the title of the program. There are various restrictions on how to write this statement. Below is a simple example of a small program. (Remember: you can copy and paste the program in a text file, save the text file as filename.pas and open it with Turbo Pascal. The .pas extension is required.)

In the following program, the computer must prompt the user to enter a number, then the latter is added to the second number input by the user.

Program Lesson1_Program3;
Var       
    Num1, Num2, Sum : Integer;

Begin {no semicolon}
 Write('Input number 1:'); 
 Readln(Num1);
 Writeln('Input number 2:');
 Readln(Num2);
 Sum := Num1 + Num2; {addition} 
 Writeln(Sum);
 Readln;
End.

Now we must take a look at the program. A program in Pascal starts with the reserved word 'Program' (although it is not explicitly required) and ends with 'End', following a full stop (this is required though). A full-stop is never used within the program, except when dealing with records (later topics) and at the end of the program as seen in the example above.

The 'Var' statement, is used to introduce any suitable variables which will be used later in the program. These variables are non-constant terms so that they are used in the program for storing values. The terms 'Num1', 'Num2' and 'Sum' in the program are the variables which store any numbers, except those which are real (in fact, during the execution of the program, a runtime error may occur if a decimal number is input). As you can see in the example above, these variables are assigned to as integers. The term 'integer' means any whole number, i.e. a number which is not a decimal number but a positive or negative number. The integer type ranges from -32768 to 32767. So values which are not within the specified range cannot be stored by an integer type. There are other types which are wider in range, but for now the integer type is enough to hold up our values. The variables 'Num1', 'Num2' and 'Sum' are terms which are not reserved words, but can be used as variables in the program to store data in them. They could be changed more than once. Moreover, I could have used 'number1', 'number2' and 'totalsum' (note that there must be no spaces within the variables), instead of 'Num1', 'Num2' and 'Sum', respectively. As you can see, it is much better to shorten the variables than writing long words, such as 'variable_number1'.

After declaring all the variables which are required to be used later in the program, the main program always starts with the reserved word 'Begin'. Without this word, the compiler will display a diagnostic (error message). In the program above, both of the two types of 'write' are used. These are 'write' and 'writeln'. Both has the same function, except that the 'write' function, does not proceed to the following line when writing a statement. If you run this program, you will notice the difference between them. When using these two terms, any message that will be typed in between the brackets and the inverted commas '(' ')', is displayed on the screen. However, if a variable is used instead of a message, without using the inverted commas, the CPU will display the stored variable in the memory, on the screen. In line 9, the CPU will not display 'Sum' on the screen, but the stored number in the memory. Another important thing which must be noticed is the semi-colon (;). The semicolon is used after each statement in the program, except those that you will learn later. However, in the example above, there isn't a semicolon after a 'begin' statement. This is because the flow of the program has just started and must not be stopped by a ';'.

The messages in between the braces ({ }) are called comments or in-line documentation. I guess you consider the comments to be 'extra'. Very long programs which include thousands of lines, have already been felt in need of describing certain functions or even complicated functions. In my experiences, I have already met many problems, when refusing to write a program for a long time, and then resuming again writing it! I've made a long time trying to understand what I have done. You must keep it into your mind that comments within the braces are not read or compiled by the compiler/interpreter.

The 'readln' statement is another reserved word for input purposes, which enables the user to input a number or text only i.e.: using the keyboard. But in our case the 'readln' statement is used to input numbers only (letters are accepted but will cause a run-time error because it is not the input we want) and store them in the variables 'Num1' and 'Num2'. This is because both variables are assigned to as integers, and integer variables do not store strings. A run-time error is detected by the OS (Operating System; ex. Windows or Linux) if something goes wrong with the input. Later in the course, you will also learn how to control input and output exceptions - unexpected runtime errors. One last thing on errors is this: there are 2 major error types which are - Runtime Errors and Compilation Errors. Runtime errors are those which occur unexpectedly during the execution of the program, whereas a Compilation error is one which is detected during the compilation process. Note that a decimal number is also considered as a wrong input; a decimal number must not be input, since it is a real number (more on this later).

After the prompts and inputs by the user, follows the addition. i.e. 

Sum := Num1 + Num2;

The result of the above statement is the addition of the values stored in variables 'Num1' and 'Num2'. The important thing that you should know is that one cannot make the same statement as follows:

Num1 + Num2 := Sum;

This is another syntax error. It is the fact that transfer of information is from left to right and not from right to left. So, mind not to make this error. The ':=' is called the assignment statement, and should be discussed later on.

 
 


User Comments

Post By: slecet Posted On: 2009-11-18
Thank you... :)

Post By: Mynick Posted On: 2009-10-23
Thank you very much :)

Post By: greyshirt Posted On: 2009-09-24
what is the genesis of computer programing

Post By: ashamide Posted On: 2009-08-21
Nice lesson,very simple and straight forward...more power to your elbow!

Post By: cathylee Posted On: 2009-08-17
This is awesome,thanks for the great work guys.I really appreciate it.I'm a first user of pascal and I'm loving this already. thank you

Post By: Mina Fouad Posted On: 2009-07-01
Nice language I like it

Post By: doro Posted On: 2009-06-16
this is welldone,exelent work

Post By: Kelseyer$$ SALATION Posted On: 2009-06-15
NO COMMENT...joke it's great thanks for he sample tutorial post more sample......NO COMMENT.

Post By: ASL TAONG GRASSA Posted On: 2009-06-15
pos more tutorial keep up the good work "we Rock". Hi 2 all emo-punk and punksters....

Post By: meroolover Posted On: 2009-06-01
not good article i know much more

Post By: Ashanthi Posted On: 2009-05-31
This is a very good article,very helpful to the beginers.

Post By: nasky Posted On: 2009-05-25
This is a very interesting site to learn Pascal from and i will be delighted if you can send the books/manuals u use to my mail.Thank you.

Post By: mariaC0610 Posted On: 2009-05-25
I am starting pascal programmin in few months in school and i've already know programmin in Visual Basic....i think this 2 languages are very similar and i find this as the perfect way to learn....! thanks

Post By: Muhedin Abdullahi Mohamed Posted On: 2009-05-16
this is really a usefull site, but I see it may be better to organise the lessons more than it is; for example giving a detailed explanation about a particular structure of the language followed by a number of examples increasing in default the simplest one first,then the second simplest......and lastly the most deficult one.....that is my poor point of view.

Post By: Boz Posted On: 2009-05-06
Cheers man dat was sick now I got da ICT in da bag u guys r legends

Post By: xerox009 Posted On: 2009-04-27
this lesson is very thrilling, simpl self explanatory

Post By: Kurting(-_-) Posted On: 2009-03-31
astig yung site na to!

Post By: markie Posted On: 2009-03-11
interesting site...vgood!!! hi mines...

Post By: mines Posted On: 2009-03-11
nice site...i like it...but the color blue is not good for me...hehe.. Hi taming..

Post By: taming Posted On: 2009-03-11
simple and cool!!! hi vic...

Post By: vic Posted On: 2009-03-09
very nice tutorial of pascal...i like it...impressive!!!

Post By: Daqcenzo Posted On: 2009-03-03
Hot and cool at same tym. Lyk y offs et said,u don try to be everything one tym ,u just stick to explain what on hand.Real great work,I appreciate.Thank u

Post By: rotich Posted On: 2009-02-26
It really make sense. Plaese proceed. Thankz.

Post By: JazzMan^ Posted On: 2009-02-21
Realy good. Its very easy to understand about what the talk is going on (sry for bad english)

Post By: morash Posted On: 2009-01-23
actually the lesson was very nice..i appreciate that........thank you alot

Post By: alaka ti. Posted On: 2009-01-20
thank you for the lesson

Post By: jerrry Posted On: 2008-12-24
now i know it's great to learn pascal.. sir... can pascal create a program from games

Post By: grace Posted On: 2008-12-12
i really understand the way it explain...

Post By: Abraham Stephen - ABnice Posted On: 2008-12-10
You really touching lives. Continue and do it unconditionally. Thanks Great.

Post By: Benoua Mohamed Posted On: 2008-11-26
THANKS FOR THIS LESSONS , GREAT JOB

Post By: PC Doctor Posted On: 2008-11-23
Wonderful thing you are doing, thank you...great site

Post By: Anadya Posted On: 2008-11-02
good site

Post By: Cool Posted On: 2008-10-30
You sir are one good teacher :))

Post By: Arsh Posted On: 2008-10-22
How come does it say "Resource file icon not found" or some like that.

Post By: valyR Posted On: 2008-10-16
very good. Thank you I use lazarus a free GUI ide and it's ok. Pascal is a very good programing language - i'm sorry because Borland doesn''t dupport it but i think it will ressurect :)

Post By: valyR Posted On: 2008-10-16
very good. Thank you I belive pascal is a very good programing language - i use now lazarus a free delphi IDE and it's ok.

Post By: bago Posted On: 2008-10-03
It doesent work for me..an error 85 shows when I want to start a program. Please help

Post By: Mathieu Bizu Posted On: 2008-09-20
great work,thanks.

Post By: MR gibbs student Posted On: 2008-09-19
AWESOMETASTIC SITE THANKS ;)

Post By: ngalande emmanuel Posted On: 2008-09-16
this is very impressing

Post By: Dee Posted On: 2008-09-14
Sir I really liked your notes. Is it possible for you to add some tutorial exercises after each lesson. Thank you so much.

Post By: Jak Posted On: 2008-09-03
Thanks, i like it. Very easy to understand.

Post By: cZARJAN Posted On: 2008-07-22
I like very much! It helps many students1

Post By: DOCTOR Posted On: 2008-07-10
Great idea . Many need such help today .

Post By: tonguetiedfaberdrive Posted On: 2008-07-08
i hope you could edit your site and give a service of answering some programming languages problems?

Post By: DIDDY Posted On: 2008-06-17
wow?that was one nice lesson but i think there should be much of the statements e.g FOR,IF,CASE,DO E.T.C

Post By: GayanG Posted On: 2008-06-08
Hi ! i`m Gayan pradeep,21 years old,higher student from Sri lanka. I think that pascal was very interest program in my education life. so learning pascal will be very proud to me.

Post By: stephan Posted On: 2008-04-29
I can't run it..

Post By: Chris Posted On: 2008-04-26
Have to get ready for work shortly, this course helps me with my spare time.

Post By: Chris Posted On: 2008-04-26
Unfortunately I'm still a newbie at programming but so far I like what I see.

Post By: BONAVENTURE Posted On: 2008-04-20
Master, you are doing a very good job, please keep it up!!!!

Post By: Posted On: 2008-04-20
Your Comment

Post By: Posted On: 2008-04-03
Your Comment

Post By: Posted On: 2008-03-24
Your Comment

Post By: gochi Posted On: 2008-02-28
it is to hard

Post By: George Posted On: 2008-02-21
Your Comment I almost drop the course 'programming in Pascal' untill I met ur site. Thanks a million. I now find the course interesting. My lecturer is amazed at my improvement

Post By: cliffy-nx1 Posted On: 2008-02-20
you guys are the boom

Post By: Posted On: 2008-02-18
Your Comment

Post By: pablo2times Posted On: 2008-02-12
thanks a lot for the elaboration. i could not understand what my lecturer was giving me at college until i met you. please keep up. I'm very grateful

Post By: jughead Posted On: 2007-12-29
I've been wanting to learn programming for a long time but felt I was in over my head with C. So far this tutorial is very clear and easy to understand and has similarities with what I've seen of C. As for the IDE it's brilliant, the perfect way to learn programming, rather than trying to learn how to program and how to pass code to the compiler

Post By: CJ Posted On: 2007-12-27
hi i am a total noob in programming and i have a question. how do you actually run the programm u wrote?

Post By: histevenk Posted On: 2007-12-13
FANTASTIC!!!

Post By: MGGM Posted On: 2007-11-10
I ran the animate in my windows and it zoomed.... thanks I love to cut and paste... haha

Post By: Jimi Posted On: 2007-10-24
I like this weather!

Post By: Funskin Posted On: 2007-10-15
hello!!!have a nige day

Post By: aLex Posted On: 2007-10-14
helolo",>

Post By: mahal Posted On: 2007-10-08
wala lng

Post By: tal2 Posted On: 2007-10-08
elow!!!

Post By: alex Posted On: 2007-10-08
hahahaha. . . ewan q. . . ",>

Post By: ALPHA Posted On: 2007-09-13
This is great as I using Pascal in my Exams, thanks

Post By: hobbit Posted On: 2007-09-12
Sweet This is just like SCAR Well it should be since SCAR is based off Pascal Thanks!

Post By: leonard Posted On: 2007-08-28
nested for loop by plusing.

Post By: y offs et Posted On: 2007-07-05
THIS IS FOR BEGINNERS!! Distracting references to various pascal dialects are kept to a minimum. The most complete, easy to comprehend tutorial I have ever found. I really like the way you "stick to the program" and don't try to be all things to all people.

Post By: manisekaran Posted On: 2007-06-29
Please send me free PASCAL tutorials

Post By: Darky Posted On: 2007-06-26
Thanks 4 the site man .. it's just awesome . keep up the good work ;)

Post By: Darky Posted On: 2007-06-26
Thanks 4 the site man .. it's just awesome . keep up the good work ;)

Post By: farnoosh momeni Posted On: 2007-06-06
this site is an excellent site.

Post By: Posted On: 2007-05-29
Your Comment

Post By: gholamreza ghyiasi Posted On: 2007-05-27
this site is very useful to help me please analyze this material

Post By: Posted On: 2007-05-19
Your Comment

Post By: Posted On: 2007-05-16
Your Comment

Post By: Posted On: 2007-03-07
Your Comment

Post By: lucky wickramasinghe-sri lanka Posted On: 2006-12-11
I am so happy!best wishes!i find its really useful

Post By: nicko Posted On: 2006-12-01
gawa nmn kau ng iba ung bagong example para maintindihan k ng maayos

Post By: emankcin Posted On: 2006-11-21
i very very think full to your sight..... c u

Post By: Manisha Posted On: 2006-11-17
Your Comment

Post By: oyekemi Posted On: 2006-11-17
this is great, l really enjoyed it and it very very helpful espercially for new beginnners. thanks a lot.

Post By: abdullah Posted On: 2006-11-07
please I want the advantage and disadvantage in Pascal

Post By: sandu rules Posted On: 2006-10-31
interesting!!!!!!!!!!!!!!!!!!!!!!!!!

Post By: Chic Posted On: 2006-10-07
Thank you for this awsome website, I just started pascal at school so I think it will be helpfull in "times of need"....

Post By: Damian Posted On: 2006-09-24
Thanks man this is a great guide, i made more sence out of this than what my lecturer was telling me!

Post By: Samson Posted On: 2006-07-28
I Love you..!This web is so great!We need to study pascal in school,and now i can manage it well..THANKS~!

Post By: Fernando Solly Posted On: 2006-07-24
I am brazilian,67 years old, engineer and simply for lack of know-how in the basics of programming language I could understand that it is the same as to be absolutely out of the tracks. Now reading your tutorial, I could experiment some examples.It's really wonderful progeramming in PASCAL. My goal is to understand deeply how to develope a simple parser/interpreter/compiler. I would like to specialize in this subject. Thank you for your help Fernando Solly

Post By: Paul T. Gagnon Posted On: 2006-07-12
This helps. I will continue.

Post By: jah/JIBS Posted On: 2006-07-07
i like it, i believe one can do better next. thank you alot.

Post By: Adam Rice Posted On: 2006-06-28
Thanks for this whole thing, I can tell you put a lot of time and effort in and i really appreciate it. ThankYou


Post Your Comment on this Article!
Your Name/Nickname:
Your Email:
Your Comment:
Insert the BLUE numbers shown below in the text field on the right. [ This is an anti-spam counter-measure ]
362615191
 
 
[HOME][LESSON INDEX][INVITE A FRIEND][FORUM][ABOUT ME]

modified last: September 2005 (Article Version: 2)

mail to: victorsaliba@hotmail.com

© Victor John Saliba 2006
| Privacy Statement | Disclaimer | Contact Us |
 
 
 
www.pascalprogramming.byethost15.com © Victor John Saliba 2006