340 likes | 347 Views
Chapter 2.5. Modula 2 Control Instructions. 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
E N D
Chapter 2.5 Modula 2 Control Instructions
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 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 Modula 2 Control Statements
IFBTHENS1ELSES2END B B FALSE TRUE FALSE TRUE S2 S1 S2 S1
IFBTHENSEND B B FALSE TRUE FALSE TRUE S1 S1
FALSE FALSE TRUE IFB1THENS1ELSIFB2THENS2ELSES3END B1 B1 TRUE FALSE B2 B2 TRUE TRUE FALSE S1 S3 S2 S3 S2 S1
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 Modula 2 Control Statements
e e = a e = b e = c S1 S3 S2 CASEeOFa:S1|b:S2|c:S3END e = a ? S1 Yes No e = b ? S2 Yes No e = c ? S3 Yes No ????
e e # a e # b e # c e = a e = b e = c S3 S4 S1 S2 CASEeOFa:S1|b:S2|c:S3ELSES4END e = a ? S1 Yes No e = b ? S2 Yes No e = c ? S3 Yes No S4
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 Modula 2 Control Statements
WHILE B DO S END B B FALSE S TRUE S
WHILE examples WHILEcustomers in shopDO serve customer END WHILENOT end of tape DO play a song END
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 Modula 2 Control Statements
S REPEAT S UNTIL B S B FALSE B TRUE
REPEAT example REPEAT Remove bolt UNTIL No bolts left
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 Modula 2 Control Statements
S S LOOP S1 END
LOOP example LOOP Temperature := ReadSensor; IF Temperature> MaxTemp THEN StopReactor END END
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 Modula 2 Control Statements
LOOP S1; IF B THEN EXITEND; S2 END S1 S1 B TRUE B FALSE S2 S2
LOOP with EXIT exampleRefinement of WHILE example NumberCustomers := CountCustomers; WHILE NumberCustomers > 0 DO ServeCustomer; NumberCustomers := CountCustomers END
LOOP with EXIT example LOOP NumberCustomers := CountCustomers; IF NumberCustomers = 0 THEN EXIT END; ServeCustomer END
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 Modula 2 Control Statements
S FOR i := m TO n DO S END m <= n TRUE m<=n FALSE i := m TRUE i := m S exit loop when i = n TRUE i = n FALSE inc(i) inc(i)
FOR example FOR NBolt := 1 TO 4 DO Remove bolt number NBolt END
FOR example FOR Count := 10 TO 1 BY -1 DO SpeakCardinal(Count,3) END; SpeakString("GO")
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 TRUE x := x-y y := y-x Computing the GCD (1)
Top level design : Read value of x and y Compute g, the GCD of x and y Write the value of g Computing the GCD (2) MODULEGCD1; ... VARx,y,g : CARDINAL; BEGIN (* Read value of x and y *) ... (* Compute g, the GCD of x and y *) ... (* Write value of g *) ... ENDGCD1.
Computing the GCD (3) MODULEGCD1; FROMInOutIMPORTWriteString,ReadCard; VARx,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 *) ... ENDGCD1.
Computing the GCD (4) MODULEGCD1; FROMInOutIMPORTWriteString,ReadCard; VARx,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 *) ... ENDGCD1.
Computing the GCD (5) MODULEGCD1; FROMInOutIMPORTWriteString,ReadCard, WriteCard, WriteLn; VARx,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 ENDGCD1.
Computing the GCD (6) MODULEGCD1; FROMInOutIMPORTWriteString,ReadCard, WriteCard, WriteLn, Read; VARx,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 *) ... ENDGCD1.