• No results found

Jensen K., Wirth N. PASCAL User Manual and Report.pdf

N/A
N/A
Protected

Academic year: 2021

Share "Jensen K., Wirth N. PASCAL User Manual and Report.pdf"

Copied!
175
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Lecture Notes in

Computer Science

Edited by G. Goos and J. Hartmanis

18

Kathleen Jensen

Niklaus Wirth

PASCAL

(3)

C. Moler • G. SeegmQIler • N. Wirth

Ms.,Kathleen Jensen

Prof. Dr. Niklaus Wirth

Institut fLir Informatik

ETH Zerich

Clausiusstra6e 55

C H - 8 0 0 6 Z0rich

Library of Congress Cataloging in Publication Data

Jensen, Kathleen, 1949-

PASCAL: user manual and report.

(Lecture notes in computer science, v. 18)

Bibliography: p.

1. PASCAL (Computer program language). I. Wirth, Niklaus, joint author. II. Title. IIIo Series.

QA76.73 .P35J46 001.6'424 74-16327

AMS Subject Classifications (1970): 68-02, 68 A 0 5

CR Subject Classifications (1974): 4.2, 4.22

ISBN 3-540-06950-X Springer-Verlag Berlin • Heidelberg • New York

ISBN 0-387-06950-X Springer-Verlag New York • Heidelberg • Berlin

This work is subject to copyright. All rights are reserved, whether the whole

or part of the material is concerned, specifically those of translation,

reprinting, re-use of illustrations, broadcasting, reproduction by photo-

copying machine or similar means, and storage in data banks.

Under § 54 of the German Copyright Law where copies are made for other

than private use, a fee is payable to the publisher, the amount of the fee to

be determined by agreement with the publisher.

(4)

T h i s m a n u a l i s d i r e c t e d t o t h o s e w h o h a v e p r e v i o u s l y a c q u i r e d s o m e p r o g r a m m i n g s k i l l . T h e i n t e n t i o n is t o p r o v i d e a m e a n s o f l e a r n i n g P a s c a l w i t h o u t o u t s i d e g u i d a n c e . I t i s b a s e d o n T h e ~ r p a r a m m i n o L a n a u a a e E a s c a l ( ~ e v i s e d ~ e a o r t ) I l l - - t h e b a s i c d e f i n i t i o n o f P a s c a l a n d c o n c i s e r e f e r e n c e m a n u a l f o r t h e e x p e r i e n c e d P a s c a l p r o g r a m m e r . T h e l i n e a r s t r u c t u r e o f a b o o k is by no m e a n s i d e a l f o r i n t r o d u c i n g a l a n g u a g e , w h e t h e r i t b e a f o r m a l o r n a t u r a l o n e . N e v e r t h e l e s s , it is r e c o m m e n d e d t o f o l l o w t h e g i v e n o r g a n i z a t i o n , p a y i n g p a r t i c u l a r a t t e n t i o n t o t h e example p r o g r a m s , a n d t h e n t o r e r e a d t h o s e s e c t i o n s c a u s i n g d l f f i c u l t e s . O n e m a y w i s h , h o w e v e r , t o r e f e r e n c e c h a p t e r 12 if t r o u b l e s a r i s e c o n c e r n i n g t h e i n p u t a n d o u t p u t c o n v e n t i o n s o f t h e p r o g r a m s . T h e m a n u a l w a s p r e p a r e d a s e f i l e o n a c o m p u t e r , t h a t i s . a s a s e q u e n c e o f c h a r a c t e r s o f a s i n g l e t y p e f o n t . T h i s i s v e r y c o n v e n i e n t f o r t h e p u r p o s e s o f u p d a t i n g : u n f o r t u n a t e l Y ~ it is s o m e t i m e s a bit a w k w a r d t o r e a d . T h e r e a d e r is a s k e d t o be i n d u l g e n t w i t h t h e a b s e n c e o f s u b - a n d s u p e r s c r i p t s ( e . g . m r a i s e d t o t h e p o w e r n is d e n o t e d by m * * n ) . C h a p t e r s 0 - - 1 2 d e f i n e t h e l a n g u a g e P a s c a l a n d s e r v e a s a s t a n d a r d f o r b o t h t h e i m p l e m e n t o r a n d t h e p r o g r a m m e r . T h e i m p l e m e n t e r m u s t r e g a r d t h e t a s k o f r e c o g n i z i n g S t a n d a r d P a s c a l a s t h e minmum r e q u i r e m e n t o f h i s s y s t e m , w h i l e t h e p r o g r a m m e r w h o i n t e n d s his p r o g r a m s t o be t r a n s f e r a b l e f r o m o n e i n s t a l l a t i o n t o a n o t h e r s h o u l d u s e o n l y f e a t u r e s d e s c r i b e d as S t a n d a r d P a s c a l . O n t h e o t h e r h a n d , a n y i m p l e m e n t a t i o n m a y ( a n d u s u a l l y d o e s ) go b e y o n d t h e m i n i m u m . C h a p t e r s 13 a n d 14 d o c u m e n t t h e i m p l e m e n t a t i o n o f P a s c a l o n t h e C D C 6 0 0 0 m a c h i n e . C h a p t e r 13 d e s c r i b e s t h e a d d i t i o n a l f e a t u r e s o f t h e l a n g u a g e P A S C A L 6 0 0 0 , w h e r e a s c h a p t e r 14 is d e v o t e d t o t h e u s e o f t h e c o m p i l e r a n d t h e s y s t e m u n d e r t h e o p e r a t i n g s y s t e m S C O P E . T h e e f f o r t s o f m a n y g o i n t o t h i s m a n u a l , e n d we e s p e c i a l l y t h a n k t h e m e m b e r s o f t h e I n s t i t u t f u e r I n f o r m a t i k , ETH Z u r i c h . e n d J o h n L a r m o u t h , R u d y S c h i l d , O l i v i e r L e c e r m e , a n d P i e r r e D e s j a r d i n s f o r t h e i r c r i t i c i s m , s u g g e s t i o n s , a n d e n c o u r a g e m e n t . O u r i m p l e m e n t a t i o n o f P a s c s l - - w h i c h m a d e t h i s m a n u a l b o t h p o s s i b l e a n d n e c e s s a r y - - i s t h e w o r k o f U r s A m m a n n , a i d e d by H e l m u t S a n d m a y r . J u n e 1 9 7 4 K a t h l e e n d e n s e n N i k l a u s W i r t h ETH Z u r i c h S w i t z e r l a n d

(5)

U S E R M A N U A L by K. Jensen and N. Wirth O. I n t r o d u c t i o n . . . I. N o t a t i o n a n d V o c a b u l a r y . . . 2. T h e C o n c e p t of D a t a . . . A. T h e t y p e B o o l e a n . . . B. T h e t y p e i n t e g e r . . . . . . C. T h e t y p e r e a l . . . D. T h e t y p e c h a r . . . 3. T h e P r o g r a m H e a d i n g a n d t h e D e c l a r a t i o n P a r t . . . A. T h e p r o g r a m h e a d i n g . . . B. T h e l a b e l d e c l a r a t i o n p a r t . . . C. T h e c o n s t a n t d e f i n i t i o n p a r t . . . D. T h e t y p e d e f i n i t i o n p a r t . . . E. T h e v a r i a b l e d e c l a r a t i o n p a r t . . . F. T h e p r o c e d u r e a n d f u n c t i o n d e c l a r a t i o n p a r t . . . 4. T h e C o n c e p t o f A c t i o n . . . A. T h e a s s i g n m e n t s t a t e m e n t . . . B. T h e c o m p o u n d s t a t e m e n t . . . C. R e p e t i t i v e s t a t e m e n t s . . . C.I T h e w h i l e s t a t e m e n t . . . C . 2 T h e r e p e a t s t a t e m e n t . . . C . 3 T h e f o r s t a t e m e n t . . . D. C o n d i t i o n a l s t a t e m e n t s . . . D . I T h e i f s t a t e m e n t . . . D . 2 T h e c a s e s t a t e m e n t . . . E. T h e g o t o s t a t e m e n t . . . 5. S c a l a r a n d S u b r a n g e T y p e s . . . A. S c a l a r t y p e s . . . B. S u b r a n g e t y p e s . . . 6. S t r u c t u r e d T y p e s i n G e n e r a l - - T h e A r r a y i n P a r t i c u l a r . 7. R e c o r d T y p e s . . . A. T h e w i t h s t a t e m e n t . . . 8. S e t T y p e s . . . 9. ~ i l e T y p e s . . . A. T e x t f i l e s . . . B. T h e s t a n d a r d f i l e s " i n p u t " a n d " o u t p u t " . . . 10. P o i n t e r T y p e s . . . 11. P r o c e d u r e s a n d F u n c t i o n s . . . A. P r o c e d u r e s . . . B. F u n c t i o n s . . . C. R e m a r k s . . . 12. I n p u t a n d O u t p u t . . . A. T h e p r o c e d u r e r e a d . . . B. T h e p r o c e d u r e w r i t e . . . 13. P A S C A L 6 0 0 0 - 3 . 4 . . . A. E x t e n s i o n s t o t h e l a n g u a g e P a s c a l . . . A . I S e g m e n t e d f i l e s . . . 3 9 12 12 13 14 14 16 16 16 16 18 18 19 2 O 2 0 21 2 2 2 2 2 3 2 3 2 6 2 6 31 31 3 4 3 4 35 3 6 4 2 4 7 5 O 55 5 7 5 9 6 2 6 7 6 7 7 8 8 2 8 4 8 4 8 6 8 8 8 8 8 8

(6)

