1) Data Review 2) Anonymous Data Items 3) Renaming 4) Overloading 5) Introduction to Data Types 6) Basic types 7) Range and precision 8) Ada/Pascal “attributes”

Slides:



Advertisements
Similar presentations
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Advertisements

Programming Languages and Paradigms
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Names and Bindings.
ISBN Chapter 7 Expressions and Assignment Statements.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
COEN Expressions and Assignment
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
1) Pointers 2) Type declarations 3) Access/reference values 4) Type renaming/aliasing 5) Subtypes 6) Macro substitution COMP205 IMPERATIVE LANGUAGES 4.
1 ) Data Attributes 2) Declarations, Assignment and Instantiation 3) Global and Local Data 4) Variables 5) Constants 6) Example programs COMP205 IMPERATIVE.
1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Data types and variables
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
1) Introduction to higher level types 2) Arrays and their declaration 3) Assigning values to array elements 4) Operations on arrays 5) Ada array attributes.
ISBN Chapter 7 Expressions and Assignment Statements.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 2 Data Types, Declarations, and Displays
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
Objectives You should be able to describe: Data Types
CPS120: Introduction to Computer Science Lecture 8.
Programming Languages
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
Names Variables Type Checking Strong Typing Type Compatibility 1.
C Tokens Identifiers Keywords Constants Operators Special symbols.
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
CPS120: Introduction to Computer Science
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arithmetic Expressions
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Copyright © – Curt Hill Types What they do.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CPS120: Introduction to Computer Science Variables and Constants.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Floating Point Numbers
Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Chapter 6: Data Types Lectures # 10.
COMP205 IMPERATIVE LANGUAGES
Expressions and Assignment Statements
Expressions and Assignment Statements
Arithmetic Expressions
Introduction to Abstract Data Types
Expressions and Assignment Statements
Basics of ‘C’.
College of Computer Science and Engineering
C++ Data Types Data Type
Expressions and Assignment
Chapter 7 Expressions and Assignment Statements.
Data Types and Expressions
PRESENTED BY ADNAN M. UZAIR NOMAN
C Language B. DHIVYA 17PCA140 II MCA.
Expressions and Assignment Statements
Presentation transcript:

1) Data Review 2) Anonymous Data Items 3) Renaming 4) Overloading 5) Introduction to Data Types 6) Basic types 7) Range and precision 8) Ada/Pascal “attributes” COMP205 IMPERATIVE LANGUAGES 3. DATA AND DATA TYPES

ANONYMOUS DATA ITEMS Often we use data items in a program without giving them a name, such data items are referred to as anonymous data items. Example: or: The (5*6) is an anonymous data item. Anonymous data items cannot be changed or used again in other parts of a program. 4 + (5*6) x + (5*6)

SUMMARY Uninitialised data items. Anonymous data items. Constant data items. Variable data item.

THUS: Data items can be variables or constants or anonymous. Variables or constants can be global or local Data items are "introduced" using a declaration statement. Data items have an "initial" value associated with them through a process known as initialisation (uninitialised data items). The value associated with a variable can be changed using an assignment operation.

RELATIONSHIPS BETWEEN DATA ATTRIBUTES

RENAMING (ALIASING) This is supported by some imperative languages such as Ada (but not C). Example: I: integer = 1; ONE: integer renames I; It is sometimes useful, given a particular application, to rename (alias) particular data items, i.e. provide a second access path to it.

procedure RENAME is I: integer:= 1; ONE: integer renames I; begin put("I = "); put(I); put("ONE = "); put(ONE); I:= I*10; put("I = "); put(I); put("ONE = "); put(ONE); ONE:= ONE*10; put("I = "); put(I); put("ONE = "); put(ONE); end RENAME; It is dangerous to rename variables! I = 1 ONE = 1 I = 10 ONE = 10 I = 100 ONE = 100

START : INTEGER := 1 ; END : INTEGER := 7 ; MY_ARRAY : array (START..END) of INTEGER ; MID_ELEMENT : INTEGER renames MY_ARRAY((START+END)/2)) ; There are legitimate reasons why we might wish to rename a variable. For example:

OVERLOADING Whereas aliasing binds more than one name to one data item, overloading binds two or more data items (possibly of different types) to one name. Care must be taken not to cause ambiguity. Neither Ada, Pascal or C explicitly support overloading, however: 1In C overloading can be implemented by declaring a compound type known as a union 2In Ada the same effect can be contrived using an appropriately defined record.

More generally overloading can best be illustrated by considering the `+' and `-' arithmetic operators. Traditionally the operands for these operators are overloaded to allow: C also allows mixing of operands (Ada does not) - so called mixed mode arithmetic

