Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type.

Slides:



Advertisements
Similar presentations
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Types and Arithmetic Operators
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
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Data types and variables
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
VB .NET Programming Fundamentals
String Escape Sequences
Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Computers Organization & Assembly Language
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
CSU0014 Assembly Languages Homepage: Textbook: Kip R. Irvine, Assembly Language for Intel-Based Computers,
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CPS120: Introduction to Computer Science
Chapter 2.6 Modula 2 Structured Types. Types in Modula 2 Simple Types: values can’t be decomposed –Ordinal Types: bijection with natural numbers –Reals:
Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.
Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arithmetic Expressions
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Simple Data Types Built-In and User Defined Chapter 10.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
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.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
Data Types Declarations Expressions Data storage C++ Basics.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
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.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
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 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
The Machine Model Memory
Chapter 7: Expressions and Assignment Statements
BASIC ELEMENTS OF A COMPUTER PROGRAM
Data Representation Binary Numbers Binary Addition
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
CS 326 Programming Languages, Concepts and Implementation
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Introduction to Java, and DrJava part 1
Building Java Programs
Primitive Types and Expressions
Rayat Shikshan Sanstha’s S. M. Joshi College, Hadapsar
Assistant Professor Rabia Koser Dept. of Computer Applications
Building Java Programs
Presentation transcript:

Chapter 2.4 Modula 2 Simple Types

Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type = set of all values the variable can have The type determines the internal representation of data as well as the operations that can be performed on the data

Types in Modula 2 Simple Types: values can’t be decomposed –Ordinal Types: bijection with natural numbers –Reals: approx. representation for real values –Pointers: addresses in data memory Structured Types: Values have # components –Arrays: all components have same type –Records: components can have # types –Sets: small sets of ordinal values –Procedures: entire subprograms

Simple Types Ordinal Types –Enumerated types –BOOLEAN type –CHAR type –CARDINAL type –INTEGER type –Subrange types REAL Type POINTER Type

Enumerated Types Possible values: User defined TYPE Day = (Monday,Tuesday,Wednesday, Thursday, Friday, Saturday, Sunday) Operators : Only relational (=, #, >, =, <=) –Ordering by enumeration Day = (Monday,Tuesday,Wednesday, Thursday, Friday, Saturday, Sunday); Monday < Sunday Day = (Sunday, Monday,Tuesday,Wednesday, Thursday, Friday, Saturday); Monday > Sunday

Enumerated Types Main benefit : Clarity TYPE Sex = (Male, Female); VAR SexA, SexB : Sex;... IF SexA = Male THEN SexB := Female ELSE SexB := Male END (* IF *) Replaces manual encoding (* Male encoded by 0, Female by 1 *) VAR SexA, SexB : CARDINAL;... SexB := 1 - SexA;

BOOLEAN Type Possible values: TRUE, FALSE Operators : NOT, AND, OR X AND Y X = TRUE X = FALSE TRUE Y = TRUEY = FALSE FALSE X OR Y X = TRUE X = FALSE TRUE Y = TRUEY = FALSE FALSETRUE NOT XFALSE X = TRUEX = FALSE TRUE

Relational Expressions a = bTRUE when a equals b a # bTRUE when a different from b a > b TRUE when a greater than b a < bTRUE when a less than b a >= bTRUE when a greater or equal b a <= bTRUE when a less or equal b a IN bTRUE when value of a belongs to the set b have a BOOLEAN value

CHAR Type Possible values: all characters of a system specific alphabet. In many systems : extended ASCII –128 standardized characters additional characters –Various extensions for allowing non-anglosaxon characters In new Microsoft & Java systems : UNICODE –65536 different characters, adequate for all languages in the world. ASCII is a subset of UNICODE. Operators : Only the relational operators –Order can be different in different alphabets. Relative position of space, letters and digits VAN ZOERSEL Van Zoersel VANANTWERPENvan Gorp

Printable ASCII Character Set 047/063?079O095_111o

ASCII extensions for pc 207 ¤ 223 _ 239 ´ 255

ASCII special characters 128DELDelete

CARDINAL Type Possible values: implementation dependant –With 16 bit representations : 0 <= C < 2 16 = –With 32 bit representations : 0 <= C < 2 32 ~ Operators : relational + + addition -subtraction *multiplication DIVinteger quotient MODinteger remainder Examples : 5 DIV 2 = 2 5 DIV 7 = 0 5 DIV 1 = 5 5 MOD 2 = 1 5 MOD 7 = 5 5 MOD 1 = 0

INTEGER Type Possible values: implementation dependant –With 16 bit representations : <= I < 2 15 = –With 32 bit representations : <= I < 2 31 ~ Operators : relational + + addition -subtraction *multiplication DIVinteger quotient MODinteger remainder Examples : 5 DIV 2 = 2 -5 DIV 2 = -2 5 DIV -2 = -2 5 MOD 2 = 1 -5 MOD 2 = ?? 5 MOD -2 = ??

Internal representation of Integers Conventions: n = number of bits in representation M = value to be represented M' = value of representation Two's complement definition: M' = (M + 2 n ) MOD 2 n Range: - 2 n-1 <= I < 2 n-1

Internal representation of Integers

INTEGER & CARDINAL Overflow With 16 bit representations: Cardinal : = !!! Integer : = !!!

Score: With thanks to Ariane for the score

Subrange Types For all ordinal types subrange types can be defined TYPE Day = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); WeekDay = [Mon..Fri]; DayOfMonth = [1..31]; Month = [1..12]; Why Subranges ? –Makes out-of-range detection possible –Allows optimized memory allocation by compiler

