1 Control Flow Aaron Bloomfield CS 415 Fall 2005.

Slides:



Advertisements
Similar presentations
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Advertisements

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.
Programming Languages and Paradigms
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.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
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.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
COMP4730/2002/lec8/H.Melikian Statement-Level Control Structures Introduction Compound Statements Selection Statements Iterative Statements Unconditional.
Structure of Programming Language
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Control Structures Programs have 4 basic control structures:
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
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement.
Chapter 8: Statement-Level Control Structures
Control Structures sequence of execution of high-level statements.
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)
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.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
COP4020 Programming Languages Control Flow Prof. Robert van Engelen.
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.
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.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
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.
Control Flow Chapter 6.
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
COP4020 Programming Languages
Chap. 6 :: Control Flow Michael L. Scott.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Statement-Level Control Structures
The structure of programming
Thinking procedurally
CSC215 Lecture Control Flow.
Chapter 8: Statement Level Control Structures
Presentation transcript:

1 Control Flow Aaron Bloomfield CS 415 Fall 2005

2 Side Effects & Idempotent Functions Side Effects – a function has a side effect if it influences subsequent computation in any way other than by returning a value. A side effect occurs when a function changes the environment in which it exists Idempotent – an idempotent function is one that if called again with the same parameters, will always give the same result

3 Referentially transparent - Expressions in a purely functional language are referentially transparent, their value depends only on the referencing environment. Imperative programming – “programming with side effects”.

4 Side Effects Some Languages (Euclid and Turing) do not allow side effects in functions which return a value –If called repeatedly with the same set of arguments, will always give the same answer Sometimes side effects are desired: –Int rand (); Needs to have a side effect, or else the same random number every time it is called.

5 Evaluation of Operands and Side Effects int x = 0; int foo() { x += 5; return x; } int a = foo() + x + foo(); What is the value of a?

6 Control Flow (really)

7 Structured vs. Unstructured Control Flow Structured Programming – hot programming trend in the 1970’s Top down design Modularization of code Structured types Descriptive variable names Extensive commenting After Algol 60, most languages had: if…then…else, while loops Don’t need to use gotos!

8 Types of control flow Sequencing Selection Iteration Procedural abstraction Recursion Concurrency Nondeterminacy

9 Sequencing When one statement occurs before another in the program text the first statement executes before the second. Statement block groups multiple statements together Examples { } in C, C++, and Java begin/end in Algol, Pascal, and Modula Basic block (we’ll discuss later when we finish compilers) block where the only control flow allowed is sequencing

10 Selection

11 If Statements If condition then statement else statement –Nested if statements have a dangling else problem If … then if … then … else … Which one does the else map to? Algol 60 –Doesn’t allow “then if” Pascal –Else associates with closest unmatched then Perl –Has a separate elsif keyword (in addition to else and if) –“else if” will cause an error

12 Strict vs short-circuit evaluation of conditions strict –evaluate all operands before applying operators Pascal short-circuit –skip operand evaluation when possible –examples || and && in C++ and Java –always use short-circuit evaluation then if and or else in Ada –language supports both strict and short-circuit, programmer decides: use and, or for strict evaluation

13 Short circuiting C++ p = my_list; while (p && p->key != val) p = p->next; Pascal p := my_list; while (p <> nil) and (p^.key <> val) do p := p^.next

14 Short-circuiting Perl –open (FILE, “>filename.ext”) or die (“error!”); Can avoid: out-of-bounds/dereferencing NULL reference errors, division by zero Can lead to more efficient code Can be problematic when conditions have side effects.

15 Case Statements Alternative to nested if…then…else blocks j := … (* potentially complicated expression *) IF j = 1 THEN clause_A ELSEIF j IN 2,7 THEN clause_B ELSEIF j IN 3..5 THEN clause_C ELSEIF (j = 10) THEN clause_D ELSE clause_E END

16 Implementation of Case Statements If…then…elseCase (uses jump table)

17 Case vs Switch Switch is in C, C++, and Java –Unique syntax –Use break statements, otherwise statements fall through to the next case Case is used in most other languages –Can have ranges and lists –Some languages do not have default clauses Pascal

18 Origin of Case Statements Descended from the computed goto of Fortran goto (15, 100, 150, 200), J If J is 1, then it jumps to label 15 If J is 4, then it jumps to label 200 If J is not 1, 2, 3, or 4, then the statement does nothing

19 Iteration

20 Iteration More prevalent in imperative languages Takes the form of loops –Iteration of loops used for their side effects Modification of variables

21 Loops Two (2) kinds of iterative loops –Enumeration-Controlled Loop Executed once for every value in a finite set –Logically-Controlled Loop Execute until some boolean condition –Depends on value altered in the loop

22 Enumeration-Controlled Loop Early Fortran: do 10 i = 1, 50, : continue Equivalent? 10: i = 1... i = i + 2 if i <= 50 goto 10

23 Issues #1 Can the step size/bounds be: –Positive/negative ? –An expression ? –Of type Real ?

24 Issues #2 Changes to loop indices or bounds –Prohibited to varying degrees –Algol 68, Pascal, Ada, Fortran 77/90 Prohibit changes to the index within loop Evaluate bound once (1) before iteration

