Http://flic.kr/p/aGhubM Control Flow.

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
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.
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
Procedures and Control Flow CS351 – Programming Paradigms.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
1 Control Flow Aaron Bloomfield CS 415 Fall 2005.
Language Evaluation Criteria
Imperative Programming
1 Control Flow: Expressions, Sequencing & Selection (Section ) A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
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.
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.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
LECTURE 18 Control Flow. CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
A Single Intermediate Language That Supports Multiple Implemtntation of Exceptions Delvin Defoe Washington University in Saint Louis Department of Computer.
Lecture 18 Control Flow.
CSE341: Programming Languages Lecture 11 Type Inference
Why study programming languages?
Def: A control structure is a control statement and
Principle of Programming Lanugages 2: Imperative languages
Expressions and Assignment
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
Lecture 14: Iteration and Recursion (Section 6.5 – 6.6)
Chapter 6:: Control Flow
Imperative languages Most familiar: computation modifies store
CS 363 – Chapter 6 Control Within statements Statements in a function
Chap. 6 :: Control Flow Michael L. Scott.
Chapter 9 :: Subroutines and Control Abstraction
Final Review In Text: Chapters 1-3, 5-11,
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Control Flow Chapter 6.
Variables, Environments and Closures
Conditional Statements
Final Review In Text: Chapters 1-11,
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Lecture 11 Type Inference
Midterm Review In Text: Chapters 1-3, 5-9, 15.
Midterm Review In Text: Chapters 1-3, 5-10, 15.
Chap. 6 :: Control Flow Michael L. Scott.
Iteration Implemented through loop constructs (e.g., in C++)
Final Review In Text: Chapters 1-3, 5-12,
CSE341: Programming Languages Lecture 11 Type Inference
Statement-Level Control Structures
Chapter 6:: Control Flow
CSE 3302 Programming Languages
Final Review In Text: Chapters 1-3, 5-16.
Overview of Programming Paradigms
Chapter8: Statement-Level Control Structures April 9, 2019
CSE341: Programming Languages Lecture 11 Type Inference
Final Review In Text: Chapters 1-3, 5-16.
Midterm Review In Text: Chapters 1-3, 5-9, 15.
Midterm Review In Text: Chapters 1-3, 5-11, 15.
CSE341: Programming Languages Lecture 11 Type Inference
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
CSE341: Programming Languages Lecture 11 Type Inference
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE 3302 Programming Languages
Presentation transcript:

http://flic.kr/p/aGhubM Control Flow

Categories of Control Mechanisms Sequencing (e.g., expression evaluation) Selection (e.g., if/else) Iteration (e.g., loops) Procedural abstractions (e.g., subroutines) Recursion (e.g., recursive function calls) Concurrency (e.g., threads) Exception handling and speculation (e.g., try/catch) Nondeterminacy

Sequencing Design Issue: Operator Syntax Cambridge Polish Notation (Used in Lisp, ML, and R)

Sequencing Design Issue: Variables and Assignment l-values: Locations r-values: Values

Two Models of Variables Value Model Reference Model a = 4; b = 2; c = 2; For what types of variables does Java use each model? Primitive types use value, and Object types use reference

Java References versus C/C++ Pointers MyObject objA = new MyObject(); MyObject objB = null; objB = objA; // A and B refer to same val objB.myMethod(); C++: MyObject* objA = new MyObject; MyObject* objB = null; objB = objA; // A and B refer to same val (*objB).myMethod(); // Long form objB->myMethod(); // Sugary form In C++, pointers can reference any type and must be explicitly dereferenced to get to the referent value

C++ Value/Pointer Demo

Design Issue: Orthogonality Extent to which lang. features can be used in any combination Algol 68 Example: a gets d or e a gets last value This block returns 5 Algol has no separate notion of statement and expression

Orthogonality-Related Pitfall C++ allows assignments within expressions, which leads to this problem This bug can be very difficult to spot!

Design Option: Multiway Assignments Supported in Clu, ML, Perl, Python, and Ruby is like What do you suppose this does? Swap! (r-values must be gotten before assignment) What do you suppose is happening here? Function returns tuple

Design Issue: Undefined Order of Evaluation Which of the underlined expression gets evaluated first? Does it matter? In some languages, order is undefined to allow for compiler optimizations (In Java and C# it’s l-to-r, though) Order matters if there are operations with side effects

Design Issue: Short-Circuit Evaluation What happens in these examples if all parts of expression are evaluated? Expensive function always evaluated even when pointless Possible segmentation fault! (null pointer dereference) Short-circuit evaluation helps by only evaluating subexpressions as needed

Unstructured Control Flow with goto Jump to label “10” Activity: Write a loop using gotos How does goto compare to while loops?

What makes this confusing? Call stack must be repaired (typically by “unwinding”) 2 1 3

Famous Article: “Go To Statement Considered Harmful” by Edsger W Famous Article: “Go To Statement Considered Harmful” by Edsger W. Dijkstra (1968) Gist of argument: Goto makes it hard to reason about potential program behavior This article and others lead to structured programming (if/else, while, etc. statements)

Design Issue: Iteration versus Recursion Let’s make sure you remember how to think recursively

Code a recursive implementation of this: Solution:

How could you implement this using iteration (i.e., loops)? Simulate the call stack

In general do recursive or iterative solutions perform better? Iterative because each recursive call creates another stack frame However, there are special cases, where recursion does well

Tail Recursion Additional computation does not follow recursive call Easy for compiler to optimize:

What’s next? Homework 3 due in one week

LEFTOVERS