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 2
- Variables, Constants and the Assignment Statement

 

This lesson will cover:

 
 
String Variables

Soon, you should learn how to input text by the user using 'string variables'. The following program is written showing an example of a string variable, prompting the user to input his name, whatsoever:

Program Lesson2_Program1;
Var name, surname: String;
                     
Begin
 Write('Enter your name:');
 readln(name);
 Write('Enter your surname:');
 readln(surname);
 writeln;{new line}
 writeln;{new line}
 Writeln('Your full name is: ',name,' ',surname);
 Readln;
End.

If we take a look at this program, we notice a new variable type : 'String'. Both the name and surname variables are of type string. When the program is run and prompts the user to input his name, the name which is keyed in by the user goes directly to its place in the memory called 'name'. Same occurs to surname. *I'd like to remind you that the variables 'name' and 'surname' are not reserved words, but are used by the programmer as his variables. I could have used 'n' instead of 'name' and similarly 'sname' instead of 'surname'. The two 'idle' writeln's in lines 9 and 10 are used to move on for a new line. In this case, 2 lines are cleared. The next message displays the full name of the user using the above format. If a string variable is required to be displayed on screen, it should be put in between inverted commas and commas if it is concatenated with another message; example:

It could be in the form of: (please note where you must or you must not put the inverted commas following the commas)

*Writeln('Your name is: ',name);

or:

Writeln('Your name is:',name,'. Your surname is ',surname,'.');

*Note that you can even make it this way: 

Writeln('Your name is: ',name,'');

BUT you should put the inverted commas properly (underlined) to close the message.

Constants and the Assignment Statement

Apart from variables, there are also items in the program which are referred to as 'constants'. Unlike variables, constants keep their value or string unchanged for the whole program. Here I have made a program, not so much different from the previous one: 

Program Lesson2_Program2;
Var surname: String;
Const {the reserved word 'const' 
       is used to initialize constants}
     name = 'Victor';
                     
Begin
 Write('Enter your surname:');
 readln(surname);
 writeln;
 writeln;
 Writeln('Your full name is: ',name,' ',surname);
 Readln;
End.

In the above program, the constant 'name' is assigned to as 'Victor' and is of type string. However, in other cases, you might have used integer constants (whole numbers), i.e.: 

Const

    age = 15;

The constant 'age' is a value that could be used whever it is required in a program. Example:

age2 := 15;

age2 := age + 15;

The above example shows an addition of the value of the variable 'age' with the value 15. The value of the constant 'age' remains 15, but the value of the variable 'age2' becomes 30. The assignment statement is not only used for additions, but is also used to assign a variable: text if it is a string variable or a numeric value if it is an integer variable.

Try examine this yourself:

name := 'victor';
age := 15; {also: "age:='15';" BUT in this case, 'age' is an integer variable}
writeln('Name:',name,'. Age:',age,'.');

I conclude lesson 2 with another simple program for you to read and think about:

Program lesson2_Program3;
Var PD, Dname, Cmodel : String;
    TotalKM, CostPD, TCostPD, Distance : Real; 
     {real is a decimal (described later)} 
                   
begin
 TCostPD := 0; 
 {note that this is called an 'initialisation'. 
  It is important to initialise variables to 0 
  so that it is 'refreshed' from the previous 
  'rubbish' value in the memory.}
 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.');
 Readln;
 Write('Diesel or Petrol?: ');
 Readln(PD);
 Write('Name Of Driver: ');
 Readln(Dname);
 Write('Car Model: ');
 Readln(Cmodel);
 Write('Cost of Diesel/Petrol: (£) ');
 Readln(CostPD);
 Writeln('Average distance you travel '+
        +'with your car every week: (kilometres) ');
 Readln(Distance);
 Writeln;
 Writeln;
 Writeln('Name of Driver:',Dname);
 Writeln('Car Model:',Cmodel);
 Writeln('Diesel/Petrol:',PD);
 Writeln('Average distance covered '+
        +'every week: ',Distance:1:2,'Km');
 Writeln('Cost of ',PD,' per liter: £',CostPD:1:2,'/litre');
 Writeln;
 Writeln;
 TCostPD := Distance * CostPD;
 Writeln('Total cost of ',PD,' per week:'+
        +'£',TCostPD:1:2); {note this,}
 TCostPD := 0;
 Writeln('Total cost of ',PD,' per week:'+
        +'£',(Distance * CostPD):1:2); {this}
 Writeln('Total cost of ',PD,' per week:'+
        +'£',Distance * CostPD); {and this - without ':1:2'}
 readln;