A . 2 E x t e r n a l p r o c e d u r e s . . . B. S p e c i f i c a t i o n s l e f t u n d e f i n e d i n t h e p r e c e d i n g c h a p t e r s . B.I T h e p r o g r a m h e a d i n g a n d e x t e r n a l f i l e s . . . B . 2 R e p r e s e n t a t i o n o f f i l e s . . . B . 3 T h e s t a n d a r d t y p e s . . . B . 4 T h e s t a n d a r d p r o c e d u r e w r i t e . . . C. R e s t r i c t i o n s . . . D. A d d i t i o n a l p r e d e f i n e d t y p e s , p r o c e d u r e s , a n d f u n c t i o n s . D . I A d d i t i o n a l p r e d e f i n e d t y p e s . . . D . 2 A d d i t i o n a l p r e d e f i n e d p r o c e d u r e s a n d f u n c t i o n s . . 14. H o w t o U s e t h e P A S C A L 6 0 0 0 - 3 . 4 S y s t e m . . . A. C o n t r o l s t a t e m e n t s . . . B. C o m p i l e r o p t i o n s . . . C. E r r o r m e s s a g e s . . . C.I C o m p i l e r . . . C . 2 R u n - t i m e . . . R e f e r e n c e s . . . . . . . . . . A p p e n d i x A A p p e n d i x B A p p e n d i x C A p p e n d i x D A p p e n d i x E A p p e n d i x F S t a n d a r d P r o c e d u r e s a n d F u n c t i o n s . . . S u m m a r y o f O p e r a t o r s . . . T a b l e s . . . S y n t a x . . . E r r o r N u m b e r S u m m a r y . . . P r o g r a m m i n g E x a m p l e s . . . I n d e x . . . 9 O 91 91 9 2 9 3 9 6 9 7 9 7 9 7 9 8 I 0 0 1 0 0 1 0 0 I O 2 1 0 2 I 0 2 I 0 4 I 0 5 I O 8 I 0 9 1 1 0 1 1 9 1 2 2 1 2 6 b y N. W i r t h P r e f a c e t o t h e R e v i s e d R e p o r t . . . 1. I n t r o d u c t i o n . . . 2. S u m m a r y o f t h e l a n g u a g e . . . 3. N o t a t i o n , t e r m i n o l o g y , a n d v o c a b u l a r y . . . 4. I d e n t i f i e r s , N u m b e r s a n d S t r i n g s . . . 5. C o n s t a n t d e f i n i t i o n s . . . 6. D a t a t y p e d e f i n i t i o n s . . . 6 . 1 . S i m p l e t y p e s . . . 6 . 2 . S t r u c t u r e d t y p e s . . . 6 . 3 . P o i n t e r t y p e s . . . 7. D e c l a r a t i o n s a n d d e n o t a t i o n s o f v a r i a b l e s . . . 7 . 1 . E n t i r e v a r i a b l e s . . . 7 . 2 . C o m p o n e n t v a r i a b l e s . . . 7 . 3 . R e f e r e n c e d v a r i a b l e s . . . 8. E x p r e s s i o n s . . . 8 . 1 . O p e r a t o r s . . . 8 . 2 . F u n c t i o n d e s i g n a t o r s . . . 9. S t a t e m e n t s . . . 9 . 1 . S i m p l e s t a t e m e n t s . . . 9 . 2 . S t r u c t u r e d s t a t e m e n t s . . . 10. P r o c e d u r e d e c l a r a t i o n s . . . 1 0 . 1 . S t a n d a r d p r o c e d u r e s . . . 11. F u n c t i o n d e c l a r a t i o n s . . . 1 1 . 1 . S t a n d a r d f u n c t i o n s . . . 1 3 3 1 3 6 1 3 7 1 4 0 1 4 0 141 1 4 2 1 4 2 1 4 3 1 4 5 1 4 6 1 4 7 1 4 7 1 4 8 1 4 8 1 4 9 151 151 1 5 2 1 5 3 1 5 8 1 6 0 162 1 6 3

(7)

12. I n p u t a n d O u t p u t . . . 13. P r o g r a m s . . . 14. A s t a n d a r d f o r i m p l e m e n t a t i o n a n d p r o g r a m i n t e r c h a n g e . . 15. I n d e x . . . 1 6 4 167 168 169

(8)

~ N T R O D U C T I O N M u c h o f t h e f o l l o w i n g t e x t a s s u m e s t h e r e a d e r h a s a m i n i m a l g r a s p o f c o m p u t e r t e r m i n o l o g y a n d a " f e e l i n g " f o r t h e s t r u c t u r e o f e p r o g r a m . The p u r p o s e o f t h i s s e c t i o n i s t o s p a r k t h a t i n t u i t i o n . { p r o g r a m 0 . 1 a s s u m i n g a n n u a l i n f l a t i o n r a t e s o f 9 , 8 , a n d 10 p e r c e n t . f i n d t h e f a c t o r b y w h i c h t h e f r a n k , d o l l a r , p o u n d s t e r l i n g , m a r k , o r g u i l d e r w i l l h a v e b e e n d e v a l u e d i n 1. 2 . . . . n y e a r s . } ~ r o c r a m i n f l a t i o n ( o u t p u t ) ; # o n s t n = 10 ; v a r i : i n t e g e r ; w l . w 2 ~ o W 3 : r e a l ; b e c i n i := O; w l := 1 . 0 ; w 2 := 1.0; w 3 := 1 . 0 ; r_@~.~at i := i + I ; w I : = w I * 1 . 0 7 ; w 2 : = w2 * 1 . 0 8 ; w3 : = W3 * 1 . 1 0 ; w r i t e l n (i .w l , w 2 . w 3 ) i = n e n d . 1 1 . 0 9 0 0 0 0 0 0 0 0 0 0 e + 0 0 1 . 0 8 0 0 0 0 0 0 0 0 0 0 e + 0 0 1 . 1 0 0 0 0 0 0 0 0 0 0 0 e + 0 0 2 1 . 1 4 4 9 0 0 0 0 0 0 0 0 e + 0 0 1 . 1 6 6 4 0 0 0 0 0 0 0 0 e + 0 0 1 . 2 1 0 0 0 0 0 0 0 0 0 0 e + 0 0 3 1 . 2 2 5 0 4 3 0 0 0 0 0 0 e + 0 0 1 . 2 5 9 7 t 2 0 0 0 0 0 0 e + 0 0 1 . 3 3 1 0 0 0 0 0 0 0 0 0 e + 0 0 4 1 . 3 1 0 7 9 6 0 1 0 0 0 0 e + 0 0 1 . 3 6 0 4 8 8 9 6 0 0 0 0 e + 0 0 1 . 4 6 4 1 0 0 0 0 0 0 0 0 e + 0 0 5 1 . 4 0 2 5 5 1 7 3 0 7 0 0 e + 0 0 1 . 4 6 9 3 2 8 0 7 6 U O O e + 0 0 1 . 6 1 0 5 1 0 0 0 0 0 0 0 e + 0 0 6 1 . 5 0 0 7 3 0 3 5 1 8 4 9 e + 0 0 1 . 5 8 6 8 7 4 3 2 2 9 4 4 e + 0 0 1 . 7 7 1 5 6 1 0 0 0 0 0 0 e + 0 0 7 1 . b O b T U 1 4 7 6 4 7 8 e + O 0 1 . 7 1 3 8 2 4 2 6 8 7 7 9 e + 0 0 1 . 9 4 8 7 1 7 1 0 0 0 0 0 e + 0 0 1 . 7 1 8 1 8 6 1 7 9 8 3 2 e + 0 0 1 . 8 5 0 9 3 0 2 1 0 2 8 2 e + 0 0 2 . 1 4 3 5 8 ~ 8 1 0 0 0 0 e + 0 0 9 1 . 8 3 8 4 5 9 2 1 2 4 2 0 e + 0 0 1 . 9 9 9 0 0 4 6 2 7 1 0 4 e + 0 0 2 . 3 5 7 9 4 7 6 9 1 0 0 0 e + 0 0 10 1 . 9 6 9 1 5 t 3 5 9 2 9 0 e + 0 0 2 . 1 5 8 9 2 4 9 9 7 2 9 3 e + 0 0 2 . 5 9 3 7 4 2 4 6 0 t 0 0 e + 0 0 A n & 3 o o r i t b j ~ o r c o m p u t e r p r o g r a m c o n s i s t s o f t w o e s s e n t i a l p a r t s , a d e s c r i p t i o n o f ~ c t i o n s w h i c h e r e t o b e p e r f o r m e d , a n d a d e s c r i p t i o n o f t h e ~@_~&o w h i c h is m a n i p u l a t e d b y t h e s e a c t i o n s . A c t i o n s a r e d e s c r i b e d b y s o - c a l l e d ~ t a t e m e n t s , e n d d a t a i s d e s c r i b e d b y s o - c a l l e d ~ c l ~ r a ~ L ~ z n s e n d ~ L ~ f i n i t i o n s . T h e p r o g r a m is d i v i d e d i n t o a ~ e a d i n ~ a n d a b o d y , c a l l e d a b 3 o c k . T h e h e a d i n g g i v e s t h e p r o g r a m s name a n d l i s t s i t s p a r a m e t e r s . ( T h e s e e r e ( f i l e ) v a r i a b l e s a n d r e p r e s e n t t h e a r g u m e n t s a n d r e s u l t s o f t h e c o m p u t a t i o n . S e e c h a p t e r 1 3 . ) T h e f i l e " o u t p u t " i s e c o m p u l s o r y p a r a m e t e r . T h e b l o c k c o n s i s t s o f s i x s e c t i o n s , w h e r e a n y e x c e p t t h e l e s t may b e e m p t y . I n t h e r e q u i r e d o r d e r t h e y e r e :

(9)

<type d e f i n i t i o n p a r t > < v a r i a b l e d e c l a r a t i o n p a r t > < p r o c e d u r e and f u n c t i o n d e c l a r a t i o n p a r t > < s t a t e m e n t p a r t > T h e f i r s t s e c t i o n l i s t s a l l l a b e l s d e f i n e d in t h i s b l o c k . T h e s e c o n d s e c t i o n d e f i n e s s y n o n y m s f o r c o n s t a n t s : i . e . it i n t r o d u c e s i d e n t i f i e r s t h a t m a y l a t e r be u s e d in p l a c e o f t h o s e c o n s t a n t s . T h e t h i r d c o n t a i n s t y p e d e f i n i t i o n s : e n d t h e f o u r t h . v a r i a b l e d e f i n i t i o n s . T h e f i f t h s e c t i o n d e f i n e s s u b o r d i n a t e p r o g r a m p a r t s ( i . e . p r o c e d u r e s a n d f u n c t i o n s ) . T h e s t a t e m e n t p a r t s p e c i f i e s t h e a c t i o n s t o be t a k e n . T h e a b o v e p r o g r a m o u t l i n e is m o r e p r e c i s e l y e x p r e s s e d in a s y n t a x ~ a p r a m . S t a r t i n g a t t h e diagram named p r o g r a m , a p a t h t h r o u g h t h e d i a g r a m d e f i n e s a s y n t a c t i c a l l y c o r r e c t p r o q r a m . E a c h b o x r e f e r e n c e s a d i a g r a m by t h a t n a m e . w h i c h is t h e n u s e d t o d e f i n e i t s m e a n i n g . T e r m i n a l s y m b o l s ( t h o s e a c t u a l l y w r i t t e n in a P a s c a l p r o g r a m ) a r e in r o u n d e d e n c l o s u r e s . ( S e e a p p e n d i x D f o r t h e f u l l s y n t a x d i a g r a m o f P a s c a l . )

