Download presentation
Presentation is loading. Please wait.
Published byNeal Palmer Modified over 6 years ago
1
CPSC 110 - Pascal Brent M. Dingle Texas A&M University 2001, 2002
Variables CPSC Pascal Brent M. Dingle Texas A&M University 2001, 2002
2
Declaring Variables Almost all programming languages use variables and Pascal is no exception. Variables hold data on which a program operates. Variables have values. The values are often numeric but can be a wide variety of other data types.
3
The TYPE of a variable The type of a variable is determined by the type of data that it holds. Pascal requires the type of the variable to be explicitly declared before the variable is used. Think of variables as a boxes which hold values where the name of the box is the name of the variable. Pascal requires you to decide in advance what type of values fit in the box.
4
Variables as Boxes
5
Some Types We Will See string integer real boolean
text (string of letters and/or numbers) Used to Write to monitor or Read from keyboard integer integer number (1, 17, -37, etc.) Used in Arithmetic operations such as +, -, *, / real decimal number (2.5, 0.7, , etc) Also used in Arithmetic operations boolean true or false
6
Variables as Boxes (cont)
So if your box is designed to hold integers then you cannot put strings in it. Likewise if your box is designed to hold strings, then you cannot put a number in it (though you can put the character representation of a number in it)
7
Assigning Values to Variables
There are two basic ways to assign a value to variables Use the procedure: Readln Use the assignment operator: := (that’s a colon equal) Let’s look at some examples
8
Program Example – Using :=
PROGRAM PetInfo; VAR pet_age : integer; pet_name : string; BEGIN pet_age := 4; pet_name := ‘Suds'; Writeln(pet_name, ' is ', pet_age, ' years old.'); END.
9
In the above := Program pet_age is a variable of type integer
pet_name is a variable of type string notice it defaults to a maximum length of 255 characters Both variables are assigned values using the assignment operator :=
10
Program Example – Using Readln
PROGRAM YouAre; VAR your_name : string[40]; BEGIN Write(‘What’’s your name? ‘); Readln( your_name ); Writeln(‘Howdy ‘, your_name, ‘ ! ‘); END.
11
In the above Readln program
your_name is a variable of type string. It can contain up to 40 characters – as that is how much space the brackets, [ ], reserved. If you leave the brackets out then the string defaults to its maximum capacity of 255 characters.
12
Rules for Naming Variables
When naming your variables: Do not use spaces. Use only letters, numbers and underscore. And they MUST start with a letter. (Pascal will not allow anything else)
13
End Variables
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.