Download
einf hrung in die programmierung introduction to programming prof dr bertrand meyer n.
Skip this Video
Loading SlideShow in 5 Seconds..
Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer PowerPoint Presentation
Download Presentation
Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

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

63 Views Download Presentation
Download Presentation

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Einführung in die ProgrammierungIntroduction to ProgrammingProf. Dr. Bertrand Meyer Lecture 8: Control Structures

  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. 2 and 3 based on precisely defined conventions, suitable for execution by an automatic computer • 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

  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 Precondition of instruction1must hold initially Postcondition of each instructionimust imply precondition of each instructioni+1 Final effect is postcondition of instructionn

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

  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 and in the textbook: fancy_line 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. Assignment Result := "XYZ " -- Change the value ofResultto "XYZ"

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

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

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

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

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

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

  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 highest of the -- names of previous stations until fancy.after loop Result:= greater (Result, fancy.item.name) fancy.forth end .. Result = Max (names1 n)

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

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

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

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