(10)

block

9

~1 .... i~.di,l,~o~ I-

j ~ I ~~G-o

~

~ - ~ P R O C E D U I I ~ ~ - - I

p ... ter list I

"i

F i g u r e O.a Syntax d i a g r a m s d e f i n i n g the

(11)

E n g l i s h w o r d s e n c l o s e d b e t w e e n t h e a n g u l a r b r a c k e t s < a n d >. T h e s e w o r d s a r e s u g g e s t i v e o f t h e n a t u r e o r m e a n i n g o f t h e c o n s t r u c t . A s e q u e n c e o f c o n s t r u c t s (I o r m o r e e l e m e n t s ) e n c l o s e d b y t h e m e t e - b r a c k e t s { a n d } i m p l y t h e i r r e p e t i t i o n z e r o o r m o r e t i m e s . ( F o r t h e B N F o f P a s c a l . s e e a p p e n d i x D . ) A s a n e x a m p l e , t h e c o n s t r u c t < p r o g r a m > o f f i g u r e O . a i s d e f i n e d b y t h e f o l l o w i n g f o r m u l a s , c a l l e d " p r o d u c t i o n s " : < p r o g r a m > : : = < p r o g r a m h e a d i n g > < b l o c k > . < p r o g r a m h e a d i n g > ::~ ~ r o m r a ~ < i d e n t i f i e r > ( < f i l e i d e n t i f i e r > { . < f i l e i d e n t i f i e r > } ) ; < f i l e i d e n t i f i e r > ::= < i d e n t i f i e r > E a c h p r o c e d u r e ( f u n c t i o n ) h a s a s t r u c t u r e s i m i l a r t o a p r o g r a m ; i . e . e a c h c o n s i s t s o f a h e a d i n g a n d a b l o c k . H e n c e . p r o c e d u r e s m a y b e d e c l a r e d ( n e s t e d ) w i t h i n o t h e r p r o c e d u r e s . L a b e l s , c o n s t a n t s y n o n y m s , t y p e , v a r i a b l e , a n d p r o c e d u r e d e c l a r a t i o n s a r e l o c a l t o t h e p r o c e d u r e i n w h i c h t h e y a r e d e c l a r e d , T h a t i s , t h e i r i d e n t i f i e r s h a v e s i g n i f i c a n c e o n l y w i t h i n t h e p r o g r a m t e x t w h i c h c o n s t i t u t e s t h e p r o c e d u r e d e c l a r a t i o n a n d w h i c h is c a l l e d t h e ~ e o o e o f t h e s e i d e n t i f i e r s . S i n c e p r o c e d u r e s m a y b e n e s t e d , s o m a y s c o p e s . O b j e c t s w h i c h a r e d e c l a r e d i n t h e m a i n p r o g r a m , i . e . n o t l o c a l t o s o m e p r o c e d r u e , a r e c a l l e d ~ I o b a l a n d h a v e s i g n i f i c a n c e t h r o u g h o u t t h e e n t i r e p r o g r a m . S i n c e b l o c k s m a y b e n e s t e d w i t h i n o t h e r b l o c k s b y p r o c e d u r e a n d f u n c t i o n d e c l a r a t i o n s , o n e is a b l e t o a s s i g n a l e v e l o f n e s t i n g t o e a c h . I f t h e o u t e r m o s t p r o g r a m - d e f i n e d b l o c k ( e . g . t h e m a i n p r o g r a m ) i s c a l l e d l e v e l O, t h e n a b l o c k d e f i n e d w i t h i n t h i s b l o c k w o u l d b e o f l e v e l I; i n g e n e r a l , a b l o c k d e f i n e d in l e v e l i w o u l d b e o f l e v e l ( i + I ) . F i g u r e O . b i l l u s t r a t e s a b l o c k s t r u c t u r e ,

I

Q

F - - q

w h e r e l e v e l 0 : M l e v e l I = P, Q l e v e l 2 : A, R, S l e v e l 3 : B F i g u r e O . b B l o c k s t r u c t u r e

(12)

a n i d e n t i f i e r x is t h e e n t i r e b l o c k in w h i c h x is d e f i n e d , i n c l u d i n g t h o s e b l o c k s d e f i n e d in t h e s a m e b l o c k as x . (For t h i s e x a m p l e , n o t e t h a t a l l i d e n t i f i e r s m u s t be d i s t i n c t . S e c t i o n 3 . e d i s c u s s e s t h e c a s e w h e r e i d e n t i f i e r s a r e n o t n e c e s s a r i l y d i s t i n c t , ) o b j e c t s d e f i n e d in b l o c k a r e a c c e s s i b l e in b l o c k s M M ,P ,A .B ,Q .R ,S P P ,A ,B A A . B B B Q Q ,R ,S R R S S F o r p r o g r a m m e r s a c q u a i n t e d w i t h A L G O L , P L / I , o r F O R T R A N , i t may p r o v e h e l p f u l t o g l a n c e at P a s c a l in t e r m s o f t h e s e o t h e r l a n g u a g e s . F o r t h i s p u r p o s e , we l i s t t h e f o l l o w i n g c h a r a c t e r i s t i c s o f P a s c a l : 1. D e c l a r a t i o n o f v a r i a b l e s i s m a n d a t o r y , 2 . C e r t a i n k e y w o r d s ( e . g . ~ e a i n , ~ n d , ~ e o e a t ) e r e " r e s e r v e d " a n d c a n n o t b e u s e d a s i d e n t i f i e r s . I n t h i s m a n u a l t h e y a r e u n d e r l i n e d . 3. T h e s e m i c o l o n (;) is c o n s i d e r e d a s a s t a t e m e n t s e p a r a t o r , n o t a s t a t e m e n t t e r m i n a t o r (as e . g . in PL /I). 4 . T h e s t a n d a r d d a t a t y p e s a r e t h o s e o f w h o l e a n d r e a l n u m b e r s , t h e l o g i c a l v a l u e s , a n d t h e ( p r i n t a b l e ) c h a r a c t e r s . T h e b a s i c d a t a s t r u c t u r i n g f a c i l i t i e s i n c l u d e t h e a r r a y , t h e r e c o r d ( c o r r e s p o n d i n g t o C O B O L ' s a n d PL / I ' s " s t r u c t u r e " ) . t h e s e t , a n d t h e ( s e q u e n t i a l ) f i l e . T h e s e s t r u c t u r e s c a n b e c o m b i n e d e n d n e s t e d t o f o r m a r r a y s o f s e t s , f i l e s o f r e c o r d s , e t c . D a t a m a y b e a l l o c a t e d d y n a m i c a l l y a n d a c c e s s e d v i a p o i n t e r s . T h e s e p o i n t e r s a l l o w t h e f u l l g e n e r a l i t y o f l l s t p r o c e s s i n g . T h e m e is a f a c i l i t y t o d e c l a r e n e w , b a s i c d a t a t y p e s w i t h s y m b o l i c c o n s t a n t s . 5. T h e l e t d a t a s t r u c t u r e o f f e r s f a c i l i t i e s s i m i l a r t o t h e P L / I "bit s t r i n g " . 6. A r r a y s m a y b e o f a r b i t r a r y d i m e n s i o n w i t h a r b i t r a r y b o u n d s ; t h e a r r a y b o u n d s a r e c o n s t a n t . ( i . e . T h e r e a r e n o d y n a m i c a r r a y s . ) 7 . As i n F O R T R A N . A L G O L , a n d P L / I , t h e r e i s a g o t o s t a t e m e n t . L a b e l s a r e u n s i g n e d i n t e g e r s a n d m u s t b e d e c l a r e d . 8 . T h e c o m p o u n d s t a t e m e n t i s t h a t o f A L G O L , a n d c o r r e s p o n d s t o t h e DO g r o u p i n P L / I . 9 . T h e f a c i l i t i e s o f t h e ALGOL s w i t c h a n d t h e c o m p u t e d g o t o o f FORTRAN a r e r e p r e s e n t e d b y t h e c a s e s t a t e m e n t . 10. The f o r s t a t e m e n t , c o r r e s p o n d i n g t o t h e DO l o o p o f F O R T R A N . may o n l y h a v e s t e p s o f 1 ( ~ g ) o r - 1 ( ~ o w n t o ) a n d is e x e c u t e d o n l y a s l o n g a s t h e v a l u e o f t h e c o n t r o l v a r i a b l e l l e s w i t h i n t h e l i m i t s . C o n s e q u e n t l y . t h e c o n t r o l l e d s t a t e m e n t m a y n o t be e x e c u t e d at a l l .

(13)

12. P r o c e d u r e s e n d f u n c t i o n s m a y b e c a l l e d r e c u r s i v e l y . 13. T h e r e is no " o w n " a t t r i b u t e f o r v a r i a b l e s (as in A L G O L ) . t 4 . P a r a m e t e r s a r e c a l l e d e i t h e r b y v a l u e o r b y r e f e r e n c e : t h e r e i s n o c a l l b y n a m e . 1 5 . T h e " b l o c k s t r u c t u r e " d i f f e r s f r o m t h a t o f ALGOL a n d P L / I i n s o f a r a s t h e r e a r e n o a n o n y m o u s b l o c k s , i . e . e a c h b l o c k i s g i v e n a n a m e , a n d t h e r e b y i s m a d e i n t o a p r o c e d u r e . 1 6 . A l l o b j e c t s - - c o n s t a n t s , v a r i a b l e s , e t c . - - m u s t b e d e c l a r e d ~ e £ o ~ _ ~ t h e y a r e r e f e r e n c e d . T h e f o l l o w i n g t w o e x c e p t i o n s e r e h o w e v e r a l l o w e d : I) t h e t y p e i d e n t i f i e r in a p o i n t e r t y p e d e f i n i t i o n ( c h a p t e m 1 0 ) 2 ) p r o c e d u r e a n d f u n c t i o n c a l l s w h e n t h e r e i s a f o r w a r d r e f e r e n c e ( c h a p t e r 1 1 ) . U p o n f i r s t c o n t a c t w i t h P a s c a l , m a n y t e n d t o b e m o a n t h e a b s e n c e o f c e r t a i n " f a v o r i t e f e a t u r e s " , E x a m p l e s i n c l u d e an e x p o n e n t i a t i o n o p e r a t o r , c o n c a t e n a t i o n o f s t r i n g s , d y n a m i c a r r a y s , a r i t h m e t i c o p e r a t i o n s o n B o o l e a n v a l u e s , a u t o m a t i c t y p e c o n v e r s i o n s , a n d d e f a u l t d e c l a r a t i o n s . T h e s e w e r e n o t o v e r s i g h t s , b u t d e l i b e r a t e o m i s s i o n s . I n s o m e c a s e s t h e i r p r e s e n c e w o u l d be p r i m a r i l y a n i n v i t a t i o n t o i n e f f i c i e n t p r o g r a m m i n g s o l u t i o n s ; in o t h e r s , it w a s f e l t t h a t t h e y w o u l d be C o n t r a r y t o t h e a i m o f c l a r i t y a n d r e l i a b i l i t y a n d " g o o d p r o g r a m m i n g s t y l e " . F i n a l l y , a r i g o r o u s s e l e c t i o n a m o n g t h e i m m e n s e v a r i e t y o f p r o g r a m m i n g f a c i l i t i e s a v a i l a b l e h a d t o b e m a d e in o r d e r t o k e e p t h e c o m p i l e r r e l a t i v e l y c o m p a c t a n d e f f i c i e n t - - e f f i c i e n t a n d e c o n o m i c a l f o r b o t h t h e u s e r w h o w r i t e s o n l y s m a l l p r o g r a m s u s i n g f e w c o n s t r u c t s of t h e l a n g u a g e a n d t h e u s e r w h o w r i t e s l a r g e p r o g r a m s a n d t e n d s t o m a k e u s e o f t h e f u l l l a n g u a g e .

