1 / 85

Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer. Lecture 8: Control Structures I. In this (double) lecture. The notion of algorithm Basic control structures: sequence, conditional, loop Decision structures: variants of conditional instruction

jeff
Télécharger la présentation

Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

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. Einführung in die ProgrammierungIntroduction to ProgrammingProf. Dr. Bertrand Meyer Lecture 8: Control Structures I

  2. In this (double) lecture • The notion of algorithm • Basic control structures: sequence, conditional, loop • Decision structures: variants of conditional instruction • Repeating operations: the loop • Loops as approximation strategy: the loop invariant • What does it take to ensure that a loop terminates? • A look at the general problem of loop termination • Lower-level control structures: “Goto” and flowcharts; see rationale for the “control structures of Structured Programming” • Undecidability of the Halting Problem

  3. The notion of algorithm • General definition: An algorithm is the specification of a process to be carried out by a computer

  4. Not quite an algorithm

  5. 5 properties of an algorithm • 1. Defines data to which process will be applied • 2. Every elementary step taken from a set of well-specified actions • 3. Describes ordering(s) of execution of these steps • 4. Properties 2 and 3 based on precisely defined conventions, suitable for an automatic device • 5. For any data, guaranteed to terminate after finite number of steps

  6. Algorithm vs program “Algorithm” usually considered a more abstract notion, independent of platform, programming language etc. In practice, the distinction tends to fade: • Algorithms need a precise notation • Programming languages becoming more abstract However: • In programs, data (objects) are just as important as algorithms • A program typically contains many algorithms and object structures

  7. What makes up an algorithm Basic steps: • Feature callx.f(a) • Assignment • ... Sequencing of these basic steps:CONTROL STRUCTURES (Actually, not much else!)

  8. Control structures Definition: program construct that describes the scheduling of basic actions Three fundamental control structures: • Sequence • Loop • Conditional They are the “Control structures of Structured Programming”

  9. Control structures as problem-solving techniques Sequence: “To achieve C from A, first achieve an intermediate goal B from A, then achieve C from B” Loop: solve the problem on successive approximations of its input set Conditional: solve the problem separately on two or more subsets of its input set

  10. The sequence (or Compound) instruction1 instruction2 ... instructionn

  11. Semicolon as optional separator instruction1 ; instruction2 ; ... ; instructionn

  12. Not quite an algorithm

  13. Correctness of a Compound {P1} instruction1 instruction2 ... • instructioni • ... instructionn {Q1} Precondition of instruction1must hold initially Postcondition of each instructionimust imply precondition of each instructioni+1 Final effect is postcondition of instructionn {P2} {Q2} {Pi} {Qi} implies {Pn } {Qn }

  14. Conditional instruction if Condition then Instructions else Other_instructions end -- Boolean_expression -- Compound -- Compound

  15. Computing the greater of two numbers if a > b then max:=a else max:= b end

  16. As a query maximum (a, b: INTEGER): INTEGER -- The higher of a andb. do if a > b then Result:= a else Result:= b end end

  17. The conditional as problem-solving technique PROBLEM SPACE Region 2 Use technique 2 Region 1 Use technique 1

  18. Basic form if Condition then Instructions else Other_instructions end

  19. A variant of the conditional if Condition then Instructions end

  20. A variant of the conditional Means the same as if Condition then Instructions else end if Condition then Instructions end Empty clause

  21. Nesting if Condition1then Instructions1 else end if Condition2then Instructions2 else end if Condition3then Instructions3 else end if Condition3then Instructions 4 else end ...

  22. Nested structure

  23. Comb-like structure

  24. Comb-like conditional if Condition1then Instructions1 elseifCondition2then Instructions2 elseifCondition3then Instructions3 elseif ... else Instructions0 end

  25. Comb-like structure

  26. More control structure topics • Loops and their invariants • See what it takes to ensure that a loop terminates • Look at the general problem of loop termination • Examine lower-level control structures: “Goto” and flowcharts; see rationale for the “control structures of Structured Programming” • Prove the undecidability of the Halting Problem

  27. Loop from Initialization-- Compound variant Variant_expression-- Integer expression invariant Invariant_expression-- Condition until Exit_condition-- Boolean_expression loop Body-- Compound end

  28. Loop, full form from Initialization-- Compound invariant Invariant_expression-- Boolean_expression variant Variant_expression-- Integer_expression until Exit_condition-- Boolean_expression loop Body-- Compound end

  29. Another loop syntax

  30. Loop, full form from Initialization-- Compound invariant Invariant_expression-- Boolean_expression variant Variant_expression-- Integer_expression until Exit_condition-- Boolean_expression loop Body-- Compound end

  31. Looping over stations of a line Previously: fancy_lineIn textbook: Line8 from fancy.start until fancy.after loop • -- “Do something with fancy.item” fancy.forth end

  32. Operations on a list before after item 1 index count forth back start Commands Queries (boolean) (The cursor)

  33. Operations on a list before after item 1 index count start forth (The cursor)

  34. Looping over stations of a line from • fancy.start until • fancy.after loop -- “Do something with fancy.item” fancy.forth end

  35. Displaying station names from fancy.start until fancy.after loop -- Display name of next station: Console.show (fancy.item) fancy.forth end

  36. Computing the “maximum” of station names from fancy.start ; Result:="" until fancy.after loop • Result :=greater (Result, fancy.item.name) fancy.forth end

  37. Computing the “maximum” of station names from fancy.start ; Result:="" until fancy.after loop Result:= greater (Result, fancy.item.name) fancy.forth end The greater of two strings, alphabetically, e.g. greater("ABC ", "AD")="AD"

  38. In a function highest_name: STRING -- Alphabetically greatest station name of line do from fancy.start ; Result:= "" until fancy.after loop • Result:= greater (Result, fancy.item.name) fancy.forth end end

  39. Forms of loop whileConditiondo Instructionsend • from • Instructions • until • Condition • loop • Instructions • end repeat InstructionsuntilConditionend • for i : a..bdo • Instructions • end • for(Instruction; Condition; Instruction)do • Instructions • end

  40. Loop as approximation strategy name1 name2 namei namen .. Loop body: Result = name1 = Max (names11) i:= i + 1 Result:= greater(Result , fancy.item.name) .. Result = Max (names1 2) Slice .. Result = Max (names1 i) .. Result = Max (names1 n)

  41. Postcondition? highest_name: STRING -- Alphabetically greatest station name of line do from fancy.start ; Result:="" until fancy.after loop Result:= greater (Result, fancy.item.name) fancy.forth end ensure Result /= Void and then not Result.empty • -- Resultis the alphabetically greatest of station names end

  42. Computing the “maximum” of station names • from • fancy.start ; Result:="" • until • fancy.after • loop • Result:= greater (Result, fancy.item.name) • fancy.forth • ensure • -- Resultis the alphabetically greatest of station names • end

  43. The loop invariant Somethingmissing? from fancy.start ; Result:="" invariant fancy.index >= 1 fancy.index <= fancy.count -- Resultis the alphabetically greatest of the -- names of previous stations until fancy.after loop Result:= greater (Result, fancy.item.name) fancy.forth • ensure • -- Resultis the alphabetically greatest of station names end + 1

  44. Loop as approximation strategy name1 name2 namei namen .. Loop body: Result = name1 = Max (names11) i:= i + 1 Result:= greater(Result , fancy.item.name) .. Result = Max (names1 2) .. Result = Max (names1 i) .. Result = Max (names1 n) The loop invariant

  45. Loop invariant (Do not confuse with class invariant) Property that is: • Satisfied after initialization (fromclause) • Preserved by every loop iteration (loopclause) executed with the exit condition (until clause)not satisfied

  46. The loop invariant from fancy.start ; Result:="" invariant fancy.index >= 1 fancy.index <= fancy.count + 1 -- Resultis the alphabetically greatest of the -- names of previous stations until fancy.after loop Result:= greater (Result, fancy.item.name) fancy.forth • ensure • -- Resultis the alphabetically greatest of station names end .. Result = Max (names1 i)

  47. The loop invariant (better) from fancy.start ; Result:="" invariant • index >= 1 • index <= count + 1 • -- If there are any previous stations, • -- Resultis the alphabetically greatest of their names until fancy.after loop Result:= greater (Result, fancy.item.name) fancy.forth • ensure • -- Resultis the alphabetically greatest of station names if any • end .. Result = Max (names1 i)

  48. The effect of the loop Invariant satisfied after initialization from fancy.start ; Result := "" invariant index >= 1 index <= count + 1 --Result is greatest of previous station names until fancy.after loop Result := greater (Result, fancy.item.name) fancy.forth end Invariant satisfied after each iteration Exit condition satisfied at end • At end: invariant and exit condition • All stations visited (fancy.after ) • Result is greatest of their names

  49. Quiz: what’s the invariant? xxxx (a, b: INTEGER): INTEGER -- ????????????????????????????????? require a > 0 ; b > 0 local m, n : INTEGER do from m := a ; n := b invariant -- “????????” variant ???????? until m = n loop ifm > nthen m :=m −n else n := n − m end end Result :=m end

  50. Quiz: what’s the invariant? euclid (a, b: INTEGER): INTEGER -- Greatest common divisor of a and b require a > 0 ; b > 0 local m, n : INTEGER do from m := a ; n := b invariant -- “????????” variant ???????? until m = n loop ifm > nthen m :=m −n else n := n − m end end Result :=m end

More Related