CSE 3302 Programming Languages

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

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.
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.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CSE 452: Programming Languages Expressions and Control Flow.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
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 creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Structure of Programming Language
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
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.
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
Chapter 7: Expressions and Assignment Statements
Structure of Programming Language
CSE 3302 Programming Languages
Expressions and Assignment
Java Statements Java control structure is entrenched in statements:
CS2403 Programming Languages Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
CSE 3302 Programming Languages
Computing with C# and the .NET Framework
Chapter 8: Control Structures
Expressions & Assignments
Flow of Control.
11/10/2018.
Chap. 6 :: Control Flow Michael L. Scott.
Flow of Control.
null, true, and false are also reserved.
The University of Texas – Pan American
Control Flow.
CSE 3302 Programming Languages
Conditional Statements
Control statements Simple statements Basic structured statements
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.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
CSE 3302 Programming Languages
Statement-Level Control Structures
Homework Any Questions?.
Languages and Compilers (SProg og Oversættere)
CSE 3302 Programming Languages
2. Second Step for Learning C++ Programming • Data Type • Char • Float
CSE 3302 Programming Languages
Introduction to Programming
Chap 7. Advanced Control Statements in Java
Loops CGS3416 Spring 2019 Lecture 7.
CSC215 Lecture Control Flow.
Controlling Program Flow
CSE 3302 Programming Languages
Presentation transcript:

CSE 3302 Programming Languages Control I Expressions and Statements Based on slides by Chengkai Li Kelly Scott French University of Texas at Arlington Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Control Control is about execution: what, when, and in what order. Abstraction of control: Expression Statement Exception Handling Procedures and functions Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Expression vs. Statement Difference in purity Pure = no side effects Returns same output given the same input (vocab term: Idempotent) Expression no side effect return a value Statement side effect no return value Functional languages aim at achieving this pure form Not clear-cut in most languages Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington 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 2017 CSE3302 Programming Languages, UT-Arlington

Expression and 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 Let x = 1 What is the result of (x=x+1)+(x=x*2) ? Or void evil(){ int i = 0; i+= i++ + ++i; } (http://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior) Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Applicative Order Evaluation Also known as 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? * - 4 + 3 6 5 Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Order Matters C: int x=1; int f(void) { x=x+1; return x; } main(){ printf(“%d\n”, x + f()); return 0; 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. Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Predictable Side Effects 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 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington 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 2017 CSE3302 Programming Languages, UT-Arlington

Non-strict evaluation Evaluating an expression without necessarily evaluating all the subexpressions. short-circuit Boolean expression e1 or e2 if-expression, case-expression Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Short-Circuit Evaluation if (false and x) … if (true or x)… No need to evaluate x, no matter x is true or false if (false and foo(x)) ?? if (false and x=foo(y)) ?? What is it good for? if (i <=lastindex and a[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 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington 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? Key: these are expressions Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington case-expression ML: case color of ( variable to examine) red => “R’’ | ( possible choice) blue => “B’’ | ( possible choice) green => “G” | ( possible choice) _ => “AnyColor”; ( default choice) Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Loops While loop: – while (e) S; – Same as: The GOTO controversy loop: if (not e) goto exit; S; goto loop; exit: The GOTO controversy Java prohibits it FORTRAN 77 required it Do-while loop: do S while (e); Same as: { S; while (e) S; } Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Loops For-loop: A break statement can be used inside a loop For (e1; e2; e3) S; Same as: { e1; while (e2) { S; e3} e.g., for (int i=0; i<10; i++) a[i]++ Most languages enforce restrictions on for-loops A break statement can be used inside a loop Labeled break A continue statement can be used to jump to the next loop step Labeled continue Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Exception Handling An implicit control mechanism An exception is any unexpected or infrequent event Typically is an error event Raise (or throw) an exception: the point where the exception occurs Handle (or catch) an exception: code to be executed when a particular exception occurs Try-catch blocks in C++ struct Trouble {} t; try { code that may raise an exception using 'throw' } catch (Trouble t) { code that handles the Trouble exception } catch (...) { code that handles the rest of all exceptions } throw t; Propagating exceptions and call/stack unwinding Most languages provide predefined exceptions Structured GOTO Lecture 9 – Control I, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Normal order evaluation Also known as 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)) = square(4) = 16 Normal order evaluation: square(double(2)) = double(2)*double(2) = (2+2)*(2*2) Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington What is it good for? You can define if-then-else, case, etc as functions (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 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington 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 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Unhygienic Macros Macro (http://stackoverflow.com/q/321143/135078) #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); } Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Statements If-statements, case-(switch-)statements, loops Lecture 9 – Control I, Spring 2017 CSE3302 Programming Languages, UT-Arlington