(14)

__N TAT~.~.~.!~T.J~ A N D V O C A B U L A R Y T h e b a s i c w o c a b u l a r v c o n s i s t s o f b a s i c s y m b o l s c l a s s i f i e d i n t o l e t t e r s , d i g i t s , a n d s p e c i a l s y m b o l s . T h e ~ . ~ ~ y m b o l s a r e o p e r a t o r s a n d d e l i m i t e r s : + : ( a n d a n d a i l - " ) D r r a v _ f . . i l ~ a o t ~ h e n / <> ] c a s e f u n c t i o n ~ <: } ~ i v i Z £ r o c e d u r e ~ a r • > : % ~ a i n ~r_Qar.am a h i l a , > .. d o w n t o / ~ l ~ ~ i t h 9 1 s e m o d K e o e a ~ ~ 9 ~ d - d e l i m ~ (or r e s e r v e d w o r d s ) e r e n o r m a l l y u n d e r l i n e d in t h e h a n d - w r l t t e n p r o g r a m t o e m p h a s i z e t h e i r i n t e r p r e t a t i o n as s i n g l e s y m b o l s w i t h f i x e d m e a n i n g . T h e o r o g r a m m e r m a y n o t u s e t h e s e w o r d s in a c o n t e x t o t h e r t h a n t h a t e x p l i c i t in t h e d e f i n i t i o n o f P a s c a l : in p a r t i c u l a r , t h e s e w o r d s m a y n o t b e u s e d as i d e n t i f i e r s . T h e y a r e w r i t t e n as a s e q u e n c e o f l e t t e r s ( w i t h o u t s u r r o u n d i n g e s c a p e c h a r a c t e r s ) . T h e c o n s t r u c t : { < a n y s e q u e n c e of s y m b o l s n o t c o n t a i n i n g "} ">} m a y b e i n s e r t e d b e t w e e n a n y t w o i d e n t i f i e r s , n u m b e r s , o r s p e c i a l s y m b o l s . It is c a l l e d a ~ o m m e n £ a n d m a y be r e m o v e d f r o m t h e p r o g r a m t e x t w i t h o u t a l t e r i n g i t s m e a n i n g . T h e s y m b o l s { a n d } do n o t o c c u r o t h e r w i s e in t h e l a n g u a g e , a n d w h e n a p p e a r i n g in s y n t a c t i c d e s c r i p t i o n s , t h e y d e n o t e m e t a - s y m b o l s l i k e I a n d ::=. (On s y s t e m s w h e r e t h e c u r l y b r a c k e t s a r e u n a v a i l a b l e , t h e c h a r a c t e r p a i r s (* a n d *) a r e u s e d in t h e i r p l a c e . ) ~ d e ~ i f ~ e r s a r e n a m e s d e n o t i n g c o n s t a n t s , t y p e s , v a r i a b l e s . P r o c e d u r e s . a n d f u n c t i o n s . T h e y m u s t b e g i n w i t h a l e t t e r , w h i c h m a y b e f o l l o w e d by a n y c o m b i n a t i o n a n d n u m b e r o f l e t t e r s a n d d i g i t s . A l t h o u g h a n i d e n t i f i e r m a y be v e r y l o n g . i m p l e m e n t a t i o n s m a y i m p o s e a l i m i t a s t o h o w m a n y o f t h e s e c h a r a c t e r s a r e s i g n i f i c a n t . I m p l e m e n t a t i o n s o f S t a n d a r d P a s c a l w i l l a l w a y s r e c o g n i s e t h e f i r s t ~ c h a r a c t e r s o f a n i d e n t i f i e r a s s i g n i f i c a n t . T h a t is. i d e n t i f i e r s d e n o t i n g d i s t i n c t o b j e c t s s h o u l d d i f f e r in t h e i r f i r s t U c h a r a c t e r s .

(15)

F i g u r e 1.a ~ l e t t e r ~ I d e n t i f i e r e x a m p l e s of legal i d e n t i f i e r s : s u m r o o t 3 pi h4g x t h i s i s a v e r y l o n g b u t n e v e r t h e l e s s l e g a l i d e n t i f i e r t h i s i s s v e r y l o n g b u t p r o b a b l y t h e s a m e i d e n t i f i e r a s a b o v e i l l e g a l i d e n t i f i e r s : 3rd a r r a y l e v e l . 4 r o o t - 3 C e r t a i n i d e n t i f i e r s , c a l l e d ~ e n d a r d i d e n t i f i e r s , ere p r e d e f i n e d

(e.g. s i n , c o s ) . In c o n t r a s t to the w o r d - d e l i m l t e r s (e.g.

Q r r a M ) , one is not r e s t r i c t e d to this d e f i n i t i o n and may elect

to r e d e f i n e any s t a n d a r d i d e n t i f i e r , as t h e y are a s s u m e d to be

d e c l a r e d in a h y p o t h e t i c a l b l o c k s u r r o u n d i n g the e n t i r e p r o g r a m

b l o c k .

D e c i m a l n o t a t i o n is used for ~ u m b e r s . The l e t t e r E p r e c e d i n g the

s c a l e f a c t o r is p r o n o u n c e d as "times 10 to the p o w e r of". The

s y n t a x of u n s i g n e d n u m b e r s is s u m m a r i z e d in f i g u r e 1.b.

Figure 1.b U n s i g n e d n u m b e r

Note t h a t if the number c o n t a i n s a d e c i m a l p o i n t , at least one

digit m u s t p r e c e d e and s u c c e e d the p o i n t . A l s o . no comma may

o c c u r in a n u m b e r .

u n s i g n e d n u m b e r s :

(16)

3 , 4 8 7 . 1 5 9 X I I .6 E l 0 5 . E - 1 6 B l a n k s . e n d s o f l i n e s , a n d c o m m e n t s e r e c o n s i d e r e d as s e m a r a ~ r s . A n a r b i t r a r y n u m b e r o f s e p a r a t o r s m a y o c c u r b e t w e e n a n y t w o c o n s e c u t i v e P a s c a l s y m b o l s w i t h t h e f o l l o w i n g e x c e p t i o n : no s e p a r a t o r s m a y o c c u r w i t h i n i d e n t i f i e r s , n u m b e r s , o r s p e c i a l s y m b o l s . H o w e v e r , at l e a s t o n e s e p a r a t o r m u s t o c c u r b e t w e e n a n y p a i r o f c o n s e c u t i v e i d e n t i f i e r s , n u m b e r s , o r w o r d s y m b o l s . S e q u e n c e s o f c h a r a c t e r s e n c l o s e d b y s i n g l e q u o t e m a r k s a r e c e l l e d ~ n o s . T o i n c l u d e a q u o t e m a r k in s s t r i n g , o n e w r i t e s t h e q u o t e m a r k t w i c e . e x a m p l e s o f s t r i n g s : "a' ";" "3 ° ' b e g l n " " d o n ' ° t " * t h i s s t r i n g h a s 3 3 c h a r a c t e r s "

(17)

D a t a is t h e g e n e r a l e x p r e s s i o n d e s c r i b i n g a l l t h a t is o p e r a t e d o n by t h e c o m p u t e r . At t h e h a r d w a r e a n d m a c h i n e c o d e l e v e l s , a l l d a t a a r e r e p r e s e n t e d a s s e q u e n c e s o f b i n a r y d i g i t s ( b i t s ) . H i g h e r l e v e l l a n g u a g e s a l l o w o n e t o u s e a b s t r a c t i o n s a n d t o i g n o r e t h e d e t a i l s o f r e p r e s e n t a t i o n - - l a r g e l y b y d e v e l o p i n g t h e C o n c e p t o f d a t a ~ v o e . A d a t a t y p e d e f i n e s t h e s e t o f v a l u e s a v a r i a b l e m a y a s s u m e . E v e r y v a r i a b l e o c c u r r i n g in a p r o g r a m m u s t b e a s s o c i a t e d w i t h O n e a n d o n l y o n e t y p e . A l t h o u g h d a t a t y p e s in P a s c a l c a n be q u i t e s o p h i s t i c a t e d , e a c h m u s t b e u l t i m a t e l y b u i l t f r o m u n s t r u c t u r e d t y p e s . A n u n s t r u c t u r e d t y p e is e i t h e r d e f i n e d b y t h e p r o g r a m m e r , a n d t h e n c a l l e d a d e c l a r e d s c a l a r t y p e . or o n e o f t h e f o u r s t a n d a r d s c a l a r t y p e s - - i n t e g e r , r e a l , B o o l e a n . o r c h a r . A s c a l a r t y p e is c h a r a c t e r i z e d by t h e s e t o f i t s d i s t i n c t v a l u e s , u p o n w h i c h a l i n e a r o r d e r i n g is d e f i n e d . T h e v a l u e s a r e d e n o t e d b y i d e n t i f i e r s in t h e d e f i n i t i o n o f t h e t y p e (see c h a p t e r 5 ) . A . T h e t y p e B o o l e a n A B o o l e a n v a l u e is o n e o f t h e l o g i c a l t r u t h v a l u e s d e n o t e d by t h e p r e d e f i n e d i d e n t i f i e r s f a l s e a n d t r u e . T h e f o l l o w i n g l o g i c a l o p e r a t o r s y i e l d e B o o l e a n v a l u e w h e n a p p l i e d t o B o o l e a n o p e r a n d s : ( A p p e n d i x B s u m m a r i z e s a l l O p e r a t o r s . ) and or no__t l o g i c a l c o n j u n c t i o n l o g i c a l d i s j u n c t i o n l o g i c a l n e g e t i o n E a c h o f t h e r e l a t i o n a l o p e r a t o r s ( = , < > . < = , < , > , > = , ~ ) y i e l d s a B o o l e a n v a l u e . F u r t h e r m o r e . t h e t y p e B o o l e a n i s d e f i n e d s u c h t h a t f a l s e < t r u e . H e n c e , i t i s p o s s i b l e t o d e f i n e e a c h o f t h e 16 B o o l e a n o p e r a t i o n s u s i n g t h e a b o v e l o g i c a l a n d r e l a t i o n a l o p e r a t o r s . F o r e x a m p l e , i f p a n d q a r e B o o l e a n v a l u e s , o n e c a n e x p r e s s i m p l i c a t i o n as p < = q e q u i v a l e n c e a s p = q e x c l u s i v e O R as p <> q

