Wednesday, October 21, 2009

3. Variables in Pascal

Variables are entities which vary in their life time. For example Numbers, they could be 1 to 10,000 or they could be 1.25 to 10000.85. Characters could be A to Z or ARAM to Zooram. So the computer needs to know what type of variable we are dealing with or manipulating in the program.

numbers 1 to say 10000 which do not have a decimal are called INTEGER. Those which have decimal are called REAL. variables which represent a group of characters are called STRING.

Let us make a program with variables. Our aim is to use 3 types of variables and print their values using Pascal program. Here is the program. Try it on your computer.



Program program3;
uses crt,graph;
var number1, number2 : integer;
var interest1, interest2 : real;
var name1, name2 : string;
{ This is my third pascal program }
begin
clrscr;
number1 := 21;
number2 := 1004;
interest1 := 2.5;
interest2 := 5.4;
name1 := 'Rao';
name2 := 'Lakkaraju';
Writeln('number1 = ', number1);
writeln('number2 = ', number2);
writeln('interest1 = ', interest1, ' interest2 = ', interest2);
writeln('My Name is ', name1, ' ', name2);

writeln('My third Pascal Program');
readln;
end.

The symbol ' :=' is called assignment symbol. number1 := 21 means number1 = 21.
name1 := 'Rao' is an assignment but it is a string assignment.
Uses crt,graph; I want the computer to use crt and graphics related language also.
clrscr; means clear the screen before writing. I am using crt language.

Summary: Variables are three types, integer,real and string. integer numbers are numbers without decimals, real numbers are numbers with decimals and string variables are combination of characters. by enclosing combination of characters by quotation marks, we can assign to a string variable, ex: 'David'.

No comments:

Post a Comment