CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Control I Expressions and Statements Lecture 9 – Control I, Spring 20081 CSE3302 Programming.

Slides:



Advertisements
Similar presentations
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Advertisements

Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
P ROGRAMMING L ANGUAGES : C ONTROL 1. S LIDES R EFERENCES Kenneth C. Louden, Control I: Expressions and Statements: Principles and Practice. 2.
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Changing Control.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Functional Programming Language (Introduction and Scheme) Lecture 17 – Functional Programming, Spring.
Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
C expressions (Reek, Ch. 5) 1CS 3090: Safety Critical Programming in C.
Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand.
CSE 452: Programming Languages Expressions and Control Flow.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
ISBN Lecture 07 Expressions and Assignment Statements.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Structure of Programming Language
OperatorstMyn1 Operators The sequence in which different operators in an expression are executed is determined by the precedence of the operators. Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
CSC 204 Programming I Loop I The while statement.
CS 331, Principles of Programming Languages Chapter 2.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Exam Delay Exam II will be held during the first fifty (50) minutes of class on 13 November 2003.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS 331, Principles of Programming Languages Chapter 2.
Semantics (1).
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE 3302 Programming Languages
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Programming Principles Operators and Expressions.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Lazy Evaluation Computer Science 3 Gerb Objective: Understand lazy evaluation in Java.
© Kenneth C. Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 7: Expressions and Assignment Statements
CSE 3302 Programming Languages
Operators And Expressions
Expressions and Assignment
CS2403 Programming Languages Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Chap. 6 :: Control Flow Michael L. Scott.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
The University of Texas – Pan American
Chap. 6 :: Control Flow Michael L. Scott.
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE 3302 Programming Languages
CSE 3302 Programming Languages
OPERATORS in C Programming
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
OPERATORS in C Programming
CSE 3302 Programming Languages
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Control I Expressions and Statements Lecture 9 – Control I, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Control Control: what gets executed, when, and in what order. Abstraction of control: – Expression – Statement – Exception Handling – Procedures and functions Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Expression vs. Statement In pure (mathematical) form: – Expression: no side effect return a value – Statement: side effect no return value Functional languages aim at achieving this pure form No clear-cut in most languages Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Expression Constructed recursively: – Basic expression (literal, identifiers) – Operators, functions, special symbols Number of operands: – unary, binary, ternary operators Operator, function: equivalent concepts – (3+4)*5(infix notation) – mul( add(3,4), 5) “*”(“+”(3,4),5) (Ada, prefix notation) (* (+ 3 4) 5)(LISP, prefix notation) Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Postfix notation PostScript: %!PS /Courier findfont 20 scalefont setfont moveto (Hello world!) show showpage Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Expression and Side Effects Side Effects: – changes to memory, input/output – Side effects can be undesirable – But a program without side effects does nothing! Expression: – No side effect: Order of evaluating subexpressions doesn’t matter (mathematical forms) – Side effect: Order matters Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Applicative Order Evaluation (Strict Evaluation) Evaluate the operands first, then apply operators (bottom-up evaluation) (subexpressions evaluated, no matter whether they are needed) But is 3+4 or 5-6 evaluated first? Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, *

Order Matters C: int x=1; int f(void) { x=x+1; return x; } main(){ printf(“%d\n”, x + f()); return 0; } Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Java: class example { static int x = 1; public static int f() { x = x+1; return x; } public static void main(String[] args) { System.out.println(x+f()); } } 4 3 Many languages don’t specify the order, including C, java. – C: usually right-to-left – Java: always left-to-right, but not suggested to rely on that.

Expected Side Effect Assignment (expression, not statement) x = (y = z) (right-associative operator) x++, ++x int x=1; int f(void) { return x++; } main(){ printf(“%d\n”, x + f()); return 0; } Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Why?

Sequence Operator (expr1, expr2, …, exprn) – Left to right (this is indeed specified in C) – The return value is exprn x=1; y=2; x = (x=x+1, y++, x+y); printf(“%d\n”,x); Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Non-strict evaluation Evaluating an expression without necessarily evaluating all the subexpressions. short-circuit Boolean expression if-expression, case-expression Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Short-Circuit Evaluation if (false and x) … if (true or x)… – No need to evaluate x, no matter x is true or false What is it good for? – if (i =x)… – if (p != NULL and p->next==q)… Ada: allow both short-circuit and non short- circuit. – if (x /= 0) and then (y/x > 2) then... – if (x /= 0) and (y/x > 2) then... ? – if (ptr = null) or else (ptr.x = 0) then... – if (ptr = null) or (ptr.x = 0) then... ? Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

if-expression if (test-exp, then-exp, else-exp) ternary operator – test-exp is always evaluated first – Either then-exp or else-exp are evaluated, not both – if e1 then e2 else e3 (ML) – e1 ? e2 : e3 (C) Different from if-statemnt? Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

case-expression ML: case color of red => “R’’ | blue => “B’’ | green => “G” | _ => “AnyColor”; Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Normal order evaluation (lazy evaluation) When there is no side-effect: Normal order evaluation (Expressions evaluated in mathematical form) – Operation evaluated before the operands are evaluated; – Operands evaluated only when necessary. int double (int x) { return x+x; } int square (int x) { return x*x; } Applicative order evaluation : square(double(2)) = … Normal order evaluation : square(double(2)) = … Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

What is it good for? (p!=NULL) ? p->next : NULL int if_exp(bool x, int y, int z) { if (x) return y; else return z; } if_exp(p!=NULL, p->next, NULL); With side effect, it may hurt you: int get_int(void) { int x; scanf(“%d” &x); return x; } Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Examples Call by Name (Algol60) Macro #define swap(a, b) {int t; t = a; a = b; b = t;} – What are the problems here? Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Unhygienic Macros Call by Name (Algol60) Macro Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, #define swap(a, b) {int t; t = a; a = b; b = t;} main (){ int t=2; int s=5; swap(s,t); } main (){ int t=2; int s=5; {int t; t = s; s = t; t = t;} } #define DOUBLE(x) {x+x;} main() { int a; a = DOUBLE(get_int()); printf("a=%d\n", a); } main() { int a; a = get_int()+get_int(); printf("a=%d\n", a); }

Statements If-statements, case-(switch-)statements, loops Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,