Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.

Slides:



Advertisements
Similar presentations
Names and Bindings.
Advertisements

ISBN Chapter 7 Expressions and Assignment Statements.
Types and Arithmetic Operators
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
ISBN Chapter 7 Expressions and Assignment Statements.
CSC321: Programming Languages5-1 CSC321: Programming Languages Chapter 5: Types 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 5: Types Fall 2009 Marco Valtorta.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
ISBN Lecture 07 Expressions and Assignment Statements.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
C H A P T E R S E V E N Expressions and Assignment Statements.
Names Variables Type Checking Strong Typing Type Compatibility 1.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Arithmetic Expressions
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
1 CS Programming Languages Class 10 September 26, 2000.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
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
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Expressions and Assignment Statements
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Data Types In Text: Chapter 6.
The Machine Model Memory
Chapter 7: Expressions and Assignment Statements
Chapter 6 – Data Types CSCE 343.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Expressions and Assignment Statements
Chapter 6: Data Types Lectures # 10.
Lecture 16: Introduction to Data Types
Object Oriented Programming
CS 326 Programming Languages, Concepts and Implementation
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Arithmetic Expressions
Introduction to Abstract Data Types
Expressions and Assignment Statements
College of Computer Science and Engineering
Chapter 7 Expressions and Assignment Statements.
B=15 b = a + fun(a); a=10 fun(a)=5.
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Types(1)

Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations +, -, *, /, <,... The Boolean type  has values true and false  operations   AND,   OR,   NOT.

Lecture 53 Type(2)  Computer types have a finite number of values due to fixed size allocation problematic for numeric types.  Exceptions: Smalltalk uses unbounded fractions. Haskell type Integer represents unbounded integers.  Fixed Size Floating point problems? 0.2 is not exact in binary. So 0.2 * 5 is not exactly 1.0 Floating point is inconsistent with real numbers in mathematics.

Lecture 54 Type(3)  In the early languages, Fortran, Algol, Cobol, all of the types were built in.  Purpose of types in programming languages is to provide ways of effectively modeling a problem solution. A type color, could use integers; but what does it mean to multiply two colors.

Lecture 55 Type Errors(1)  Machine data carries no type information.  Basically, just a sequence of bits.  Example: The floating point number The 32-bit integer 1,079,508,992 Two 16-bit integers and 0 Four ASCII characters X NUL NUL

Lecture 56 Type Errors(2)  A type error is any error that arises because an operation is attempted on a data type for which it is undefined. Type errors are common in assembly language programming. High level languages reduce the number of type errors.  A type system provides a basis for detecting type errors.  Types constraints cannot be expressed syntactically in EBNF

Lecture 57 Type Errors Example  union {int a; float b;}  u.a =1;  …  x=x+u.p

Lecture 58 Static and Dynamic Typing(1)  A language is statically typed if the types of all variables are fixed when they are declared at compile time.  A language is dynamically typed if the type of a variable can vary at run time depending on the value assigned.

Lecture 59 Static and Dynamic Typing(2)  A language is strongly typed if its type system allows all type errors in a program to be detected either at compile time or at run time.  A strongly typed language can be either statically or dynamically typed.  Strongly typed languages Java  Non- Strongly typed languages C and C++

Lecture 510 Basic Types Issues  Binding types to memory is the compiler implementor task  Some languages allows to specify the required range for each variable  Numeric types is finite in size  overflow  Example a + (b + c)  (a + b) + c  C-like languages, the equality and relational operators produce an int, not a Boolean  Exact decimal 0.2 don’ have exact binary representation 5 x 0.2 ≠ 1.0

Lecture 511 Type conversion  At the machine level, arithmetic operations required that both operands be the same type  Type conversion Creating a different bit pattern to represent a value in a different type  A type conversion is a narrowing conversion if the result type permits fewer bits, thus potentially losing information.  Otherwise it is termed a widening conversion.

Lecture 512 Non-basic Types  Enumeration  Pointers  Arrays and Lists  Strings  Structures  Variant Records and Unions

Lecture 513 Enumeration : enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; enum day myDay = Wednesday;  In C/C++ the above values of this type are 0,..., 6.  Enumeration allows the code to become more readable.

Lecture 514 Pointers  A pointer is a value that represent a memory address  Pointers operators in C (unary &)  address of operator  Takes a variable argument  Returns the address of the variable (unary *)  dereferencing operator  Takes a reference  Produces the value of the reference

Lecture 515 Pointers and Arrays float sum(float a[ ], int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += a[i]; } float sum(float *a, int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += *a++; }

Lecture 516 Arrays and Lists  C-like languages restrict array to be one dimensional  More dimensional array is an array of arrays  Array size is statically bound Java creates array and the size can be determined at the runtime Ada the size of declared array is determine by the number of elements in its initial value

Lecture 517 Strings  In C, a string is a 1D array with the string value terminated by a NUL character (value = 0). User most remember to allocate at least 1 extra byte for trailing NUL character String assignment (a = b) is the programmer responsibility  In Java, Perl, Python, a string behaves as a built-in type. String variable can hold an unbounded number of characters.

Lecture 518 Structures  Collection of elements of different types  Used first in Cobol  Absent from Fortran  Common to Pascal-like and C-like languages  Omitted from Java as redundant

Lecture 519 Structure Example struct employeeType { int id; char name[25]; int age; float salary; char dept; }; struct employeeType employee;... employee.age = 45;

Lecture 520 Variant Record and Union  Two or more different fields share the same block of memory  Programmers had to keep the processing of the union variables separated through the run-time life of the program.  Languages that having union are not strongly-typed languages C++ is not strongly typed because of the backward compatibility with C  Less widely used on modern Applications