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 3
- Special Reserved Words of the CRT Unit: ClrScr(), GotoXy(), etc...

 
This lesson will cover :
 
 

Use of the Reserved Words

Before I teach you the if statements and for loops, I would like to give you an idea of some pascal functions which are quite useful. This would require your program to include the library 'crt.tpu'. To include a library in the program, one should use the reserved word 'uses', because it is used to call a library of functions and procedures. Here is the program of lesson 2 (program 3) which is better handled and more user-friendly:

Program lesson3_Program1;

Uses Crt; {We will make use of the crt library}

Var PD, Dname, Cmodel : String;
    CostPD, TCostPD, Distance : Real; 
    {real is a decimal (described later)}

Begin
 textbackground(brown); {background colour}
 ClrScr; {Clear screen with a brown colour. 
             Try run the program without this!!!}
 TextColor(lightgreen); {text colour}
 TCostPD := 0; 
 Writeln('This program prompts you to '+
        +'input the cost per litre of');
 Writeln('the petrol/diesel you spend in and '+
          +'the average distance you travel');
 Writeln('with your car every week. Then, '+
          +'the computer calculates the total cost');
 Writeln('you spend in fuel every week.');
 Readkey; {program move on as soon as a key is pressed}
 ClrScr;{short for clear screen}
 GotoXy(28,3); 
 {^move to a position on the screen: 
   x (horizontal), y (vertical)}
 Write('Diesel or Petrol? Type p or d: ');
 PD := Readkey; 
 {^as soon as a key is pressed, 
  it is stored in the variable 'PD'}
 GotoXy(30,4);
 Write('Name Of Driver: ');
 Readln(Dname);
 GotoXy(30,5);
 Write('Car Model: ');
 Readln(Cmodel);
 GotoXy(29,6);
 Write('Cost of Diesel/Petrol: (£) ');
 Readln(CostPD);
 GotoXy(8,7);
 Writeln('Average distance you travel with '+
          +'your car every week: (kilometres) ');
 Readln(Distance);
 ClrScr;
 GotoXy(28,3);
 Writeln('Name of Driver:',Dname);
 GotoXy(31,4); Delay(500);
 Writeln('Car Model:',Cmodel);
 GotoXy(32,5); Delay(500);
 Writeln('Diesel/Petrol:',PD);
 GotoXy(8,6); Delay(500);
 Writeln('Average distance covered '+
        +'every week: ',Distance:1:2,'Km');
 GotoXy(25,7); Delay(500);
 Writeln('Cost of ',PD,' per litre: £',CostPD:1:2,'/litre');
 Writeln; Delay(500);
 Writeln;
 TCostPD := Distance * CostPD;
 GotoXy(21,10);
 Writeln('Total cost of ',PD,' per week:£',TCostPD:1:2);
 TCostPD := 0;
 GotoXy(21,12);
 Writeln('Total cost of ',PD,' per week:'+
        +'£',(Distance * CostPD):1:2);
 GotoXy(18,14);
 Writeln('Total cost of ',PD,' per week:£',Distance * CostPD);
 readln;
End. 

(if you want to see the difference of the 2 programs then you should run them) What is the difference between this program and the program which is program 3 in lesson 2? 

The 'Crt' (short for cathode-ray tube) library has a wide range of functions and procedures that you will use so frequently. Some of them are listed in the table below. There are many similar libraries, such as 'Strings' (you will be learning something on this later) and 'Dos'.

Description of the following Reserved Words

Below is a table of the new words:

Reserved Word

Crt: Yes/No

Description

Clrscr Yes Clears screen
Gotoxy(int,int) Yes Takes the cursor to the pre-defined position
Textbackground(word/int) Yes Background colour
Textcolor(word/int) Yes Colour of text
Readkey Yes Reads a key; Could be assigned to a variable
Delay(int) Yes Waits for the included time(milliseconds)
Halt(parameter) No Program terminates

key  (variable type) :

int - integer (-32768 to 32767), word - 0 to 65535.

Example of each

  • Clrscr: (clear screen)

writeln('When you press enter, the screen would be cleared!');
readln;
clrscr;

  • Gotoxy(int,int): (Go to position x and y);

gotoxy(10,10);
Writeln('The position is 10 pixels from the left of the screen, and ten pixels');
Writeln('from the top of the screen.');
readln;

  • Textbackground(word/int): (Background colour);

Textbackground(red); {word - red}
Writeln('Note the difference');
Textbackground(5); {integer - 5}
ClrScr;
Writeln('Note the difference'); Readln;

  • Textcolor(word/int): (Text colour);

Textcolor(red); {word - red}
Writeln('Text colour');
Textcolor(5); {integer - 5}
Writeln('Text colour'); Readln;

  • Readkey: (Reads a key-press);

Example 1:

Writeln('Press ANY key!!!');
Readkey;

Example 2:

Writeln('Press ANY key');
Keypress := readkey; {keypress is a DECLARED string variable(can be an integer variable)}
Writeln(Keypress);

  • Delay(int): (Waits for some time);

Writeln('1');
Delay(1000);{1000 milliseconds}
Writeln('2');
Delay(1000);
Writeln('3');
Readln;

  • Halt(int): (Program terminates with an exit code);