End.

 
 


User Comments

Post By: Riro Enox Posted On: 2010-03-15
Quite educate fl as soon, fl privileged to be ua associate!!!

Post By: Vorsakend Posted On: 2010-03-04
Thanks a lot man, this really helps me, i have an exam in few months , now i can finaly study. Peace

Post By: Justine manda Posted On: 2010-02-28
Iam a beginer in programmng and the lessons are so nice and ease to follow.THANCKS.

Post By: toni Posted On: 2010-01-26
why didnt u put input oor output a the top of the program in brackets foollowed by the semi colon

Post By: kim Posted On: 2010-01-26
i hope to become a great programmer!!!

Post By: apple Posted On: 2010-01-19
please... anyone can help me..in turbo magic words..help po...tnx

Post By: M@jiD Posted On: 2010-01-18
It's really useful... Thank You.

Post By: Vanquish Posted On: 2010-01-07
Can anybody help me? I'm using Bloodshed "Dev-Pascal" for compiling and when i compile programs with constants, for example "Lesson2_Program2", I'm getting syntax error which says: "expected but const char found". Any ideas..? Thank you.

Post By: Chibyke Posted On: 2009-12-28
please can you show me how to use a Boolean expression in a program. Thank you.

Post By: arya Posted On: 2009-12-19
nice job, go on... :)

Post By: ^_^ jO-aNe^_^ Posted On: 2009-11-24
we love it!!!!!!!!!!!! ♥♥♥

Post By: ♥kyang2x♥ Posted On: 2009-11-24
nice job!!!!! we are using ur website for our lessons in T.L.E♥♥ i like it!!!

Post By: Remmy Posted On: 2009-10-27
I learned this in school, but it didn't make sense what Strings and constants were until I read this~ I commend you!

Post By: sarbaz Posted On: 2009-10-27
good job

Post By: waza Posted On: 2009-09-29
consider the following procedure q. procedure q(num1,num2:integer); begin if num2<=0 then writeln else begin q(num1-1,num2-1); write(num1:1); q(num1+1,num2-1); end{else} end{q}; the question is what output is produced by the procedure call q(14,4). how many lines of output are produced by by the call q(14,10).if the write statement is moved before the first rercursive call to q,what output will be produced by q(14,4).

Post By: ashamide/Angel Posted On: 2009-08-21
Nice one...get to get the compiller install and start practising. Thanks

Post By: lionel Posted On: 2009-07-31
Really web site.. It will help me to learn pascal

Post By: nishshanka Posted On: 2009-07-15
Your Comment

Post By: ramsey mnjala Posted On: 2009-07-15
thanks for this lesson i aprecite it

Post By: akila Posted On: 2009-06-17
nice

Post By: dorabela Posted On: 2009-04-16
o teu nome é mt feio!!!! bkokas

Post By: marvambi Posted On: 2009-03-06
I'm yet to check the stuff here up; so, I shall be commenting another time when i visit this site again.

Post By: ~viper~ Posted On: 2009-02-04
that last program needs revision... compile error on your double '+' usage to wrap lines for me...also the prompt before the input of km would look better with 'write' rather than 'writeln', and the be output says 'cost per week' twice

Post By: boss Posted On: 2008-11-07
Thank you so much for the good lessons.am currently preparing for Dip IT (module II)exams,And to be honest,am getting alot of informations which i think is relevant to the forthcoming exams.keep it up for the good work.