INTRODUCTION TO DATA TYPES The type of a data item defines: 1) The nature of the internal representation of the data item. 2) The interpretation placed on the internal representation. 3) As a result of (2) the allowed set of values for the item. 4) The operations that can be performed on it. The process of associating a type with a data item is referred to as a data declaration.

CATEGORISATION OF TYPES Pre-defined and programmer-defined types. Pre-defined types are types immediately available to the user. Programmer-defined types are types derived by the programmer. Scalar and Compound types. Scalar types define data items that can be expressed as single values. Compound types define data items that comprise several individual values. Discrete and Non-discrete types. Discrete types are types for which each value (except its min. and max.) has a predecessor and a successor value. The opposite are non- discrete types. Basic and Higher Level types. Basic types are the standard predefined scalar types available for immediate use in any imperative programming language. Higher level types are made up of such basic types and existing higher level types.

PREDEFINED DATA TYPES Characters (int. range n..m) Integer (range -n..m) Natural number (range 0..m) Real numbers Logical type Character string Void Ada character integer float boolean string C char int float void Modula-2 CHAR INTEGER CARDINAL REAL BOOLEAN Pascal char integer real boolean text

EXAMPLES OF DATA INITIALISATION IN ADA & C Same Line Multiple declarations Using existing data items to initialise new data items Using arithmetic expressions Using functions C int i = 5, j = 4; Ada I: integer:= 5; j: integer:= 4; int i = 4, j = 4 int k = i; I, J: integer:= 4; K: integer:= I; int i = 4; int j = i+1; I: integer:= 4; J: integer:= I+1; int i = timesTwo(2) int i = 4*2;I: integer:= 4*2;

RANGE AND PRECISION WITH RESPECT TO NUMERIC TYPES The term range refers to the minimum and maximum value that numeric types can take. The term precision refers to the number of significant figures with which a number is stored. All numeric types have default precisions and/or ranges associated with them. Different imperative languages specify ranges and precisions in different ways: 1) Predefined (cannot be influenced by the programmer) 2) Adjectives (e.g. C) 3) Explicit specification (e.g. Ada, Pascal)

ADJECTIVES Adjectives are used to specify classes of numeric types. Some C examples are given below:

Unsigned short int Signed short int short int short Unsigned long int Signed long int long int long int float double Long double Type Size in bits Values 0 to to to to Real number to approx. 6 sig. figs.Real number to approx. 15 sig. figs. Real number to approx. 15 sig. figs. Real number to approx. 32 sig. figs.

NOTE ON TWO'S COMPLEMENT NOTATION The most common number code for storing integer values. Considering the following 8 bit "value box": the binary number will be equal to: = -115 From the above it is evident that any two's complement number with a leading 1 represents a negative number (and vice-versa)

NOTE ON FIXED & FLOATING POINT STORAGE Note: assigning more bits for the exponent will increase the range and decrease the precision. Fixed point = Number of bits available for exponent and mantissa static. Floating point = Number of bits available for exponent and mantissa dynamic. Floating point offers the advantage that it can be used to represent both very large and very small numbers without having to store all the zeroes. Most modern imperative languages use floating point storage.

EXPLICIT SPECIFICATION Explicit specification of range/precision is the most high level. In Ada this is achieved using a general type declaration statement: type is Examples: type SCORE is range ; type TEMPERATURE is digits 4; type PERCENTAGE is digits 4 range ;

with TEXT_IO; use TEXT_IO; procedure EXAMPLE is type PERCENTAGE is digits 4 range ; package PERCENTAGE_INOUT is new float_io(PERCENTAGE); use PERCENTAGE_INOUT; N_VAL, M_VAL: PERCENTAGE; begin get(N_VAL);get(M_VAL); put(N_VAL);put(M_VAL); new_line; end EXAMPLE;

ADA ATTRIBUTES Many imperative languages (Ada, Pascal) allow the programmer to access constants that give information about the default properties of a given type. In Ada such constants are referred to as attributes. Some common (Ada) attributes: integer float character first, last digits, small, large pos, val Attributes

with CS_IO; use CS_IO; procedure EXAMPLE is begin put("first = "); put(integer'first); new_line; put("last = "); put(integer'last); new_line; put("digits = "); put(float'digits); new_line; put("small = "); put(float'small); new_line; put("large = "); put(float'large); new_line; put("pos(A) = "); put(character'pos('A')); new_line; put("val(66) = "); put(character'val(66)); new_line; end EXAMPLE; first = last = digits = 15 small = E-62 large = E+61 pos(A) = 65 val(66) = B

SUMMARY 1) Data Review 2) Anonymous Data Items 3) Renaming 4) Overloading 5) Introduction to Data Types 6) Basic types 7) Range and precision 8) Ada/Pascal attributes