Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 11 27. October 2008.

Slides:



Advertisements
Similar presentations
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Advertisements

Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 5 6 October 2008.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Introduction to Computing Science and Programming I
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 14: Container Data Structures.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Design by Contract. Specifications Correctness formula (Hoare triple) {P} A {Q} – A is some operation (for example, a routine body) – P and Q are predicates.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 4 30 September 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 14: Container Data Structures.
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: More on references and.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 11.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 1 COSC3311 – Software Design Loop invariant/variant.
1 Assertions. 2 A boolean expression or predicate that evaluates to true or false in every state In a program they express constraints on the state that.
COMP Loop Statements Yi Hong May 21, 2015.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4 Repetition Statements (loops)
5.13 Recursion Recursive functions Functions that call themselves
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Repetition-Counter control Loop
While Loops in Python.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
Lecture 2: Axiomatic semantics
Algorithm Discovery and Design
3.1 Iteration Loops For … To … Next 18/01/2019.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Lecture 2: Axiomatic semantics
While Loops in Python.
Presentation transcript:

Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008

Chair of Software Engineering Today’s learning goals  Control structures: loop  Linked lists 2

Chair of Software Engineering Loop Instruction Boolean expression Integer expression Boolean expression Instruction Optional  Syntax: from initialization invariant inv variant var until exit_condition loop body end

Chair of Software Engineering Simple loop (1)  How many times the body of the following loop will be executed? i: INTEGER... from i := 1 until i > 10 loop print (“I will not say bad things about assistants”) i := i + 1 end Hands-On In Eiffel we usually start counting from 1

Chair of Software Engineering Simple loop (2) Hands-On  And what about this one? i: INTEGER... from i := 10 until i < 1 loop print (“I will not say bad things about assistants”) end Caution! Loops can be infinite!

Chair of Software Engineering What does this function do? f (n: INTEGER): INTEGER is local i: INTEGER do from i := 2 Result := 1 until i > n loop Result := Result * i i := i + 1 end factorial Hands-On

Chair of Software Engineering Invariant and variant  Loop invariant (do not mix with class invariant)  holds after execution of from clause and after each execution of loop clause  captures how the loop iteratively solves the problem: e.g. “to calculate the sum of all n elements in a list, on each iteration i (i = 1..n) the sum of first i elements is obtained”  Loop variant  integer expression that is nonnegative after execution of from clause and after each execution of loop clause and strictly decreases with each iteration  a loop with a variant can not be infinite (why?)

Chair of Software Engineering Invariant and variant  What are the invariant and variant of the “factorial” loop? from i := 2 Result := 1 invariant ? variant ? until i > n loop Result := Result * i i := i + 1 end Hands-On i = 2; Result = 1 = 1!i = 3; Result = 2 = 2!i = 4; Result = 6 = 3! Result = factorial (i - 1) n – i + 2

Chair of Software Engineering Writing loops  Implement a function that calculates Fibonacci numbers, using a loop fibonacci (n: INTEGER): INTEGER -- n-th Fibonacci number require n_non_negative: n >= 0 ensure first_is_zero: n = 0 implies Result = 0 second_is_one: n = 1 implies Result = 1 other_correct: n > 1 implies Result = fibonacci (n - 1) + fibonacci (n - 2) end 9 Hands-On

Chair of Software Engineering Writhing loops (solution) fibonacci (n: INTEGER): INTEGER local a, b, i: INTEGER do from a := 0 b := 1 i := 2 invariant a = fibonacci (i - 2) b = fibonacci (i - 1) until i > n loop a := a + b b := a + b i := i + 2 end if i = n + 1 then Result := b else -- i = n + 2 Result := a end 10 Hands-On

Chair of Software Engineering  Electronic queue (like in the post office) Two kinds of queues May I stand here? Pleeease... Andy Bob Chuck Dan Ed Fred Garry Hugo No! We’ll have to take tickets again! Ilinca

Chair of Software Engineering Two kinds of queues  Live queue 12 Andy Bob Chuck Dan Ed Fred Garry Hugo Bob is after me I am the last Sure! Just remember that Dan is after you Chuck is after me Dan is after me... Ilinca May I stand here? Pleeease...

Chair of Software Engineering LINKABLE  To make it possible to link infinitely many similar elements together, each element should contain a reference to the next element class LINKABLE feature... right: LINKABLE end data right data right data right data right (LINKABLE)

Chair of Software Engineering INT_LINKABLE class INT_LINKABLE create set_item feature item: INTEGER set_item (an_item: INTEGER) do item := an_item end right: INT_LINKABLE put_right (other: INT_LINKABLE) do right := other end end

Chair of Software Engineering INT_LINKED_LIST 4 item right item right item right first_element last_element count3 item right : inserting at the end (INT_LINKABLE) (INT_LINKABLE)

Chair of Software Engineering extend (v: INTEGER) -- Add v to end. local new: INT_LINKABLE do create new.set_item (v) if first_element = Void then first_element := new else last_element.put_right (new) end last_element := new count := count + 1 end INT_LINKED_LIST: inserting at the end

Chair of Software Engineering INT_LINKED_LIST: search has (v: INTEGER): BOOLEAN -- Does list contain v? local temp: INT_LINKABLE do from temp := first_element until (temp = Void) or Result loop if temp.item = v then Result := True end temp := temp.right end

Chair of Software Engineering INT_LINKED_LIST  Write a routine that  calculates the sum of all positive values in a list sum_of_positive: INTEGER do... end  inserts an element after the first occurrence of a given value and does nothing if the value is not found insert (a_new, an_item: INTEGER) do... end Hands-On