25 Changes to loop indices or bounds A statement is said to threaten an index variable if –Assigns to it –Passes it to a subroutine –Reads it from a file –Is a structure that contains a statement that threatens it

26 Issues #3 Test terminating condition before first iteration EX for i := first to last by step do … end r1 := first r2 := step r3 := last L1: if r1 > r3 goto L2 … r1 := r1 + r2 goto L1 L2: r1 := first r2 := step r3 := last L1: … r1 := r1 + r2 L2: if r1 <= r3 goto L1

27 Issues #4 Access to index outside loop –undefined Fortran IV, Pascal –most recent value Fortran 77, Algol 60 –index is a local variable of loop Algol 68, Ada

28 Issues #5 Jumps –Restrictions on entering loop from outside Algol 60 and Fortran 77 and most of their descendents prevent the use of gotos to jump into a loop. –“exit” or “continue” used for loop escape

29 Logically Controlled Loops while condition do statement Advantages of for loop over while loop –Compactness –Clarity –All code affecting flow control is localized in header

30 Logically-Controlled Loops Where to test termination condition? –Pre-test –Post-test –Mid-test

31 C’s for loop –Logically controlled Any enumeration-controlled loop can be written as a logically-controlled loop for (i = first; i <= last; i += step) { … } i = first; while (i <= last) { … i += step; }

32 C’s for loop Places additional responsibility on the programmer –Effect of overflow on testing of termination condition –Index and variable in termination condition can be changed By body of loop By subroutines the loop calls

33 Combination Loops Combination of enumeration and logically controlled loops Algol 60’s for loop For_stmt -> for id := for_list do stmt For_list -> enumerator (, enumerator )* Enumerator -> expr -> expr step expr until expr -> expr while condition

34 Algol 60’s for loop Examples: (all equivalent) for i := 1, 3, 7, 9 do… for i := 1 step 2 until 10 do … for i := 1, i + 2 while i < 10 do … Problems –Repeated evaluation of bounds –Hard to understand

35 Recursion

36 Recursive Computation Decompose problem into smaller problems by calling itself Base case- when the function does not call itself any longer; no base case, no return value Problem must always get smaller and approach the base case

37 Recursive Computation No side effects Requires no special syntax Can be implemented in most programming languages; need to permit functions to call themselves or other functions that call them in return. Some languages don’t permit recursion: Fortran 77

38 Tracing a Recursive Function (define sum (lambda(n) (if (= n 0) 0 (+ n (sum (- n 1))))))

39 Tracing a Recursive Function >(trace sum) # > >(sum 5) "CALLED" sum 5 "CALLED" sum 4 "CALLED" sum 3 "CALLED" sum 2 "CALLED" sum 1 "CALLED" sum 0 "RETURNED" sum 0 "RETURNED" sum 1 "RETURNED" sum 3 "RETURNED" sum 6 "RETURNED" sum 10 "RETURNED" sum 15 15

40 Embedded vs. Tail Recursion Analogy: You’ve been asked to measure the distance between Thornton and Little John’s Embedded: 1.Check to see if you’re there yet 2.If not, take a step, put a mark on a piece of paper to keep count, restart the problem 3.When you’re there, count up all the marks Tail: 1.Write down how many steps you’re taken so far as a running total 2.When you get to Little John’s, the answer is already there; no counting!

41 Which is Better? Tail. Additional computation never follows a recursive call; the return value is simply whatever the recursive call returns The compiler can reuse space belonging to the current iteration when it makes the recursive call Dynamically allocated stack space is unnecessary

42 Iteration vs. Recursion Any logically controlled iterative algorithm can be rewritten as a recursive algorithm and vice versa Iteration: repeated modification of variables (imperative languages) –Uses a repetition structure( for, while ) –Terminates when loop continuation condition fails Recursion: does not change variables (functional languages) –Uses a selection structure ( if, if / else, or switch / case ) –Terminates when a base case is recognized

43 Prolog Example factorial(N, F) := F is the integer Factorial of N factorial (0,1). factorial(N,F) :- N > 0, N1 is N - 1, factorial(N1,F1), F is N * F1. factorial(N, F) := F is the integer Factorial of N factorial(N,N,F,F). factorial(N,F) :- factorial(0,N,1,F). factorial(I,N,T,F):- I < N, I1 is I + 1, T1 is T * I1, factorial(I1,N,T1,F).

44 Tail Recursion Example int gcd(int a, int b){ if (a==b) return a; else if (a > b) return gcd(a-b, b); else return gcd(a, b-a) } int gcd(int a, int b){ start: if (a==b) return a; else if (a > b) { a = a-b; goto start; } else { b = b-a; goto start; }

45 Which is tail recursive? (define summation (lambda (f low high) (if (= low high) (f low) (+ (f low) (summation f (+ low 1) high))))) (define summation (lambda (f low high subtotal) (if (= low high) (+ subtotal (f low)) (summation f (+ low 1) high (+ subtotal (f low))))))

46 Normal vs. Applicative (define double (lambda (x) (+ x x))) (double (* 3 4)) >(double 12) >( )  24 (double (* 3 4))  (+ (* 3 4) (* 3 4))  (+ 12 (* 3 4))  ( )  24