1 What makes up an algorithm The sequence (or Compound) instruction - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 What makes up an algorithm The sequence (or Compound) instruction - - PDF document

Not quite an algorithm Chair of Softw are Engineering Einfhrung 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:


slide-1
SLIDE 1

1

Einführung in die Programmierung Introduction to Programming

  • Prof. Dr. Bertrand Meyer

October 2006 – February 2007

Chair of Softw are Engineering

Lecture 9: Control Structures I

I ntro. to Programming, lecture 9: Control structures I 2 I ntro. to Programming, lecture 9: Control structures I 3

The notion of algorithm

General definition: An algorithm is the specification of a process to be carried out by a computer

I ntro. to Programming, lecture 9: Control structures I 4

Not quite an algorithm

I ntro. to Programming, lecture 9: Control structures I 5

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

  • f steps
I ntro. to Programming, lecture 9: Control structures I 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

slide-2
SLIDE 2

2

I ntro. to Programming, lecture 9: Control structures I 7

What makes up an algorithm

Basic steps:

Feature call x.f (a ) Assignment ...

Sequencing of these basic steps: CONTROL STRUCTURES

I ntro. to Programming, lecture 9: Control structures I 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”

I ntro. to Programming, lecture 9: Control structures I 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

I ntro. to Programming, lecture 9: Control structures I 10

The sequence (or Compound) instruction1 instruction2

...

instructionn

I ntro. to Programming, lecture 9: Control structures I 11

Semicolon as optional separator instruction1 ; instruction2 ;

... ;

instructionn

I ntro. to Programming, lecture 9: Control structures I 12

Not quite an algorithm

slide-3
SLIDE 3

3

I ntro. to Programming, lecture 9: Control structures I 13

Correctness of a Compound

Precondition of instruction1 must hold initially Postcondition of each instructioni must imply precondition of each instructioni+1 Final effect is postcondition of instructionn

I ntro. to Programming, lecture 9: Control structures I 14

Conditional instruction

if Condition then Instructions else Other_instructions end

  • - Boolean_expression
  • - Compound
  • - Compound
I ntro. to Programming, lecture 9: Control structures I 15

Computing the greater of two numbers

if a > b then max := a else max := b end

I ntro. to Programming, lecture 9: Control structures I 16

As a function

if a > b then Result := a else Result := b end maximum (a, b : INTEGER): INTEGER

  • - The higher of a and b

do end

I ntro. to Programming, lecture 9: Control structures I 17

The conditional as problem-solving technique

Region 1 Region 2 PROBLEM SPACE Use technique 1 Use technique 2

I ntro. to Programming, lecture 9: Control structures I 18

Basic form

if Condition then Instructions else Other_instructions end

slide-4
SLIDE 4

4

I ntro. to Programming, lecture 9: Control structures I 19

A variant of the conditional

if Condition then Instructions end

I ntro. to Programming, lecture 9: Control structures I 20

A variant of the conditional

(Means the same as if Condition then Instructions else end with an empty “else” clause) if Condition then Instructions end

I ntro. to Programming, lecture 9: Control structures I 21

Nesting if Condition1 then Instructions1 else end

if Condition2 then Instructions2 else end

if Condition3 then Instructions3 else end

...

I ntro. to Programming, lecture 9: Control structures I 22

Nested structure

I ntro. to Programming, lecture 9: Control structures I 23

Comb-like structure

I ntro. to Programming, lecture 9: Control structures I 24

Comb-like conditional

if Condition1 then Instructions 1 elseif Condition 2 then Instructions 2 elseif Condition3 then Instructions 3 elseif ... else Instructions 0 end

slide-5
SLIDE 5

5

I ntro. to Programming, lecture 9: Control structures I 25

Comb-like structure

I ntro. to Programming, lecture 9: Control structures I 26

What we have seen

I ntro. to Programming, lecture 9: Control structures I 27

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 28

End of lecture 9