Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data...

21
Chapter 2.3 Modula 2 An Introduction

Transcript of Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data...

Page 1: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Chapter 2.3

Modula 2

An Introduction

Page 2: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Summary• First example

• Terminal symbols

• Comments

• Data– Constants– Variables

• Actions– Assignment statement– Control statements

Page 3: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

A first example

MODULE FirstExample;(* This program computes the area of a circle *)FROM InOut IMPORT WriteString, WriteLn;FROM RealInOut IMPORT WriteReal, ReadReal;CONST pi = 3.1416;VAR radius, area : REAL;BEGIN WriteLn; WriteLn; WriteString("Hello, I compute the area of a circle for you"); WriteLn; WriteString("Give me the radius in meters "); ReadReal(radius); area := pi * radius * radius; WriteString("The area of this circle is "); WriteReal(area,0); WriteString(" square meters "); WriteLn;END FirstExample.

Page 4: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Terminal Symbols

• Upper- and Lower-case lettersa b c d e ... A B C D E ...

• Digits0 1 2 3 4 5 6 7 8 9

• Special signs; , . | + - * / = # > < ( ) [ ] { }

• Composed signs

(* *) := >= <= • Reserved Words

MODULE BEGIN END IMPORT FROM

Page 5: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Source B Source C Source DSource A

Reloc. DReloc. A Reloc. B Reloc. C

Compiler X Compiler Y Assembler

LINKER

Object Code A+B+C+D

Role of a Linker

FROM InOut IMPORT WriteString, WriteLn;FROM RealInOut IMPORT WriteReal, ReadReal;

Page 6: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Comments(* This program computes the area of a circle *)

(* Text not containing (* nor *)

Text not containing (* nor *) *)

Comment

Comment

Page 7: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Summary• First example

• Terminal symbols

• Comments

• Data– Constants– Variables

• Actions– Assignment statement– Control statements

Page 8: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

ConstantsData whose value can not change during program

execution• Literal Constants

Only a value:

• Named ConstantsA name and a value:

The value can be a constant expression

3.1416

"The area of this circle is "

pi = 3.1416

twopi = pi * 2.0

Page 9: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Syntax of Constants

= Constant ExpressionIdentifier

Constant Declaration

Whole Number LiteralLiteral

Real Literal

String Literal

Pointer Literal

Page 10: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Whole Number Literal

Whole Number Literal

+

-

Decimal Literal

Octal Literal

Hexadecimal Literal

Page 11: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Decimal Literal

Decimal Literal

DecimalDigit

0 1 2 3 4 5 6 7 8 9

DecimalDigit

Page 12: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Octal LiteralOctal Literal

OctalDigit

0 1 2 3 4 5 6 7

OctalDigit

B

10B 8B

Page 13: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Hexadecimal Literal

Hexadecimal Literal

HexadecimalDigit H

A B C D E FDecimalDigit

DecimalDigit

HexadecimalDigit

10FFFH

0FFFFFH 1000F

FFFFFH

Page 14: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Real Literal

3.1416

3.1416E0

0.31416E1 0.31416 E1

31416E-4

3,1416

Page 15: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

String Literal

"The area of this circle is "

'The area of this circle is '

"This string contains a ' " 'but no " '

" Text not containing " "

' Text not containing ' '

Page 16: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Why named constants ?Area := 6 * Length*Length

...Tax := (Price * 6) DIV 100

CONST VAT = 6;...Area := 6 * Length*Length...Tax := (Price * VAT) DIV 100

Area := 21 * Length*Length...Tax := (Price * 21) DIV 100

CONST VAT = 21;...Area := 6 * Length*Length...Tax := (Price * VAT) DIV 100

Page 17: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

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 dataas well as the operations that can be performed on the data

Page 18: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

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

Page 19: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Summary• First example

• Terminal symbols

• Comments

• Data– Constants– Variables

• Actions– Assignment statement– Control statements

Page 20: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Assignments

• Assignment operator :=– Fundamentally different from the = sign– The = sign denotes a timeless relation– The := operator describes a particular action:

• The right side expression is evaluated• Its value is stored in the left side variable

– Illustrative example : a := a + 1

area := pi * radius * radius;

Page 21: Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.

Control StatementsInfluence the order of execution of

instructions• Selection– IF THEN ELSE– CASE

• Iteration– WHILE– REPEAT– FOR– LOOP

• Procedure Call

WriteString("Hello, I computWriteLn;WriteString("Give me the radReadReal(radius);area := pi * radius * radiusWriteString("The area of thiWriteReal(area,0);WriteString(" square meters WriteLn;