1 / 34

Chapter 2.5

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

Download Presentation

Chapter 2.5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2.5 Modula 2 Control Instructions

  2. 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

  3. 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

  4. IFBTHENS1ELSES2END B B FALSE TRUE FALSE TRUE S2 S1 S2 S1

  5. IFBTHENSEND B B FALSE TRUE FALSE TRUE S1 S1

  6. FALSE FALSE TRUE IFB1THENS1ELSIFB2THENS2ELSES3END B1 B1 TRUE FALSE B2 B2 TRUE TRUE FALSE S1 S3 S2 S3 S2 S1

  7. 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

  8. 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 ????

  9. 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

  10. 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

  11. WHILE B DO S END B B FALSE S TRUE S

  12. WHILE examples WHILEcustomers in shopDO serve customer END WHILENOT end of tape DO play a song END

  13. 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

  14. S REPEAT S UNTIL B S B FALSE B TRUE

  15. REPEAT example REPEAT Remove bolt UNTIL No bolts left

  16. 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

  17. S S LOOP S1 END

  18. LOOP example LOOP Temperature := ReadSensor; IF Temperature> MaxTemp THEN StopReactor END END

  19. 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

  20. LOOP S1; IF B THEN EXITEND; S2 END S1 S1 B TRUE B FALSE S2 S2

  21. LOOP with EXIT exampleRefinement 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. 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

  24. FOR statement syntax(simple version)

  25. 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)

  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. 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)

  30. 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.

  31. 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.

  32. 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.

  33. 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.

  34. 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.

More Related