Modula 2 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Modula 2

Description:

Variables. Actions. Assignment statement. Control statements. A first ... to a type. Variables. Name. Type. Type = set of all values. the variable can have ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 22
Provided by: dtwma
Category:
Tags: modula | var

less

Transcript and Presenter's Notes

Title: Modula 2


1
Chapter 2.3
  • Modula 2
  • An Introduction

2
Summary
  • First example
  • Terminal symbols
  • Comments
  • Data
  • Constants
  • Variables
  • Actions
  • Assignment statement
  • Control statements

3
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.
4
Terminal Symbols
  • Upper- and Lower-case letters
  • a b c d e ... A B C D E ...
  • Digits
  • 0 1 2 3 4 5 6 7 8 9
  • Special signs
  • , . - / gt lt ( )
  • Composed signs
  • ( ) gt lt
  • Reserved Words
  • MODULE BEGIN END IMPORT FROM

5
Role of a Linker
FROM InOut IMPORT WriteString, WriteLn FROM
RealInOut IMPORT WriteReal, ReadReal
6
Comments
( This program computes the area of a circle )
Comment
(
Text not containing ( nor )
Comment
7
Summary
  • First example
  • Terminal symbols
  • Comments
  • Data
  • Constants
  • Variables
  • Actions
  • Assignment statement
  • Control statements

8
ConstantsData whose value can not change during
program execution
  • Literal Constants
  • Only a value
  • Named Constants
  • A 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
9
Syntax of Constants
10
Whole Number Literal
Whole Number Literal
Decimal Literal
Octal Literal
Hexadecimal Literal
11
Decimal Literal
Decimal Literal
DecimalDigit
DecimalDigit
12
Octal Literal
10B
8B
13
Hexadecimal Literal
Hexadecimal Literal
HexadecimalDigit
DecimalDigit
H
HexadecimalDigit
DecimalDigit
F
10FFFH
FFFFFH
0FFFFFH
1000F
14
Real Literal
15
String Literal
'
'
Text not containing '
"The area of this circle is "
'The area of this circle is '
"This string contains a ' "
'but no " '
16
Why named constants ?
Area 6 LengthLength ... Tax (Price 6)
DIV 100
Area 21 LengthLength ... Tax (Price
21) DIV 100
CONST VAT 6 ... Area 6 LengthLength ... T
ax (Price VAT) DIV 100
CONST VAT 21 ... Area 6
LengthLength ... Tax (Price VAT) DIV 100
17
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
18
Types in Modula 2
  • Simple Types values cant 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

19
Summary
  • First example
  • Terminal symbols
  • Comments
  • Data
  • Constants
  • Variables
  • Actions
  • Assignment statement
  • Control statements

20
Assignments
area pi radius radius
  • 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

21
Control StatementsInfluence the order of
execution of instructions
  • Selection
  • IF THEN ELSE
  • CASE
  • Iteration
  • WHILE
  • REPEAT
  • FOR
  • LOOP
  • Procedure Call

WriteString("Hello, I comput WriteLn WriteString(
"Give me the rad ReadReal(radius) area pi
radius radius WriteString("The area of
thi WriteReal(area,0) WriteString(" square
meters WriteLn
Write a Comment
User Comments (0)
About PowerShow.com