Functions and Operators for ordinal types The ORD function returns the internal representation of any ordinal variable as a cardinal value. TYPE Day = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); VAR X : Day; Ch : CHAR;... X := Wed; (* ORD(X) = 2 *) Ch := "A"; (* ORD(Ch) = 65 *)

Functions and Operators for ordinal types The CHR function returns the character represented internally by a given cardinal value. VAR Ch : CHAR; n : [0..9];... n := 3; (* Conversion of a cardinal value between 0 and 9 into the corresponding character *) Ch := CHR(ORD("0")+n);

Functions and Operators for ordinal types The VAL function returns the value in a specified type represented internally by a given cardinal value. TYPE Day = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); VAR Ch : CHAR; Today : Day;... Today := VAL(Day,2); (* Wednesday *) Ch := VAL(CHAR,65); (* "A" *) (* VAL(CHAR,n) = CHR(n) *)

Functions and Operators for ordinal types The INC and DEC procedures can be used to increment or decrement ordinal variables. TYPE Day = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); VAR Ch : CHAR; Today : Day;... Today:=Wed ; INC(Today); (* Today = Thu *) Ch := "A"; INC(ch,32); (* ch = "a" *)

REAL Type Approximate representation for real numbers Possible values and accuracy: –implementation dependant –majority of computer systems : IEEE754. Single precision (32 bit) "REAL" –Smallest value: –Largest value : –Relative error: < Double precision (64 bit) "LONGREAL" –Smallest value: –Largest value : –Relative error: <

REAL Type Operators and type conversions Arithmetic operators : +, -, *, / Relational operators : =, #, >, >=, <, <= Expressions : No mixing of types Type conversions: –From REAL to INTEGER : Truncation ! IntVar := VAL(INTEGER,RealVal); IntVar := TRUNC(RealVal); –From INTEGER to REAL : RealVal := VAL(REAL,IntVal); RealVal := FLOAT(IntVal);

REAL Type Example CONST BEFperEURO = ; VAR EUROValue : REAL; BEFValue : CARDINAL; RealBef : REAL;... RealBEF := EUROValue * BEFperEURO; BEFValue := VAL(CARDINAL, RealBEF+0.5)

Expression Syntax SimpleExpression Relational OperatorSimpleExpression Term Additive Operator + -

Term Syntax Factor Multiplicative Operator

Factor Syntax

Expression Evaluation Order of evaluation: 1Factor 2Term 3Simple Expression 4Expression Parentheses can modify order Evaluation is done from left to right, in a lazy fashion