writeln('Press enter and the program terminates!);
Readln;
Halt(0);

Note that instructions following 'halt' are not executed since the program terminates when halt is encountered.

 
 


User Comments

Post By: ramsey mnjala Posted On: 2009-07-15
thats fantastic am doing well in my classes

Post By: Daqenzo Posted On: 2009-06-06
someone help

Post By: Daqenzo Posted On: 2009-06-06
My prob is wen i go writeln('press any key to exit'); readkey; it dont move,it say unknown identifier,I no forget uses crt; Tutorial very good thanx

Post By: sole Posted On: 2009-05-27
this is the best stuff about pascal programming i've ever come across on the web so far. thanks

Post By: Boz Posted On: 2009-05-20
Respect to da man. dat was awsum da surf is up and u guyz is livin da dream

Post By: smella Posted On: 2009-05-19
pascal is sexy as me, and i am not sexy in the slightest.

Post By: strange child Posted On: 2009-05-19
i luvvv pascal

Post By: bobaluina Posted On: 2009-05-19
happy

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

Post By: ical Posted On: 2009-04-04
i' very interest ....

Post By: Jero Posted On: 2009-02-23
Please send me some Laboratory exercises base on this article. thanks:)

Post By: jero Posted On: 2009-02-23
i like the way you expalin it.. My I.T students learned to love pascal programming because of this article:) heheee.. Thanks a lot God Bless.. ( Jero )Spamast State College Phil.

Post By: mike Posted On: 2008-12-05
Thanks for this information, it really helps me a lot.But please somebody help me, when i run this program, it pop ups error 200 division by zero what does it mean.how to debug with it. thnks!

Post By: profphilipe Posted On: 2008-11-19
this article is good its easy to under stand,has alot of information am a student at copperbelt university and i hav leant alot from the article

Post By: andrei Posted On: 2008-11-04
enter is #13

Post By: bibo Posted On: 2008-10-26
would you please send to me some questions about pascal programming language?! .. thank you .. hiba

Post By: warnakulasooriya ajantha Posted On: 2008-10-05
please send to me all pascal program practical program and inormation thank yours

Post By: zakaria Posted On: 2008-09-28
j'ai besoin d'un programme pascal qui afficher le menue déppart a l'airoport

Post By: zekalo Posted On: 2008-08-25
how can i write a program that calculates income tax

Post By: Posted On: 2008-08-01
Your Comment

Post By: Renz Alviar from philippnes Posted On: 2008-06-26
How can i play sound or beep in my program..

Post By: ET Posted On: 2008-04-28
is there any way to bold strings/char in pascal?

Post By: Egzon Posted On: 2008-04-17
I think these tutorials doesn't work for Turbo Pascal for Windows, I keep getting errors..

Post By: coljetix Posted On: 2008-03-30
frank ill show example to understandlike this program program use; uses crt; var i:real; begin for i:0 to 10 do begin writeln('hi'); end; end. see uses crt; are first then i write var i:real; whats why your program dont work i hope its usefull

Post By: Wasantha Ramanayake Posted On: 2008-03-19
wow ...this is a treasure for me ,I am from sri lanka. thank you thousand times!

Post By: yogesh Posted On: 2008-03-04
it is very good guide line for any student.

Post By: coljetix Posted On: 2008-02-24
libaryes uses uper var example program ***; uses crt; var *,*,*:real; begin

Post By: sam Posted On: 2008-02-05
FRANK !!! To enter background color type in uses crt; before entering varables. Color is spelt COLOR NOT COLOUR

Post By: sam Posted On: 2008-02-05
HEY FRANK!!! TO PUT BACKGROUND COLOR YOU MOST ENTER uses crt; before entering variables

Post By: frank Posted On: 2008-01-02
I get 2 erroros when i try to run the prgogram first: error 3: Unknown identifier for the textbackround(brown); second: error 122: Invalid variable reference. if i take this 2 out the program runs fine

Post By: Sapp Posted On: 2007-12-17
Nice tutorial i loveit BUT i got stuck at the crt library ... i have no idea where to getit or if i haveit how to useit ... help :(

Post By: hesham Posted On: 2007-11-20
First i want to thank you for this iformation but i think that easy to use the order goto intx with defin label than use gotoxy .In fact,idont understand it.

Post By: sahba saffary Posted On: 2007-11-10
DEAR SIR, I WROTE A GRAPHIC PROGRAM WITH PASCAL VERSION 7 .IT COMPILES WELL BUT IT DIDNOT RUNE .AND I RECEIVED AN ERROR 200,DIVISION ZERO. WHAT MUST I DO? PLEASE HELP ME.

Post By: A5c11Char5et Posted On: 2007-10-31
West, As a programmer myself (albeit of languages other than Pascal which I'm learning currently) I very much doubt anybody is going to do your coursework for you unless they are exceptionally nice. Programming is all about figuring things out yourself, I haven't looked this tutorial over entirely yet but I wouldn't mind betting all the things you need to make a 'Ping Pong game' are here. Its your job to figure out how to construct them proper. I suggest you have a stab at it, and if it doesn't work produce your broken code. People will be much more willing to help you if you've attempted it. You can find me in IRC channel #freebasic or #peltkore on server irc.freenode.net Yours A5c11

Post By: west Posted On: 2007-09-13
SIr/Madam, your PASCAL PROGRAMMING BASICS helps me a lot..BUt do you know how to program a PING-PONG GAME coz I need it on my assignment on school..It will help me a lot if send me the source code. Thanks in advance.. yours truly, west

Post By: Posted On: 2007-09-06
Your Comment

Post By: Aakash Posted On: 2006-09-25
It was a nice thing i wanted 2 know since a long time.

Post By: Gabriel Posted On: 2006-08-23
would you please send to me the data structure tutorial in pascal programming language? Thanks in advance! Gabriel


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 ]
8536176859
 
 
[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