(18)

S t a n d a r d B o o l e a n f u n c t i o n s - - i . e , s t a n d a r d f u n c t i o n s w h i c h y i e l d a B o o l e a n r e s u l t - - e r e : ( A p p e n d i x A s u m m a r i z e s a l l s t a n d a r d f u n c t i o n s . )

odd(x)

e o l n (f) e o f ( f ) t r u e if t h e i n t e g e r x is o d d , f a l s e o t h e r w i s e e n d o f a l i n e , e x p l a i n e d in c h a p t e r 9 e n d o f f i l e . e x p l a i n e d in c h a p t e r 9 B . T h e

type

i n t e g e r A v a l u e of t y p e i n t e g e r is an e l e m e n t i m p l e m e n t a t i o n - d e f i n e d s u b s e t o f w h o l e n u m b e r s . o f t h e T h e f o l l o w i n g a r i t h m e t i c o p e r a t o r s y i e l d a n i n t e g e r v a l u e w h e n a p p l i e d t o i n t e g e r o p e r a n d s : * m u l t i p l y d i v i d e a n d t r u n c a t e ( i . e . v a l u e is n o t r o u n d e d ) rag_ d_ a mw3t b : a - ((a ~ b ) * b ) + a d d - s u b t r a c t T h e r e l a t i o n a l o p e r a t o r s =, <>. <, <=. >=. > y i e l d a B o o l e a n r e s u l t w h e n a p p l i e d to i n t e g e r o p e r s n d s . <> d e n o t e s i n e q u a l i t y . F o u r i m p o r t a n t s t a n d a r d f u n c t i o n s y i e l d i n g i n t e g e r r e s u l t s a r e : a b s (x) t h e r e s u l t is t h e a b s o l u t e v a l u e o f x . s q r ( x ) t h e r e s u l t is x s q u a r e d . t r u n c ( x ) x is a r e a l v a l u e : t h e r e s u l t is i t s w h o l e p a r t . ( T h e f r a c t i o n a l p a r t is d i s c a r d e d . H e n c e t r u n c ( J . 7 ) = 3 a n d t r u n c ( - J . 7 ) = - 3 ) r o u n d ( x ) x is a r e a l v a l u e ; t h e r e s u l t is t h e r o u n d e d i n t e g e r , r o u n d ( x ) m e a n s f o r x > = 0 t r u n c ( x + 0 . 5 ) , a n d f o r x < 0 t r u n c ( x - 0 . 5 ) N o t e s : a b s a n d s q r y i e l d an i n t e g e r r e s u l t o n l y w h e n t h e i r a r g u m e n t is a l s o o f t y p e i n t e g e r . I f i is a v a r i a b l e of t y p e i n t e g e r , t h e n s u c c ( 1 ) y i e l d s t h e " n e x t " i n t e g e r , a n d p r e d ( i ) y i e l d s t h e p r e c e d i n g i n t e g e r T h i s i s , h o w e v e r , m o r e c l e a r l y e x p r e s s e d b y t h e e x p r e s s i o n s i + 1 a n d i-I T h e r e e x i s t s an i m p l e m e n t a t i o n - d e p e n d e n t s t a n d a r d i d e n t i f i e r m a x i n t , if a a n d b a r e i n t e g e r e x p r e s s i o n s , t h e o p e r a t i o n : a go b is g u a r a n t e e d t o be c o r r e c t l y i m p l e m e n t e d w h e n :

(19)

a b s (a o@. b ) < : m a x i n t , a b s ( a ) < : m a x i n t , a n d a b s ( b ) < : m a x i n t C . T h e t y p e r e a l A v a l u e o f t y p e r e a l is an e l e m e n t o f t h e i m p l e m e n t a t i o n - d e f i n e d s u b s e t o f r e a l n u m b e r s . A s l o n g a s at l e a s t o n e o f t h e o p e r a n d s is o f t y p e r e a l ( t h e o t h e r p o s s i b l y b e i n g o f t y p e i n t e g e r ) t h e f o l l o w i n g o p e r a t o r s y i e l d a r e a l v a l u e : * m u l t i p l y / d i v i d e ( b o t h o p e r a n d s m a y b e i n t e g e r s , b u t t h e r e s u l t is a l w a y s r e a l ) + a d d - s u b t r c c t S t a n d a r d r e s u l t :

abs ( x )

sqr ( x )

f u n c t i o n s w h e n a c c e p t i n g a r e e l a r g u m e n t y i e l d a r e a l a b s o l u t e v a l u e x s q u a r e d S t a n d a r d f u n c t i o n s w i t h r e a l o r i n t e g e r r e s u l t : s i n ( x ) c o s

( x )

a m c t a n ( x ) l n ( x ) e x p

(x)

s q r t ( x ) t r i g o n o m e t r i c f u n c t i o n s n a t u r a l l o g a r i t h m e x p o n e n t i a l f u n c t i o n s q u a r e r o o t a r g u m e n t a n d m e a l W a r n i n q : a l t h o u g h r e a l is i n c l u d e d a s a s c a l a r t y p e , it c a n n o t a l w a y s b e u s e d i n t h e s a m e c o n t e x t a s t h e o t h e r s c a l a r t y p e s . I n P a r t i c u l a r . t h e f u n c t i o n s p r e d a n d s u c c c a n n o t t a k e r e a l a r g u m e n t s , a n d v a l u e s o f t y p e m e a l c a n n o t b e u s e d w h e n i n d e x i n g a r r a y s , n o r i n c o n t r o l l i n g f o r s t a t e m e n t s , n o r f o r d e f i n i n g t h e b a s e t y p e o f a s e t . D . T h e t y p e c h a r A v a l u e o f t y p e c h a r is a n e l e m e n t o f a f i n i t e a n d o r d e r e d s e t o f c h a r a c t e r s . E v e r y c o m p u t e r s y s t e m d e f i n e s s u c h a s e t f o r t h e p u r p o s e o f c o m m u n i c a t i o n . T h e s e c h a r a c t e r s a r e t h e n a v a i l a b l e o n t h e i n p u t a n d o u t p u t e q u i p m e n t . U n f o r t u n a t e l y . t h e r e d o e s n o t

(20)

e x i s t o n e s t a n d a r d c h a r a c t e r s e t ; t h e r e f o r e , t h e d e f i n i t i o n o f t h e e l e m e n t s a n d t h e i r o r d e r i n g is s t r i c t l y i m p l e m e n t a t i o n d e p e n d e n t . T h e f o l l o w i n g m i n i m a l a s s u m p t i o n s h o l d f o r t h e t y p e c h a r , i n d e p e n d e n t o f t h e u n d e r l y i n g i m p e m e n t a t i o n : T h e c h a r a c t e r s e t i n c l u d e s 1 . t h e a l p h a b e t i c a l l y o r d e r e d s e t o f c a p i t a l L a t i n l e t t e r s A . . . Z 2 . t h e n u m e r i c a l l y o r d e r e d a n d c o n t i g u o u s s e t o f t h e d e c i m a l d i g i t s 0 . . . 9 3. t h e b l a n k c h a r a c t e r . A c h a r a c t e r e n c l o s e d in a p o s t r o p h e s ( s i n g l e q u o t e s ) d e n o t e s a c o n s t a n t o f t h i s t y p e . e x a m p l e s : ' ~ " ' G " ' 3 ° " ° ° ' °X ° ( T o r e p r e s e n t a n a p o s t r o p h e , o n e w r i t e s i t t w i c e . ) T h e t w o s t a n d a r d f u n c t i o n s ~r.~[ a n d ~ h r a l l o w t h e m a p p i n g o f t h e g i v e n c h a r a c t e r s e t o n t o a s u b s e t o f n a t u r a l n u m b e r s - - c a l l e d t h e o r d i n a l n u m b e r s o f t h e c h a r a c t e r s e t - - a n d v i c e v e r s e ; o r d e n d c h r e r e c a l l e d ~ r a n s f e r ~ u n c t i o n s . o r d ( c ) is t h e o r d i n a l n u m b e r o f t h e c h a r a c t e r c in t h e u n d e r l y i n g o r d e r e d c h a r a c t e r s e t . ( a l s o s e e s e c t i o n 5 . A ) c h r ( i ) i s t h e c h a r a c t e r v a l u e w i t h t h e o r d i n a l n u m b e r i . O n e s e e s i m m e d i a t e l y t h a t o r d a n d c h r a r e i n v e r s e f u n c t i o n s . i . e . c h r ( o r d ( c ) ) = c - a n d - o r d ( c h r ( i ) ) = i F u r t h e r m o r e , t h e o r d e r i n g o f a g i v e n c h a r a c t e r s e t i s d e f i n e d b y ci < e 2 i f f o r d ( c l ) < o r d ( c 2 ) T h i s d e f i n i t i o n c a n be e x t e n d e d t o e a c h o f t h e r e l a t i o n a l o p e r a t o r s : =. <>. <, <=, > = . >. I f R d e n o t e s o n e o f t h e s e o p e r a t o r s , t h e n ci R e 2 i f f o r d ( c l ) R o r d ( c 2 ) W h e n t h e a r g u m e n t o f t h e s t a n d a r d f u n c t i o n s p r e d a n d s u c c is o f t y p e c h a r , t h e f u n c t i o n s c a n b e d e f i n e d a s : p r e d ( c ) = c h r (ord ( c ) - I ) s u c c ( c ) = c h r (ord ( c ) + I ) N o t e : T h e p r e d e c e s s o r ( s u c c e s s o r ) o f a c h a r a c t e r is d e p e n d e n t u p o n t h e u n d e r l y i n g c h a r a c t e r s e t a n d is u n d e f i n e d if o n e d o e s n o t e x i s t .