Post By: syeira Posted On: 2008-08-01
some coding easy to understand...and important for me to do the revision..

Post By: sergio ramos Posted On: 2008-06-27
the corses are very well , by they need solved problems to be more illustrative

Post By: jomar Posted On: 2008-06-05
can you lpease give the output of the input!

Post By: Admir Posted On: 2008-05-11
Is posible: 5+j4+7+j2 = 12+j6? I can not separate "j" from real numbers and same time sum all "j".

Post By: nath (nigeria) Posted On: 2008-05-10
thanks for a job well done

Post By: sam Posted On: 2008-04-15
this is the best

Post By: Satya Posted On: 2008-04-05
I have pascal exams today and i found ur site in morning...really good one :) thanks dood

Post By: jade Posted On: 2008-03-25
can someone tell me why everytime i save thses programs in notepad and complile it in pascal turbo it says error mesage why please please please please please please can someone tell be checking this site every day this week

Post By: Shar Posted On: 2008-02-05
Uhm...I dunno if this is the lesson that teaches about some identifiers...I'm having problems with my programming class and I don't know how to use the MOD statement I think.

Post By: caneM Posted On: 2008-01-17
site is GREAT>>> this is for makpahz-a... try this program... program bla; uses crt; var x : real; begin writeln('input your number... '); readln(x); write(x :5 :3); end. then try this one... program bla; uses crt; var x : real; begin writeln('input your number... '); readln(x); write(x); end. you will see diference...

Post By: AMAL Posted On: 2008-01-13
This is a wonderful site for learning PASCAL. I truly appreciate the hard work done by the creators. Thanx.

Post By: makpahz Posted On: 2008-01-09
this is a wonderful tutorial and it is giving me a good idead about pascal. i would want to note that the pascal installation program did not install to the end on my systems (as it always ask of a path that never gets recognised). secondly on this second lesson program 3, i am yet to understand why the introduction of ":1:2" statements. pls can you explain.

Post By: karimax Posted On: 2007-12-10
just dont get it ned to learn more

Post By: Darky Posted On: 2007-11-03
Really good tutorial... tkx :D

Post By: krish Posted On: 2007-09-25
This is an excellent site. thank for giving such a good knowledge about PASCAL. AWESOME!!

Post By: ALPHA Posted On: 2007-09-13
Why couldn't I have found this site earlier

Post By: Johnny Posted On: 2007-07-05
Tkanx

Post By: Jerry Posted On: 2007-06-11
Trying to work my way through your tutorial. I can't get all of Lesson 2 to download to my browser. It stops after "I'd like to remind you that the variables 'name' and surname" and then just list various posts. How do I get the rest of the lesson?

Post By: brilliant Posted On: 2007-05-29
Well done :)

Post By: mikou Posted On: 2007-04-12
Really nice web site! I think it will help me a for my BCS course! Nice job!

Post By: k9man99 Posted On: 2007-03-25
?????????

Post By: Rozza Posted On: 2007-02-27
this is an awsome site, thanks!

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

Post By: Kurt Posted On: 2006-10-12
Thanks for putting this site together! I'm having fun with it and learning a lot!

Post By: Yash Rai Posted On: 2006-09-16
Really nice web site! Think it will help me and my friends real much for our BCS course! Nice and tidy job!

Post By: AmaSaro Posted On: 2006-09-14
YOU ROCK ROCK ROCK ROCK ROOOCCCKKKK

Post By: T.Chabata Posted On: 2006-07-17
Thank you so much for such good lessons.They have helped me so much.I'm doing a Diploma in Systems analysis and Design,in this programme I do Pascal Programming.I still need much more practice.More questions for beginners. Keep up the good work!! God bless you.

Post By: Ken Posted On: 2006-07-06
Excellent site !! AND I did'nt have this "register first" nonsense !!!


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