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

Slides:



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

Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
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 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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
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.
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 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.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
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.
© 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 7.
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.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
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.
UNIT II Decision Making And Branching Decision Making And Looping
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
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 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 8: Control Structures I.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
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 3.
Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 1 COSC3311 – Software Design Loop invariant/variant.
COMP Loop Statements Yi Hong May 21, 2015.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Introduction to Algorithms
Chapter 4 Repetition Statements (loops)
GC211Data Structure Lecture2 Sara Alhajjam.
Outline Altering flow of control Boolean expressions
The following slides contain advanced material and are optional.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Introduction to Algorithms
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Presentation transcript:

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

2 News Mock exam in 2 weeks (November 10)  You have to be present  No assignment that week  The week after we will discuss the results

3 Today  Conditional  Loop  Linked list

4 Inside the routine body  The body of each routine consists of instructions (command calls, creations, assignments, etc.)  In the programs you’ve seen so far they were always executed in the order they were written create passenger.make_with_route (Route3, 1.5) passenger.go passenger. set_reiterate (True) Paris.put_passenger (passenger) create tram.make_with_line (Line1) tram.start Paris.put_tram (tram)  Programming languages have structures that allow you to change the order of execution

5 Structured programming  If the order of execution could be changed arbitrarily, it would be hard to understand programs  In structured programming instructions can be combined only in three ways: i_1 c i_2 i_1i_2 c i compound conditionalloop TrueFalse True  Each of these blocks has a single entrance and exit and is itself an instruction Instruction Condition

6 Conditional  Basic syntax: if c then i_1 else i_2 end  c is a boolean expression (e.g., entity, query call of type BOOLEAN)  else-part is optional: if c then i_1 end Condition Instruction

7 Compilation error? Runtime error? (1) f (x, y: INTEGER): INTEGER do if (x // y) then 1 else 0 end Hands-On Compilation error: integer expression instead of boolean Compilation error: expression instead of instruction Compilation error: expression instead of instruction

8 f (x, y: INTEGER): INTEGER do if (False) then Result := x // y end if (x /= 0) then Result := y // x end Hands-On Compilation error? Runtime error? (2) Everything is OK (during both compilation and runtime)

9 Calculating function’s value f (max: INTEGER; s: STRING): STRING do if s.is_equal (“Java”) then Result := “J**a” else if s.count > max then Result := “ ” end Calculate the value of:  f (3, “Java”)  f (20, “Immatrikulationsbestätigung”)  f (6, “Eiffel”) → “J**a” → “ ” → Void Hands-On

10 What does this routine do? f (x: REAL): REAL do if (x >= 0) then Result := x else Result := -x end abs Hands-On

11 Write a routine... ... that computes the maximum of two integers: max (a, b: INTEGER): INTEGER ... that increases time by one second inside class TIME: class TIME hour, minute, second: INTEGER second_forth do... end... end Hands-On

12 Comb-like conditional If there are more than two alternatives, you can use the syntax: if c1 then i_1 elseif c2 then i_2... elseif c_n then i_n else i_e end instead of: if c_1 then i_1 else if c_2 then i_2 else... if c_n then i_n else i_e end... end Condition Instruction

13 Multiple choice If all the conditions have a specific structure, you can use the syntax: inspect expression when const_1 then i_1 when const_2 then i_2... when const_n1.. const_n2 then i_n else i_e end Integer or character expression Integer or character constant Instruction Interval

14 Lost in conditions Rewrite the following multiple choice:  using a comb-like conditional  using nested conditionals inspect user_choice when 0 then print (“Here is your hamburger”) when 1 then print (“Here is your Coke”) else print (“Sorry, not on the menu today!”) end Hands-On

15 Lost in conditions: solution if user_choice = 0 then print (“Here is your hamburger”) elseif user_choice = 1 then print (“Here is your Coke”) else print (“Sorry, not on the menu today!”) end if user_choice = 0 then print (“Here is your hamburger”) else if user_choice = 1 then print (“Here is your Coke”) else print (“Sorry, not on the menu today!”) end

16 Loop Instruction Optional Boolean expression Instruction Syntax: from initialization invariant inv until exit_condition loop body variant var end Integer expression

17 Simple loop (1) How many times will the body of the following loop 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 10

18 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! ∞

19 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

20 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?)

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

22 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 Hands-On

23 Writhing loops (solution) fibonacci (n: INTEGER): INTEGER local a, b, i: INTEGER do if n <= 1 then Result := n else from a := fibonacci (0) b := fibonacci (1) i := 1 invariant a = fibonacci (i - 1) b = fibonacci (i ) until i = n loop Result := a + b a := b b := Result i := i + 1 variant n - i end Hands-On

24 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

25 Two kinds of queues Live queue 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...

26 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 (LINKABLE) right

27 INT_LINKABLE class INT_LINKABLE create put feature item: INTEGER put (i: INTEGER) do item := i end right: INT_LINKABLE put_right (other: INT_LINKABLE) do right := other end end

28 INT_LINKED_LIST class INT_LINKED_LIST feature first_element: INT_LINKABLE -- First cell of the list last_element: INT_LINKABLE -- Last cell of the list count: INTEGER -- Number of elements in the list... end

29 4 item right item right item right first_element last_element count3 item right : inserting at the end (INT_LINKABLE) (INT_LINKABLE) INT_LINKED_LIST

30 extend (v: INTEGER) -- Add v to end. local new: INT_LINKABLE do create new.put (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

31 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

32 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_after (i, j: INTEGER) do... end Hands-On

33 INT_LINKED_LIST: sum_of_positive sum_of_positive: INTEGER -- Some of positive elements local temp: INT_LINKABLE do from temp := first_element until temp = Void loop if temp.item > 0 then Result := Result + temp.item end temp := temp.right end

34 INT_LINKED_LIST: insert_after insert_after (i, j: INTEGER) -- Insert `j’ after `i’ if present local temp, new: INT_LINKABLE do from temp := first_element until temp = Voidor else temp.item = i loop temp := temp.right end if temp /= Void then create new.put (j) new.put_right (temp.right) temp.put_right (new) count := count + 1 if temp = last_element then last_element := new end