(21)

E v e r y p r o g r a m c o n s i s t s o f a h e a d i n g a n d a b l o c k . T h e b l o c k c o n t a i n s a d e c l a r a t i o n p a r t , i n w h i c h a l l o b j e c t s l o c a l t o t h e p r o g r a m a r e d e f i n e d , a n d a s t a t e m e n t p a r t . w h i c h s p e c i f i e s t h e a c t i o n s t o b e e x e c u t e d u p o n t h e s e o b j e c t s . < p r o g r a m > ::= < p r o g r a m h e a d i n g > < b l o c k > < b l o c k > : : : < l a b e l d e c l a r a t i o n p a r t > < c o n s t a n t d e f i n i t i o n p a r t > < t y p e d e f i n i t i o n p a r t > < v a r i a b l e d e c l a r a t i o n p a r t > < p r o c e d u r e a n d f u n c t i o n d e c l a r a t i o n p a r t > < s t a t e m e n t p a r t > A . P r o g r a m h e a d i n g T h e h e a d i n g g i v e s t h e p r o g r a m a n a m e ( n o t o t h e r w i s e s i g n i f i c a n t i n s i d e t h e p r o g r a m ) a n d l i s t s i t s p a r a m e t e r s , t h r o u g h w h i c h t h e p r o g r a m c o m m u n i c a t e s w i t h t h e e n v i r o n m e n t ( s e e c h a p t e r 1 3 . B . I ) . < p r o g r a m h e a d i n g > ::= & ~ l i g ~ i m < i d e n t i f i e r > ( < f i l e i d e n t i f i e r > { , < f i l e i d e n t i f i e r > } ) ; B . L a b e l d e c l a r a t i o n p a r t A n y s t a t e m e n t in a p r o g r a m m a y b e m a r k e d b y p r e f i x i n g t h e s t a t e m e n t w i t h a l a b e l f o l l o w e d b y a c o l o n ( m a k i n g p o s s i b l e a r e f e r e n c e b y a g o t o s t a t e m e n t ) . H o w e v e r , t h e l a b e l m u s t be d e f i n e d i n t h e j a b e l ~ c l a r ~ & i ~ A & ~ i ~ b e f o r e i t s u s e . T h e s y m b o l l a b e l h e a d s t h i s p a r t , w h i c h h a s t h e g e n e r a l f o r m : l a b e l < l a b e l > {, < l a b e l > } ; A l a b e l is d e f i n e d t o b e a n u n s i g n e d i n t e g e r , e n d c o n s i s t s o f a t m o s t 4 d i g i t s . e x a m p l e : i ~ e l ~ , 1 ~ ; C . C o n s t a n t d e f i n i t i o n p a r t A ~ & t a o ~ ~ e f i n i t i o n i n t r o d u c e s a n i d e n t i f i e r a s a s y n o n y m f o r a c o n s t a n t . T h e s y m b o l ~ i n t r o d u c e s t h e c o n s t a n t d e f i n i t i o n p a r t , w h i c h h a s t h e g e n e r a l f o r m : f, J i ~ & ~ < i d e n t i f i e r > = < c o n s t a n t > ; { < i d e n t i f i e r > = < c o n s t a n t > ; }

(22)

w h e r e a c o n s t a n t is e i t h e r " a n u m b e r , a c o n s t a n t i d e n t i f i e r ( p o s s i b l y s i g n e d ) , or a s t r i n g . T h e u s e o f c o n s t a n t i d e n t i f i e r s g e n e r a l l y m a k e s a p r o g r a m m o r e r e a d a b l e a n d a c t s as a c o n v e n i e n t d o c u m e n t a t i o n a i d . It a l s o a l l o w s t h e p r o g r a m m e r t o g r o u p m a c h i n e o r e x a m p l e d e p e n d e n t q u a n t i t i e s at t h e b e g i n n i n g o f t h e p r o g r a m w h e r e t h e y c a n be e a s i l y n o t e d a n d / o r c h a n g e d . ( T h e r e b y a i d i n g t h e p o r t a b i l i t y a n d m o d u l a r i t y of t h e p r o g r a m . ) A s a n e x a m p l e , c o n s i d e r t h e f o l l o w i n g p r o g r a m : { p r o g r a m 3 . 1 e x a m p l e o f c o n s t a n t d e f i n i t i o n p e r t } ~ r o a r a m c o n v e r t ( o u t p u t ) ; # o n s t a d d i n = 3 2 ; m u l b y = 1 . 8 ; l o w = O; h i g h = 3 9 ; s e s a r a t o r = * °; v a r d e g r e e : l o w . . h i g h ; J~eain w r i t e l n ( s e p a r a t o r ) : f o r d e g r e e := l o w J&o h i g h d o b e Q i ~ w r i t e ( d e g r e e ° ' c ' , r o u n d ( d e g r e e * m u l b y + a d d i n ) , ' f ' ) " i f o d d ( d e g r e e ) t h e n w r i t e l n e n d : w r i t e l n ; w r i t e l n ( s e p a r a t o r ) a n d . 0c 32f Ic 3 4 f 2c 36f 3c 3 7 f 4c 39f 5c 4 1 f 6c 4 3 f 7c 4 5 f 8c 4 6 f 9c 4 8 f I0c 50f 11c 52f 12c 54f 13c 5 5 f 14c 57f 15c 5 9 f 16c 6 1 f 17e 6 3 f IBC 6 4 f 19c 6 6 f 2 0 C 68f 2 1 c 7 0 f 22c 72f 2 3 c 7 3 f 2 4 c 75f 2 5 c 7 7 f 26c 7 9 f 2 7 c 8 1 f 2 8 c 8 2 f 2 9 c 8 4 f 3 0 c 86f 3 1 c 8 8 f 32c 90f 33c 9 1 f 34c 93f 35c 9 5 f 36c 97f 37c 9 9 f 3 8 c 100f 3 9 c I02f

(23)

D . T y p e d e f i n i t i o n p a r t A d a t a t y p e i n P a s c a l m a y b e e i t h e r d i r e c t l y d e s c r i b e d in t h e v a r i a b l e d e c l a r a t i o n o r r e f e r e n c e d b y a ~ y p Q ~ d e n t i f i e r . P r o v i d e d a r e n o t o n l y s e v e r a l s t a n d a r d t y p e i d e n t i f i e r s , b u t a l s o e m e c h a n i s m , t h e ~Z#,~ ~ e f i n i t i o o , f o r c r e a t i n g n e w t y p e s . The s y m b o l ~ . @ i n t r o d u c e s a p r o g r a m p a r t c o n t a i n i n g t y p e d e f i n i t i o n s , The d e f i n i t i o n i t s e l f d e t e r m i n e s a s e t o f v a l u e s a n d a s s o c i a t e s a n i d e n t i f i e r w i t h t h e s e t . The g e n e r a l f o r m i s : t ~ o e < i d e n t i f i e r > = < t y p e > : [ < i d e n t i f i e r > = < t y p e > : } E x a m p l e s o f t y p e d e f i n i t i o n s e r e f o u n d in t h e s u b s e q u e n t c h a p t e r s . E . V a r i a b l e d e c l a r a t i o n p a r t E v e r y v a r i a b l e o c c u r r i n g ~ a r ! a b ! e ~ ~ . T h i s a n y u s e o f t h e v a r i a b l e . in a s t a t e m e n t m u s t b e d e c l a r e d in a d e c l a r a t i o n m u s t t e x t u a l l y P r e c e d e A v a r i a b l e d e c l a r a t i o n a s s o c i a t e s e n i d e n t i f i e r a n d a d a t a t y p e w i t h a n e w v a r i a b l e b y s i m p l y l i s t i n g t h e i d e n t i f i e r f o l l o w e d b y i t s t y p e . T h e s y m b o l ~ & ~ h e a d s t h e v a r i a b l e d e c l a r a t i o n p a r t . T h e g e n e r a l f o r m i s : < i d e n t i f i e r > {, < i d e n t i f i e r > } : < t y p e > : { < i d e n t i f i e r > { , < i d e n t i f i e r > } : < t y p e > i } e x a m p l e : v ~ r o o t l , r o o t 2 . r o o t 3 : r e a l i c o u n t , i : i n t e g e r ; f o u n d : B o o l e a n ; f i l l e r : c h a r i T h i s i d e n t i f i e r / t y p e a s s o c i a t i o n is v a l i d t h r o u g h o u t t h e e n t i r e b l o c k c o n t a i n i n g t h e d e c l a r a t i o n , u n l e s s t h e i d e n t i f i e r is r e d e f i n e d i n a s u b o r d i n a t e b l o c k . S u p p o s e a b l o c k B is n e s t e d w i t h i n b l o c k A . ( i . e . d e c l a r e d w i t h i n t h e s c o p e o f a n d h e n c e s u b o r d i n a t e t o A . a s in f i g u r e 0 . b ) It is p o s s i b l e t o d e c l a r e a n i d e n t i f i e r i n B t h a t i s a l r e a d y d e c l a r e d i n A . T h i s h a s t h e e f f e c t o f a s s o c i a t i n g t h a t i d e n t i f i e r w i t h a v a r i a b l e l o c a l t o B - - n o t a v a i l a b l e t o A - - w h i c h m a y b e o f a n y t y p e . T h e l a t t e r d e f i n i t i o n is t h e n v a l i d t h r o u g h o u t t h e s c o p e o f B o u n l e s s r e d e c l a r e d i n a b l o c k s u b o r d i n a t e t o B . It is n e t a l l o w e d t o d e c l a r e a s i n g l e i d e n t i f i e r m o r e t h a n o n c e w i t h i n t h e s a m e l e v e l a n d s c o p e . H e n c e t h e f o l l o w i n g is a l w a y s i n c o r r e c t . ~ J ~ a : i n t e g e r ; a : r e a l ;

(24)

F . P r o c e d u r e a n d f u n c t i o n d e c l a r a t i o n p a r t E v e r y p r o c e d u r e o r f u n c t i o n m u s t b e d e f i n e d (or a n n o u n c e d ) b e f o r e i t s u s e . P r o c e d u r e e n d f u n c t i o n d e c l a r a t i o n s a r e t r e a t e d in c h a p t e r 11. P r o c e d u r e s a r e s u b r o u t i n e s e n d a r e a c t i v a t e d b y p r o c e d u r e s t a t e m e n t s . F u n c t i o n s a r e s u b r o u t i n e s t h a t y i e l d a r e s u l t v a l u e , a n d t h e r e f o r e c a n b e u s e d a s c o n s t i t u e n t s o f e x p r e s s i o n s .

(25)

