Not quite an algorithm Chair of Softw are Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer October 2006 – February 2007 Lecture 9: Control Structures I I ntro. to Programming, lecture 9: Control structures I 4 5 properties of an algorithm Defines data to which process will be applied Every elementary step taken from a set of well-specified actions Describes ordering(s) of execution of these steps 2 and 3 based on precisely defined conventions, suitable for execution by an automatic computer For any data, guaranteed to terminate after finite number of steps I ntro. to Programming, lecture 9: Control structures I 2 I ntro. to Programming, lecture 9: Control structures I 5 The notion of algorithm Algorithm vs program General definition: “Algorithm” usually considered a more abstract notion, independent of platform, programming language etc. An algorithm is the specification of a process to be carried out by In practice, the distinction tends to fade: a computer � 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 I ntro. to Programming, lecture 9: Control structures I 3 I ntro. to Programming, lecture 9: Control structures I 6 1
What makes up an algorithm The sequence (or Compound) instruction 1 Basic steps: � Feature call x . f ( a ) instruction 2 � Assignment � ... ... instruction n Sequencing of these basic steps: CONTROL STRUCTURES I ntro. to Programming, lecture 9: Control structures I 7 I ntro. to Programming, lecture 9: Control structures I 10 Control structures Semicolon as optional separator Definition: program construct that describes the instruction 1 ; scheduling of basic actions instruction 2 ; Three fundamental control structures: � Sequence ... ; � Loop � Conditional instruction n They are the “Control structures of Structured Programming” I ntro. to Programming, lecture 9: Control structures I 8 I ntro. to Programming, lecture 9: Control structures I 11 Not quite an algorithm 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 I ntro. to Programming, lecture 9: Control structures I 9 I ntro. to Programming, lecture 9: Control structures I 12 2
Correctness of a Compound As a function maximum ( a, b : INTEGER ): INTEGER Precondition of instruction 1 must hold initially -- The higher of a and b do Postcondition of each instruction i must imply if precondition of each instruction i+1 a > b then Final effect is postcondition of instruction n Result := a else Result := b end end I ntro. to Programming, lecture 9: Control structures I 13 I ntro. to Programming, lecture 9: Control structures I 16 Conditional instruction The conditional as problem-solving technique if PROBLEM SPACE - - Boolean_expression Condition then Region 2 Instructions - - Compound else Other_instructions Region 1 - - Compound end Use technique 2 Use technique 1 I ntro. to Programming, lecture 9: Control structures I 14 I ntro. to Programming, lecture 9: Control structures I 17 Computing the greater of two numbers Basic form if Condition then Instructions if else a > b Other_instructions then end max := a else max := b end I ntro. to Programming, lecture 9: Control structures I 15 I ntro. to Programming, lecture 9: Control structures I 18 3
A variant of the conditional Nested structure if Condition then Instructions end I ntro. to Programming, lecture 9: Control structures I 19 I ntro. to Programming, lecture 9: Control structures I 22 A variant of the conditional Comb-like structure if Condition then Instructions end (Means the same as if Condition then Instructions else end with an empty “ else ” clause) I ntro. to Programming, lecture 9: Control structures I 20 I ntro. to Programming, lecture 9: Control structures I 23 Nesting Comb-like conditional if Condition 1 then if Condition 1 then Instructions 1 Instructions 1 elseif Condition 2 then else Instructions 2 if Condition 2 then elseif Condition 3 then Instructions 3 Instructions 2 else elseif if Condition 3 then ... Instructions 3 else ... else Instructions 0 end end end end I ntro. to Programming, lecture 9: Control structures I 21 I ntro. to Programming, lecture 9: Control structures I 24 4
Comb-like structure End of lecture 9 I ntro. to Programming, lecture 9: Control structures I 25 I ntro. to Programming, lecture 9: Control structures I 28 What we have seen I ntro. to Programming, lecture 9: Control structures I 26 What we have seen � The notion of algorithm � Basic properties � Difference with “program” � The notion of control structure � Correctness of an instruction � Control structure: sequence � Control structure: conditional � Nesting, and how to avoid it I ntro. to Programming, lecture 9: Control structures I 27 5
Recommend
More recommend