© Kenneth C. Louden, 20031 Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
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.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
P ROGRAMMING L ANGUAGES : C ONTROL 1. S LIDES R EFERENCES Kenneth C. Louden, Control I: Expressions and Statements: Principles and Practice. 2.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Scope Chapter Ten Modern Programming Languages.
Chapter 12 - Logic Programming
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Introduction to Methods
Prime Numbers and Prime Factorization
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Computer Science Selection Structures.
Chapter 1 - Introduction
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Chapter 8 - Control II: Procedures and Environments
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Java Gotcha's By Rick Mercer with help from the book:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Control I Expressions and Statements Lecture 9 – Control I, Spring CSE3302 Programming.
Information and Computer Sciences University of Hawaii, Manoa
The need for Programming Languages
Chapter 7 User-Defined Methods.
Building Java Programs
Introduction to Computer Science / Procedural – 67130
Primitive Data, Variables, Loops (Maybe)
CS4450: Principles of Programming Languages
Control Flow Chapter 6.
Building Java Programs
Functions Pass By Value Pass by Reference
6.001 SICP Further Variations on a Scheme
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Methods/Functions.
CSE 3302 Programming Languages
Presentation transcript:

© Kenneth C. Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden

Chapter 7K. Louden, Programming Languages2Introduction "Control" is the general study of the semantics of execution paths through code: what gets executed, when, and in what order. Most important control issue in modern languages: procedure/function/method call and return, studied in Chapter 8. Here we study more localized control issues in expressions and statements, and: Exception handling, which involves non- local control but has a local component too.

Chapter 7K. Louden, Programming Languages3 Expressions In their purest form, expressions do not involve control issues: subexpressions can be evaluated in arbitrary order, and the order does not affect the result. Functional programming tries to achieve this goal for whole programs. Of course, there must always be a few expressions that can modify the execution/evaluation process: if-then-else expressions, short-circuit boolean operators, case/switch expressions. If these could have arbitrary evaluation order, programs would become non-deterministic: any of a number of different outcomes might be possible. Usually this is not desirable, but see later.

Chapter 7K. Louden, Programming Languages4 Side Effects A side effect is any observable change to memory, input or output. A program without any side effect is useless. Side effects expose evaluation order: class Order { static int x = 1; public static int getX() { return x++; } public static void main(String[] args) { System.out.println( x+getX() ); } } This prints 2, but the corresponding C program will usually print 3! Referential transparency limits side effects, so this can't happen for r. t. expressions.

Chapter 7K. Louden, Programming Languages5 Strictness An evaluation order for expressions is strict if all subexpressions of an expression are evaluated, whether or not they are needed to determine the value of the result, non-strict otherwise. Arithmetic is almost always strict. Every language has at least a few non-strict expressions (?:, &&, || in Java). Some languages use a form of non-strictness called normal-order evaluation: no expression is ever evaluated until it is needed (Haskell). Also called delayed evaluation. A form of strict evaluation called applicative-order is more common: "bottom-up" or "inside-out". Still leaves open whether left-to-right or not.

Chapter 7K. Louden, Programming Languages6 Function calls Obey evaluation rules like other expressions. Applicative order: evaluate all arguments (left to right?), then call the procedure. Normal order: pass in unevaluated representations of the arguments. Only evaluate when needed. With side effects, order makes a difference. Representation of argument value also makes a difference (value or reference?).

Chapter 7K. Louden, Programming Languages7 Examples C and Scheme: no explicit order required for subexpressions or arguments to calls. Java always says left to right, but warns against using that knowledge. Case/switch/cond expressions imply a top-down order: (define (f) (cond (#t 1) (#t 2))) Theoretically, this could return either 1 or 2 (non- determinism—the "guarded if" of text). Java and C outlaw this: no duplicate cases.

Chapter 7K. Louden, Programming Languages8 Sequencing and Statements A sequence of expressions makes no sense without side effects. Thus, a referentially transparent program should not need sequences. Both ML and Scheme have sequences: (e1;e2;…) [ML] and (begin e1 e2 …) [Scheme]. What about a let expression? Is there an implied sequence? ( let val x = e1 in e2 end; ) Applicative order would say yes: e1 is an argument to a call: (fn x => e2) e1. Normal order would say no: only evaluate e1 if the value of x is actually needed in e2. Statements by definition imply sequencing, since there is no computed value.

Chapter 7K. Louden, Programming Languages9Statements Can be viewed as expressions with no value. Java, C have very few: if, while, do, for, switch, plus "expression statements." Scheme: valueless expressions also exist: define, set! (some versions give these values). ML: "valueless" expressions have value (). What about val and fun? Declarations may be neither expressions nor statements.

Chapter 7K. Louden, Programming Languages10 Summary Every language has three major program components: expressions, statements, and declarations. Expressions are executed for their values (but may have side effects), and may or may not be sequenced. Statements are executed solely for their side effects, and they must be sequenced. Declarations define names; they can also give values to those names. They may or may not be viewed by a language as expressions or statements.