Table Of ContentJim Welsh and
John Elder
Introduction to
Modula-2
PRENTICE HALL
INTERNATIONAL
SERIES IN
COMPUTER
SCIENCE C.A.R. HOARE SERIES EDITOR
INTRODUCTION TO
MODULA-2
Prentice Hall International
Series in Computer Science
C. A. R. Hoare, Series Editor
BACKHOUSE, R. C., Program Construction and Verification
BACKHOUSE, R. C., Syntax ofProgramming Languages, Theory and Practice
de BAKKER, J. W., Mathematical Theory ofProgram Correctness
BJORNER, D., and JONES, C. B., Formal Specification and Software Development
BORNAT, R., Programmingfrom First Principles
CLARK, K. L., and McCABE, F. G., micro-PROLOG: Programming in Logic
DROMEY, R. G., How to Solve it by Computer
DUNCAN, F., Microprocessor Programming and Software Development
ELDER, J., Construction ofData Processing Software
GOLDSCHLAGER, L., and LISTER, A., Computer Science: A Modern Introduction
HAYES, I. (Editor), Specification Case Studies
HEHNER, E. C. R., The Logic ofProgramming
HENDERSON, P., Funct,onal Programming: Application and Implementation
HOARE, C. A. R., Communicating Sequential Processes
HOARE, C. A. R., and SHEPHERDSON, J. C. (Editors), Mathematical Logic and
Programming Languages
INMOS LTD, Occam Programming Manual
JACKSON, M. A., System Development
JOHNSTON, H., Learning to Program
JONES, C. B.,Systematic Software Development Using VDM
JONES, G., Programming in Occam
JOSEPH, M., PRASAD, V. R.,and NATARAJAN, N.,A Multiprocessor Operating
System
LEW, A., Computer Science: A Mathematical Introduction
MacCALLUM, I., Pascalfor the Apple
MacCALLUM, I., UCSD Pascalfor the IBM PC
MARTIN, J. J., Data Types and Data Structures
POMBERGER, G., Software Engineering and Modula-2
REYNOLDS, J. C., The CraftofProgramming
SLOMAN, M., and KRAMER, J., Distributed Systems and Computer Networks
TENNENT, R. D., Principles ofProgramming Languages
WELSH, J., and ELDER, J., Introduction to Modula-2
WELSH, J., and ELDER, J., Introduction to Pascal, 2nd Edition
WELSH, J., ELDER, J., and BUSTARD, D., Sequential Program Structures
WELSH, J., and HAY, A.,A Model Implementation ofStandard Pascal
WELSH, J., and McKEAG, M.. Structured System Programming
INTRODUCTION TO
MODULA-2
JIM WELSH
University of Queensland,
Australia
JOHN ELDER
Queen's University of Belfast,
Northern Ireland
PRENTICE HALL
NewYork London Sydney Tokyo
Welsh, Jim, 1943-
Introduction to Modula-2.
(Prentice Hall International Series in Computer
Science)
Includes index.
1. Modula-2 (Computer program language) I. Elder,
John, 1949— . 11. Title. III. Title: Introduction
to Modula-Two. IV. Series.
QA76.73.M63W45 1987 005.13'3 87-11521
ISBN 0-13-488610-0 (pbk.)
British Library Cataloguing in Publication Data
Welsh, Jim
Introduction to Modula-2.—(Prentice
Hall International Series in Computer
Science).
1. Modula-2 (Computer program language)
I. Title II. Elder, John, 1949
-
005.13'3 QA76.73.M63
ISBN 0-13-488610-0
© 1987 Prentice Hall International (UK) Ltd
All rights reserved. No part of this publication may be
reproduced, stored in a retrieval system, or transmitted,
in any form or by any means, electronic, mechanical,
photocopying, recording or otherwise, without the prior
permission of Prentice Hall International (UK) Ltd.
For permission within the United States of America
contact Prentice Hall Inc., Englewood Cliffs, NJ 07632.
Prentice Hall is an imprint of the Simon & Schuster
International Group.
Printed and bound in Great Britain for
Simon & Schuster International Group,
66 Wood Lane End, Hemel Hempstead,
Hertfordshire, HP2 4RG
by A. Wheaton and Company Limited, Exeter
1 2345 91 90 89 88 87
0-l'3-4flflblQ-D
Contents
PREFACE, XI
CHAPTER 1 COMPUTERS AND PROGRAMMESG, 1
• The computer, 1
•Writing a computer program, 3
• Running a computer program, 4
• Language implementations, 6
• Programming objectives, 6
• Correctness, 6
• Clarity, 7
• Efficiency, 8
CHAPTER 2 NOTATIONS AND BASIC CONCEPTS, 9
• Which Modula-2?, 9
• Extended Backus-Naur form, 10
• The vocabulary of Modula-2,12
• Numbers, 13
• Identifiers, 14
• Strings, 16
• Comments, 17
• Basic program structure, 18
• Exercises, 21
CHAPTER 3 DATA TYPES AND DECLARATIONS, 22
• Data types, 22
• The type INTEGER, 23
• The type CARDINAL, 25
•The typeLONGDNT, 25
• The type REAL, 26
• The type CHAR, 27
• The type BOOLEAN, 29
• Enumerated types, 31
• Subrange types, 32
• Data declarations, 34
• Constants and constant declarations, 34
• Type declarations, 35
• Variable declarations, 37
• Uniqueness and order of declarations, 38
• Exercises, 39
CHAPTER 4 STATEMENTS, EXPRESSIONS
AND ASSIGNMENTS, 41
• Statements,41
• Expressions, 42
• The assignment statement, 46
• Exercises, 49
CHAPTER 5 INPUT AND OUTPUT OF DATA, 51
• Transferring data to and firom the program, 51
• Output from Modula-2 programs, 52
• Input to Modula-2 programs, 55
• Import-lists, 58
• Case study 1,59
• Alternative input and output streams, 63
• Exercises, 65
CHAPTER 6 BASIC CONTROL STRUCTURES, 66
• Statement sequences, 66
• Conditional statements, 68
• The if-statement, 68
• Case study 2,72
• The case-statement, 74
• Case study 3,77
• Repetitive statements, 80
• The while-statement, 81
• The repeat-statement, 83
• The for-statement, 84
• Nested loops, 88
• Case study 4, 89
• The loop-statement, 93
• Exercises, 97