1 Iteration and Recursion (Section 6.5 – 6.6) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Statement-Level Control Structures
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
Controlling Program Flows
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
1 Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence.
ISBN Lecture 08 Statement-Level Control Structures.
1 Control Flow Aaron Bloomfield CS 415 Fall 2005.
COMP4730/2002/lec8/H.Melikian Statement-Level Control Structures Introduction Compound Statements Selection Statements Iterative Statements Unconditional.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
Iteration and Recursion Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 21.
1 Control Flow: Expressions, Sequencing & Selection (Section ) A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
Chapter 8: Statement-Level Control Structures
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
COP4020 Programming Languages Control Flow Prof. Robert van Engelen.
Lecture 19: Control Abstraction (Section )
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
W E E K F I V E Statement-Level Control Structures.
LECTURE 18 Control Flow. CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Programming Techniques
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Lecture 18 Control Flow.
Def: A control structure is a control statement and
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
Lecture 14: Iteration and Recursion (Section 6.5 – 6.6)
Chapter 8: Control Structures
Chap. 6 :: Control Flow Michael L. Scott.
Statement-Level Control Structures
Control Flow.
Control Flow Chapter 6.
Conditional Statements
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
Chap. 6 :: Control Flow Michael L. Scott.
Iteration Implemented through loop constructs (e.g., in C++)
Statement-Level Control Structures
Chapter8: Statement-Level Control Structures April 9, 2019
Chapter 8: Statement Level Control Structures
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
PZ07B - Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

1 Iteration and Recursion (Section 6.5 – 6.6) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott

2 Control Flow Mechanisms SequencingSequencing –Textual order, Precedence in Expression SelectionSelection IterationIteration Procedural abstractionProcedural abstraction RecursionRecursion ConcurrencyConcurrency NondeterminacyNondeterminacy

3 Iteration and Recursion These two control flow mechanism allow a computer to perform the same set of operations repeatedlyThese two control flow mechanism allow a computer to perform the same set of operations repeatedly They make computers usefulThey make computers useful –Go beyond the power of deterministic finite automata Imperative languages mainly rely on iterationsImperative languages mainly rely on iterations Functional languages make more use of recursionFunctional languages make more use of recursion

4 Iteration Iteration usually takes the form of loopsIteration usually takes the form of loops There are two principal varietiesThere are two principal varieties –Enumeration-controlled loops »E.g. for (int i = 0; i <= 10; i++) { …} –Logically controlled loops »E.g. int i = 0; while (i <= 10) { … i++; i++;}

5 Iteration Enumeration-controlled loops Enumeration-controlled loopsEnumeration-controlled loops –Index variable –Step size and bounds –Body of the loop Fortran I, II and IVFortran I, II and IV do 10 i = 1, 10, 2 10: i = 1 do 10 i = 1, 10, 2 10: i = 1...  … i = i + 2 i = i : continue if i <= 10 goto 10 –The value of i is tested at the end of the loop »When continue is reached –Implementation is very fast »This statement is very close to assembly code

6 Iteration Enumeration-controlled loops Problems:Problems: –Loop boundaries must be integer »Expressions are not allowed –The index variable can change within the body of the loop –Goto statements may jump in and out of the loop –The value of i after the termination of the loop is implementation dependent –The test of the loop takes place at the end, so the body is executed at least once »Even if the lower bound is larger than the upper bound!

7 Iteration Loop should check for empty boundsLoop should check for empty bounds Code generationCode generation –Optimization

8 Iteration Backward loopsBackward loops –Previous code assumed a positive step size

9 Iteration Access to Index Outside the Loop The value of the index variable at the end of loop is undefined in several languagesThe value of the index variable at the end of loop is undefined in several languages –E.g. Fortran, Pascal Compilers can fix this, but…Compilers can fix this, but… –Generating slower code

10 Iteration Access to Index Outside the Loop The value of the index after the loop completes may not be validThe value of the index after the loop completes may not be valid –E.g. var c: ‘a’..’z’; … for c:= ‘a’ to ‘z’ do begin …end; (* what comes after ‘z’? *) In summary, even the simplest type of loop requires a good designIn summary, even the simplest type of loop requires a good design –You will use language with poorly designed statements!

11 Iteration Iterators Iterators generalize enumeration-controlled loopsIterators generalize enumeration-controlled loops –In the previous examples, the iteration was always over the elements of an arithmetic sequence Iterators are used to enumerate the elements of any well-defined setIterators are used to enumerate the elements of any well-defined set –E.g. In Clu, for i in from_to_by(first, last, step) do …end –Notice some similarity to Perl’s foreach statement

12 Iteration Iterators Clu allows any set-like abstract data type to provide an iteratorClu allows any set-like abstract data type to provide an iterator –E.g. integer iterator

13 Iterations Iterators Iterators can also be based on object-oriented design patternsIterators can also be based on object-oriented design patterns –Java’s Iterator interface » ction.html ction.htmlhttp://java.sun.com/docs/books/tutorial/collections/interfaces/colle ction.html Enumeration-controlled loops evolved significantly since FORTRAN’s original forEnumeration-controlled loops evolved significantly since FORTRAN’s original for

14 Iteration Logically-Controlled Loops They have fewer semantic subtletiesThey have fewer semantic subtleties –The programmer has to be more explicit There are some design optionsThere are some design options –Pre-test » –Post-test » –Midtest »C/C++/Java idiom: for (;;) {... if (condition) break... }

15 Recursion Recursion requires no special syntaxRecursion requires no special syntax Recursion and logically-controlled iteration are equally powerfulRecursion and logically-controlled iteration are equally powerful ExampleExample –Compute the greatest common divisor –It can be defined as a recurrence: for a, b positive integers

16 Recursion Implementation using recursion is directImplementation using recursion is direct Iteration Recursion

17 Nondeterminacy Nondeterministic constructs make choices between alternatives deliberately unspecifiedNondeterministic constructs make choices between alternatives deliberately unspecified This mechanism is specially useful in concurrent programsThis mechanism is specially useful in concurrent programs –Message-based concurrent languages

18 Nondeterminacy This is a very practical matterThis is a very practical matter –Event-driven programming is related to nondeterminacy »See events and listeners in Java » ml mlhttp://java.sun.com/docs/books/tutorial/uiswing/overview/event.ht ml –Non-blocking IO is related to nondeterminacy »See the lastest addition to Java (1.4): Channels and Selectors »