Pascal Programming - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Pascal Programming

Description:

What is a variable. A variable is a named reference to a storage area in reserved memory. Examples ... letters(upper and lower cases), digits(0 to 9) and the ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 19
Provided by: website6
Category:

less

Transcript and Presenter's Notes

Title: Pascal Programming


1
Pascal Programming
  • Variables, Constants
  • and Data Types
  • Carl Smith
  • National Certificate Unit 4

2
What is a variable
  • A variable is a named reference to a storage area
    in reserved memory
  • Examples
  • Fred 10 integer
  • Loop_count 0 integer
  • User_name Carl Smith string
  • NOTE Use a name that is relevant to its use

3
Declaration
  • In Pascal, all variables must be declared before
    use. The variables are declared with the type of
    data they hold using the Var statement

4
Declaration cont
  • Identifiers
  • Identifiers are names that you use in your
    program for constants, types, variables,
    procedures, functions, units, programs and fields
    in records.
  • They can contain letters(upper and lower cases),
    digits(0 to 9) and the underscore character but
    not spaces. The first character cannot be a digit
    and a name can be up to 63 characters long.
  • Pascal identifiers are not case sensitive but
    must be unique within the program.

5
Pascal Reserved Words
6
DATA TYPES
7
INTEGER Data Types
  • Type From Through
  • Byte 0 255
  • Integer -32768 32767
  • LongInt -2147483648 2147483647
  • ShortInt -128 127
  • Word 0 65535

8
REAL Data Types
  • Type Name Range of Values
  • Comp 9.2E-18 to 9.2E18
  • Double 5.OE-324 to 1.7E308
  • Extended 3.4E-4932 to l.lE4932
  • Real 2.9E-39 to 1.7E38
  • Single 1.5E-45 to 3.4E48

9
Declaration cont
  • Strings are sequences of characters with a
    maximum size of 255 characters.String variables
    in particular can consume a lot of memory
  • This can be reduced by declaring their length
    before use e.g.-
  • fred string10
  • Declares a string variable called fred that can
    contain up to ten characters

10
Declaration cont
  • You can declare the name, data type and length in
    one statement e.g.-
  • Var
  • namea, nameb, namec, named, namee, namef, nameg,
    nameh, namei, namej string20
  • All the above variables become individual strings
    of length 20 characters each

11
Constants
  • When would you use a constant?
  • A very good documented example comes from the
    NASA space centre.
  • All programs controlling a launch use constants
    that store values such as wind speed,
    temperature, cloud cover etc. These are changed
    in the declaration section depending on the
    weather conditions to tailor the program to the
    prevailing conditions at lift off.

12
Constants cont
  • Examples-
  • Pi 3.142
  • Rulerline --------------------------
  • VATrate 17.5
  • Note that constants deduce the data type from the
    content

13
Elements of a string variable
  • String variables can be broken down into elements
    or individual characters
  • We can think of itlooking like this -
  • fred A Pascal Program
  • Therefore fred3 P

14
Introduction to Arrays
  • We have seen that each character of a string
    variable can be addressed
  • Arrays are a special kind of variable (of any
    data type) that have an Index number attached
    to each element such as-
  • VarName1 or VarName10

15
Declaration of arrays
  • We declare arrays in the following way-
  • ltvar identifiergt array range of lttypegt e.g.
  • Var
  • name array 1..10 of string20

16
To Use
  • We could input to or output any element of the
    array-
  • readln(name5)
  • writeln(Contents are , name5)
  • Arrays come into their own when we use within
    an iterative loop

17
To use (cont)
  • For example, we could display the whole (name)
    array with-
  • for counter 1 to 10 do
  • begin
  • write(Contents of ,counter)
  • writeln( is ,namecounter)
  • end

18
Summary
  • We looked at-
  • What is a variable?
  • What is a constant?
  • Data types of variables
  • String Elements
  • An Introduction to Arrays and Loops
  • Declaration and examples
  • QUESTIONS?
Write a Comment
User Comments (0)
About PowerShow.com