Download presentation
Presentation is loading. Please wait.
Published byGian Crissey Modified over 9 years ago
1
Chapter 2.5 Modula 2 Control Instructions
2
Modula 2 Control Statements Selection statements –BOOLEAN Selector : IF statement –Ordinal Selector : CASE statement Iteration statements –Initial termination test : WHILE loop –Final termination test : REPEAT loop –Infinite loop : LOOP –Termination of LOOP : EXIT –Known number of iterations : FOR PROCEDURE call : chapter 7
3
Modula 2 Control Statements Selection statements –BOOLEAN Selector : IF statement –Ordinal Selector : CASE statement Iteration statements –Initial termination test : WHILE loop –Final termination test : REPEAT loop –Infinite loop : LOOP –Termination of LOOP : EXIT –Known number of iterations : FOR PROCEDURE call : chapter 7
4
IF B THEN S1 ELSE S2 END B S2S1 FALSETRUE B FALSE TRUE S2S1
5
IF B THEN S END B S1 FALSETRUE B FALSE TRUE S1
6
IF B1 THEN S1 ELSIF B2 THEN S2 ELSE S3 END S3S2 B2 FALSE TRUE B1 FALSE TRUE S3 S1 B1 FALSE TRUE S1 S2 FALSE TRUE B2
7
Modula 2 Control Statements Selection statements –BOOLEAN Selector : IF statement –Ordinal Selector : CASE statement Iteration statements –Initial termination test : WHILE loop –Final termination test : REPEAT loop –Infinite loop : LOOP –Termination of LOOP : EXIT –Known number of iterations : FOR PROCEDURE call : chapter 7
8
CASE e OF a:S1|b:S2|c:S3 END No e = a ? S1 Yes No e = b ? S2 Yes No e = c ? S3 Yes ???? e S1 S2 e = ae = be = c S3
9
CASE e OF a:S1|b:S2|c:S3 ELSE S4 END No e = a ? S1 Yes No e = b ? S2 Yes No e = c ? S3 Yes S4 e S1 S3 S2 e = ae = be = c S4 e # a e # b e # c
10
Modula 2 Control Statements Selection statements –BOOLEAN Selector : IF statement –Ordinal Selector : CASE statement Iteration statements –Initial termination test : WHILE loop –Final termination test : REPEAT loop –Infinite loop : LOOP –Termination of LOOP : EXIT –Known number of iterations : FOR PROCEDURE call : chapter 7
11
WHILE B DO S END B S TRUE FALSE S B
12
WHILE examples WHILE customers in shop DO serve customer END WHILE NOT end of tape DO play a song END
13
Modula 2 Control Statements Selection statements –BOOLEAN Selector : IF statement –Ordinal Selector : CASE statement Iteration statements –Initial termination test : WHILE loop –Final termination test : REPEAT loop –Infinite loop : LOOP –Termination of LOOP : EXIT –Known number of iterations : FOR PROCEDURE call : chapter 7
14
REPEAT S UNTIL B S B S B TRUE FALSE
15
REPEAT example REPEAT Remove bolt UNTIL No bolts left
16
Modula 2 Control Statements Selection statements –BOOLEAN Selector : IF statement –Ordinal Selector : CASE statement Iteration statements –Initial termination test : WHILE loop –Final termination test : REPEAT loop –Infinite loop : LOOP –Termination of LOOP : EXIT –Known number of iterations : FOR PROCEDURE call : chapter 7
17
S S LOOP S1 END
18
LOOP example LOOP Temperature := ReadSensor; IF Temperature > MaxTemp THEN StopReactor END
19
Modula 2 Control Statements Selection statements –BOOLEAN Selector : IF statement –Ordinal Selector : CASE statement Iteration statements –Initial termination test : WHILE loop –Final termination test : REPEAT loop –Infinite loop : LOOP –Termination of LOOP : EXIT –Known number of iterations : FOR PROCEDURE call : chapter 7
20
B S2 TRUE FALSE S2 B S1 LOOP S1; IF B THEN EXIT END; S2 END
21
LOOP with EXIT example Refinement of WHILE example NumberCustomers := CountCustomers; WHILE NumberCustomers > 0 DO ServeCustomer; NumberCustomers := CountCustomers END
22
LOOP with EXIT example LOOP NumberCustomers := CountCustomers; IF NumberCustomers = 0 THEN EXIT END; ServeCustomer END
23
Modula 2 Control Statements Selection statements –BOOLEAN Selector : IF statement –Ordinal Selector : CASE statement Iteration statements –Initial termination test : WHILE loop –Final termination test : REPEAT loop –Infinite loop : LOOP –Termination of LOOP : EXIT –Known number of iterations : FOR PROCEDURE call : chapter 7
24
FOR statement syntax (simple version)
25
FOR i := m TO n DO S END S m <= n S TRUE FALSE i := m inc(i) i = n TRUE FALSE exit loop when i = n i := m inc(i) TRUE
26
FOR example FOR NBolt := 1 TO 4 DO Remove bolt number NBolt END
27
FOR statement syntax (complete version)
28
FOR example FOR Count := 10 TO 1 BY -1 DO SpeakCardinal(Count,3) END; SpeakString("GO")
29
Computing the GCD (1) Specifications : –Given two cardinal numbers x and y –Compute G, the GCD of x and y Algorithm : GCD(x, y) = GCD(x, y-x)(x < y) GCD(x, y) = GCD(x-y, y)(x > y) WHILE x # y x > y x := x-yy := y-x TRUE
30
Computing the GCD (2) Top level design : Read value of x and y Compute g, the GCD of x and y Write the value of g MODULE GCD1;... VAR x,y,g : CARDINAL; BEGIN (* Read value of x and y *)... (* Compute g, the GCD of x and y *)... (* Write value of g *)... END GCD1.
31
Computing the GCD (3) MODULE GCD1; FROM InOut IMPORT WriteString, ReadCard; VAR x,y,g : CARDINAL; BEGIN (* Read value of x and y *) WriteString("Enter a cardinal number please "); ReadCard(x); WriteString("Enter a cardinal number please "); ReadCard(y); (* Compute g, the GCD of x and y *)... (* Write value of g *)... END GCD1.
32
Computing the GCD (4) MODULE GCD1; FROM InOut IMPORT WriteString, ReadCard; VAR x,y,g : CARDINAL; BEGIN (* Read value of x and y *)... (* Compute g, the GCD of x and y *) WHILE x # y DO IF x > y THEN x := x - y ELSE y := y - x END (* IF *) END; (* WHILE *) g := x; (* Write value of g *)... END GCD1.
33
Computing the GCD (5) MODULE GCD1; FROM InOut IMPORT WriteString, ReadCard, WriteCard, WriteLn; VAR x,y,g : CARDINAL; BEGIN (* Read value of x and y *)... (* Compute g, the GCD of x and y *)... (* Write value of g *) WriteString(" The GCD of these numbers is : "); WriteCard(g,10); WriteLn END GCD1.
34
Computing the GCD (6) MODULE GCD1; FROM InOut IMPORT WriteString, ReadCard, WriteCard, WriteLn, Read; VAR x,y,g : CARDINAL; ch : CHAR; BEGIN (* Read value of x and y *)... (* Compute g, the GCD of x and y *) WHILE x # y DO WriteString("x = "); WriteCard(x,4); WriteString("; y = "); WriteCard(y,4); WriteLn; Read(ch); IF x > y THEN x := x - y ELSE y := y - x END (* IF *) END; (* WHILE *) g := x; (* Write value of g *)... END GCD1.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.