T H E ~ Q N C E P T OE A C T I O N E s s e n t i a l t o a c o m p u t e r p r o g r a m i s a c t i o n . T h a t i s , a p r o g r a m m u s t d o s o m e t h i n g w i t h i t s d a t a - - e v e n i f t h a t a c t i o n i s t h e c h o i c e o f d o i n g n o t h i n g ! ~ ~ d e s c r i b e t h e s e a c t i o n s . S t a t e m e n t s a r e e i t h e r & i m o l e ( e . g . t h e a s s i g n e m e n t s t a t e m e n t ) o r s t r u c t u r e d . A . T h e a s s i g n m e n t s t a t e m e n t T h e m o s t f u n d a m e n t a l of s t a t e m e n t s is t h e ~ s s i ~ d 3 ~ w t a t e m e n t . I t s p e c i f i e s t h a t a n e w l y c o m p u t e d v a l u e b e a s s i g n e d t o a V a r i a b l e . T h e f o r m o f a n a s s i g n m e n t i s : < v a r i a b l e > := < e x p r e s s i o n > w h e r e := is t h e & s s i c n m e n t ~ p e r a t o r . not t o b e c o n f u s e d w i t h t h e r e l a t i o n a l o p e r a t o r = . T h e s t a t e m e n t " a : = 5 " i s p r o n o u n c e d " t h e c u r r e n t v a l u e o f a i s r e p l a c e d w i t h t h e v a l u e 5 " , o r s i m p l y , " a ~ e c o m e s 5 " . T h e n e w v a l u e is o b t a i n e d b y e v a l u a t i n g an ~ x m r e s s i o n c o n s i s t i n g o f c o n s t a n t o r v a r i a b l e o p e r a n d s , o p e r a t o r s , a n d f u n c t i o n d e s i g n a t o r s , (A f u n c t i o n d e s i g n a t o r s p e c i f i e s t h e a c t i v a t i o n o f a f u n c t i o n . S t a n d a r d f u n c t i o n s a r e l i s t e d i n A p p e n d i x A ; u s e r d e f i n e d f u n c t i o n s a r e e x p l a i n e d i n c h a p t e r 1 1 . ) An e x p r e s s i o n i s e r u l e f o r c a l c u l a t i n g e v a l u e w h e r e t h e c o n v e n t i o n a l r u l e s o f l e f t t o r i g h t e v a l u a t i o n a n d ~ E e r a t o r e r e c e d e n c e a r e o b s e r v e d . T h e o p e r a t o r ~ o t ( a p p l i e d t o a B o o l e a n o p e r a n d ) h a s t h e h i g h e s t p r e c e d e n c e , f o l l o w e d b y t h e m u l t i p l y i n g o p e r a t o r s ( * . / , ~ i v , ~_#J~, ~ U ~ ) , t h e n t h e a d d i n g o p e r a t o r s ( + , - , ~ ) , a n d o f l o w e s t p r e c e d e n c e , t h e r e l a t i o n a l o p e r a t o r s ( = , < > , < , < = . > = , > . & ~ ) . A n y e x p r e s s i o n e n c l o s e d w i t h i n p a r e n t h e s e s i s e v a l u a t e d i n d e p e n d e n t o f p r e c e d i n g o r s u c c e e d i n g o p e r a t o r s . e x a m p les : 2 * 3 - 4 * 5 = ( 2 * 3 ) - ( 4 * 5 ) = - 1 4 15 ~ 4 * 4 = ( 1 5 ~ i v 4 ) * 4 = 12 B 0 / 5 / 3 = ( 8 0 / 5 ) / 3 = 5 . 3 3 3 4 / 2 * 3 = ( 4 / 2 ) * 3 = 6 . 0 0 0 s q r t ( s q r ( 3 ) + 1 1 * 5 ) = 8 . 0 0 0 T h e s y n t a x o f A p p e n d i x D r e f l e c t s t h e e x a c t r u l e s o f p r e c e d e n c e . T h e r e a d e r i s r e c o m m e n d e d t o r e f e r e n c e i t w h e n e v e r i n d o u b t . B o o l e a n e x p r e s s i o n s h a v e t h e p r o p e r t y t h s t t h e i r v a l u e may b e k n o w n b e f o r e t h e e n t i r e e x p r e s s i o n h a s b e e n e v a l u a t e d . A s s u m e f o r e x a m p l e , t h a t x = O . T h e n ( x > O ) ~ ( x < l O ) is a l r e a d y k n o w n to be f a l s e a f t e r c o m p u t a t i o n o f t h e f i r s t

(26)

f a c t o r , a n d t h e s e c o n d n e e d n o t b e e v a l u a t e d . T h e r u l e s o f P a s c a l n e i t h e r r e q u i r e n o r f o r b i d t h e e v a l u a t i o n o f t h e s e c o n d p a r t i n s u c h c a s e s . T h i s m e a n s t h a t t h e p r o g r a m m e r m u s t a s s u r e t h a t t h e s e c o n d f a c t o r is w e l l - d e f i n e d , i n d e p e n d e n t o f t h e v a l u e o f t h e f i r s t f a c t o r . H e n c e . i f o n e a s s u m e s t h a t t h e a r r a y a h a s a n i n d e x r a n g i n g f r o m I t o 10. t h e n t h e f o l l o w i n g e x a m p l e is i n e r r o r ~ x : = O: ~ e o e a t x : = x + 1 ~ n t i l ( x > l O ) ~ ( a [ x ] = O ) ( N o t e t h a t i f n o a [ i ] = O, t h e p r o g r a m w i l l r e f e r t o a n e l e m e n t

a[11] .)

D i r e c t a s s i g n m e n t is p o s s i b l e t o v a r i a b l e s o f a n y t y p e , e x c e p t f i l e s . H o w e v e r . t h e v a r i a b l e (or t h e f u n c t i o n ) a n d t h e e x p r e s s i o n m u s t b e o f i d e n t i c a l t y p e . w i t h t h e e x c e p t i o n t h a t i f t h e t y p e o f t h e v a r i a b l e i s r e a l , t h e t y p e o f t h e e x p r e s s i o n m a y b e i n t e g e r . ( I f a s u b r a n g e t y p e is i n v o l v e d , i t s a s s o c i a t e d s c a l a r t y p e d e t e r m i n e s t h e v a l i d i t y o f t h e a s s i g n m e n t ; s e e s e c t i o n 5 . B .) e x a m p l e s o f a s s i g n m e n t s : r o o t I := p i * x / y r o o t 1 := - r o o t I r o o t 3 := ( r o o t l + r o o t 2 ) * ( 1 . 0 + y ) f o u n d := y > z c o u n t : = c o u n t + I d e g r e e := d e g r e e + 10 s q r p r := s q r ( p r ) y := s i n ( x ) + c o s ( y ) B . T h e c o m p o u n d s t a t e m e n t T h e ~ p m p o u n d ~ t a t e m e n t s p e c i f i e s t h a t i t s c o m p o n e n t s t a t e m e n t s b e e x e c u t e d i n t h e s a m e s e q u e n c e a s t h e y a r e w r i t t e n . T h e s y m b o l s ~ e g i n a n d ~ n d a c t a s s t a t e m e n t b r a c k e t s . N o t e t h a t t h e " b o d y " o f a P r o g r a m h a s t h e f o r m o f a c o m p o u n d s t a t e m e n t . { p r o g r a m 4 . 1 t h e c o m p o u n d s t a t e m e n t } ~ r o ~ r a m b e g i n e n d ( o u t p u t ) ; ~ j ~ s u m : i n t e g e r ; ~ e ~ i n s u m := 3 + 5 ; w r i t e l n ( s u m , - s u m )

e_o~.

B - 8

(27)

P a s c a l u s e s t h e s e m i c o l o n t o ~ e o a r e t e s t a t e m e n t s , n o t t o t e r m i n a t e s t a t e m e n t s ; i . e . t h e s e m i c o l o n is N O T p a r t o f t h e s t a t e m e n t . T h e e x p l i c i t r u l e s r e g a r d i n g s e m i c o l o n s a r e r e f l e c t e d in t h e s y n t a x o f A p p e n d i x D . If o n e h a d w r i t t e n a s e m i c o l o n a f t e r t h e s e c o n d s t a t e m e n t , t h e n a n ~ m o t y ~ t a t e m e n t ( i m p l y i n g no a c t i o n ) w o u l d h a v e b e e n a s s u m e d b e t w e e n t h e s e m i c o l o n a n d t h e s y m b o l ~ n d . T h i s d o e s no h a r m , f o r a n e m p t y s t a t e m e n t is a l l o w a b l e a t t h i s p o i n t . M i s p l a c e d s e m i c o l o n s c a n . h o w e v e r , c a u s e t r o u b l e s - - n o t e t h e e x a m p l e in s e c t i o n 4 . D . C. R e p e t i t i v e s t a t e m e n t s R e o e t l t i v e ~ t a t m m e n t s s p e c i f y t h a t c e r t a i n s t a t e m e n t s be r e p e a t e d l y e x e c u t e d . I f t h e n u m b e r o f r e p e t i t i o n s is k n o w n b e f o r e h a n d ( b e f o r e t h e r e p e t i t i o n s a r e b e g u n ) , t h e f o r s t a t e m e n t is u s u a l l y t h e a p p r o p r i a t e c o n s t r u c t t o e x p r e s s t h e s i t u a t i o n ; o t h e r w i s e , t h e r e p e a t o r w h i l e s t a t e m e n t s h o u l d b e u s e d . C .1 T h e w h i l e s t a t e m e n t T h e w h i l e s t a t e m e n t has t h e f o r m : w h i l e < e x p r e s s i o n > ~ g < s t a t e m e n t > T h e e x p r e s s i o n c o n t r o l l i n g t h e r e p e t i t i o n m u s t be of t y p e B o o l e a n . It is e v a l u a t e d b e f o r e e a c h i t e r a t i o n , so c a r e m u s t be t a k e n t o k e e p t h e e x p r e s s i o n a s s i m p l e e s p o s s i b l e . { p r o g r a m 4 . 2

c o m p u t e h ( n ) = I + 1/2 + I/3 + o.. + I/n }

Q r o e r a m e g w h i l e ( i n p u t , o u t p u t ) : v a r n : i n t e g e r ; h : r e a l ; ~ e a l n r e a d ( n ) ; w r i t e ( n ) ; h :: O; w h i l e n > O ~ ~ p . ~ h := h + I/n; n := n - 1 n n d ; w r i t e l n ( h ) e n d . 10 2 . 9 2 8 9 6 8 2 5 3 9 6 8 e + 0 0 T h e s t a t e m e n t e x e c u t e d by t h e w h i l e s t a t e m e n t (a c o m p o u n d s t a t e m e n t i n t h e a b o v e c a s e ) is r e p e a t e d u n t i l t h e e x p r e s s i o n b e c o m e s f a l s e . I f i t s v a l u e is f a l s e at t h e b e g i n n i n g ° t h e

(28)

s t a t e m e n t is n o t e x e c u t e d a t a l l . C . 2 T h e r e p e a t s t a t e m e n t T h e r e p e a t s t a t e m e n t h a s t h e f o r m : r e o e a t < s t a t e m e n t > { ; < s t a t e m e n t > } u n t i l < e x p r e s s i o n > T h e s e q u e n c e o f s t a t e m e n t s b e t w e e n t h e s y m b o l s ~ e a t a n d u n t i l is e x e c u t e d a t l e a s t o n c e . R e p e a t e d e x e c u t i o n i s c o n t r o l l e d b y t h e B o o l e a n e x p r e s s i o n , w h i c h is e v a l u a t e d a f t e r e v e r y i t e r a t i o n . { p r o g r a m 4 . 3 c o m p u t e h ( n ) = I + I/2 + I / 3 + ... + I/n } o r o o r a m e g r e p e a t ( i n p u t , o u t p u t ) ; v a t n : i n t e g e r ; h : r e a l ; b e o i n r e a d ( n ) ; w r i t e ( n ) ; h := O; r e p e a t h := h + I/n; n := n - 1 n=O; w r i t e l n (h) am~. 10 2 . 9 2 8 9 6 8 2 5 3 9 6 8 e + 0 0 T h e a b o v e p r o g r a m p e r f o r m s c o r r e c t l y f o r n > O , C o n s i d e r w h a t h a p p e n s i f n < = O . T h e w h i l e - v e r s l o n o f t h e s a m e p r o g r a m is c o r r e c t f o r a l l n . i n c l u d i n g n = O . N o t e t h a t i t is a s e q u e n c e o f s t a t e m e n t s t h a t t h e r e p e a t s t a t e m e n t e x e c u t e s ; a b r a c k e t i n g p a i r ~ e ~ ! ~ . . . ~ n d w o u l d b e r e d u n d a n t ( b u t n o t i n c o r r e c t ) . C . 3 T h e f o r s t a t e m e n t T h e f o r s t a t e m e n t i n d i c a t e s t h a t a s t a t e m e n t b e r e p e a t e d l y e x e c u t e d w h i l e a p r o g r e s s i o n o f v a l u e s is a s s i g n e d t o t h e & p n ~ z a r i a b l e o f t h e f o r s t a t e m e n t . I t h a s t h e g e n e r a l f o r m : f o r < c o n t r o l v a r i a b l e > := < i n i t i a l v a l u e > t o < f i n a l v a l u e > d o < s t a t e m e n t >

( o r )

< c o n t r o l v a r i a b l e > := < i n i t i a l v a l u e > d o w n t o < f i n a l v a l u e > d o < s t a t e m e n t >

(29)

{ p r o g r a m 4 . 4 c o m p u t e h ( n ) = 1 + 1 / 2 + 1 / 3 + . . . + 1 / n }

o r o o r ~ eGfor(input, output);

y a m i , n : i n t e g e r ; h : r e a l ; b e a i n r e a d (n) ; w r i t e (n) ; h := O; i := n d Q ~ n t o I d ~ h := h + 1 / i ; w r i t e l n ( h ) e n d . 10 2 . 9 2 8 9 6 8 2 5 3 9 6 8 e + 0 0 { p r o g r a m 4 . 5 c o m p u t e t h e c o s i n e u s i n g t h e e x p a n s i o n : c o s ( x ) = 1 - x * ' 2 / ( 2 " I ) + x * ' 4 / ( 4 " 3 " 2 " I ) - ... } ~ r Q o r a m c o s i n e ( i n p u t , o u t p u t ) ; c o n s t e p s = I e - 1 4 ; v a t x , s x , s , t : r e a l ; i .k ,n : i n t e g e r ; b # a i n r e a d ( n ) ; f o r i := I t o n ~L~ b e o i n r e a d ( x ) ; t := I; k :-- O; s := I; s x :~ s q r ( x ) ; ~ D J ~ a b s ( t ) > e p s * a b s ( s ) d o ~ e ~ D k :~ k + 2; t : = - t * s x / ( k * ( k - 1 ) ) ; S : = s + t e n d ; w r i t e l n (x ,s ,k d i v 2) e n d e n d . 1 . 5 3 4 6 2 2 2 2 2 2 3 3 e - 0 1 9 . 8 8 2 4 7 7 6 4 7 6 1 4 e - 0 1 3 . 3 3 3 3 3 3 3 3 3 3 3 3 e - 0 1 9 . 4 4 9 5 6 9 4 6 3 1 4 7 e - 0 1 5 . 0 0 0 0 0 0 0 0 0 0 0 0 e - 0 1 8 . V 7 5 8 2 5 6 1 8 9 0 4 e - 0 1 1 . 0 0 0 0 0 0 0 0 0 0 0 0 e + O 0 5 . 4 0 3 0 2 3 0 5 8 6 8 1 e - 0 1 3 . 1 4 1 5 9 2 6 5 3 5 9 0 e + 0 0 - 1 . 0 0 0 0 0 0 0 0 0 0 0 0 e + O 0 5 6 ? 9 14 T h e c o n t r o l v a r i a b l e , t h e i n i t i a l v a l u e , a n d t h e f i n a l v a l u e m u s t b e o f t h e s a m e s c a l a r t y p e ( e x c l u d i n g t y p e r e a l ) , a n d m u s t n o t b e a l t e r e d b y t h e f o r s t a t e m e n t . T h e i n i t i a l a n d f i n a l v a l u e s a r e e v a l u a t e d o n l y o n c e . I f in t h e c a s e o f ~ ( ~ n w ~ t o ) t h e i n i t i a l v a l u e is g r e a t e r ( l e s s ) t h a n t h e f i n a l v a l u e , t h e f o r s t a t e m e n t is n o t e x e c u t e d . T h e f i n a l v a l u e o f t h e c o n t r o l v a r i a b l e i s l e f t u n d e f i n e d u p o n n o r m a l e x i t f r o m t h e f o r s t a t e m e n t .

(30)

A f o r s t a t e m e n t o f t h e f o r m : f o r v := e l & g e 2 ~ g S is e q u i v a l e n t t o t h e s e q u e n c e o f s t a t e m e n t s : j ~ e 1 < = e 2 & h e n ~ e a i n v : = e l ; S ; v : = s u c c ( v ) ; S : . . . ; v : = e 2 ; { a t t h i s p o i n t , v i s u n d e f i n e d } a n d a f o r s t a t e m e n t o f t h e f o r m : ~ o r v : = e l # o w n t o e 2 ~ g S is e q u i v a l e n t t o t h e s t a t e m e n t : i f e l > = e 2 t h e n b e a i n v : = e l ; S ; v : = p r e d ( v ) ; e a ~ { a t t h i s p o i n t , v i s u n d e f i n e d } S; ...; v := e 2 ; S A s a f i n a l e x a m p l e c o n s i d e r t h e f o l l o w i n g p r o g r a m . { p r o g r a m 4 . 6 c o m p u t e I - I / 2 + 1 / 3 - . . . + I / 9 9 9 9 - 1 / 1 0 0 0 0 4 w a y s . 1) l e f t t o r i g h t , i n s u c c e s s i o n 2) l e f t t o r i g h t , a l l p o s a n d n e g t e r m s , t h e n s u b t r a c t 3) r i g h t t o l e f t in s u c c e s s i o n 4) r i g h t t o l e f t , a l l p o s a n d n a g t e r m s , t h e n s u b t r a c t } ~ r o q r a m s u m m i n g ( o u t p u t ) ; v_ar s 1.s 2p ,s 2n ,s 3 , s 4 p ,s 4n . l r p . l r n ,rlp , r l n : r e a l ; i : i n t e g e r ; _be.qin s 1 := 0; s 2 p := O; s 2 n := O; s 3 := O; s 4 p := O; s 4 n := O; fgj& i := I J2o 5 0 0 0 ~L~ b e g i n l r p := I / ( 2 - i - I ) ; { p o s t e r m s , l e f t t o r i g h t } l r n := I / ( 2 - i ) ; { n e g t e r m s , l e f t t o r i g h t } r l p := I / ( I 0 0 0 1 - 2 - i ) ; { p o s t e r m s , r i g h t t o l e f t } r l n := I / ( I 0 0 0 2 - 2 - i ) ; { n e g t e r m s , r i g h t t o l e f t } s l := s l + l r p - l r n : s 2 p := s 2 p + I r p ; s 2 n := s 2 n + I r n : s 3 := s 3 + r l p - r l n ; s 4 p := s 4 p + r l p ; s 4 n := s 4 n + r l n g n U ; w r i t e l n (s I ,s 2p ~s 2n ) ; w r i t e l n ( s 3 , s 4 p - s 4 n ) e n d . ~ . 9 3 0 9 9 1 8 3 0 5 9 5 e - 0 1 6 . 9 3 0 9 7 1 8 3 0 6 1 2 e - 0 1 6 . 9 3 0 9 7 1 8 3 0 5 9 9 e - 0 1 6 . 9 3 0 9 9 1 8 3 0 6 0 1 e - 0 1

References

Related documents

Field Study 4, Episode 3 – The Teacher as a Curricularist Focused on: - defining roles of teachers as curricularist. - discussing the roles of teachers

Despite relatively high rates of interest in one-to-one cessation support among baseline smokers (42%), only a small proportion (12%) of our cohort who smoked at all during

All rights are reserved, whether the whole or part of the material is concerned, specifically those of translation, reprinting, re-use of illustrations, broadcasting, reproduction

a) A licensee who has completed a course as required by (b) below and clinical experience as outlined in (c) below may perform circumcisions. b) A licensee who intends to

Turn on led_Red1 Turn on led_Green2 Wait 30 seconds Turn on led_Yellow1 Turn on led_Yellow2 Wait 3 seconds Turn on led_Red2 Turn on led_Green1 Wait 20 seconds Turn on led_Yellow1

National Conference on Technical Vocational Education, Training and Skills Development: A Roadmap for Empowerment (Dec. 2008): Ministry of Human Resource Development, Department

Based on this infor- mation, Notos uses automated classification and clustering al- gorithms to model network and zone behaviors of legitimate and malicious domains, and then

Once they have noted their selection, lower the right-hand spread and place the double card outjogged onto the left packet as shown in figure 4.. Here is where the