Adapted from Scott, 20061 Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.

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
Statement-Level Control Structures
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.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
COEN Expressions and Assignment
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
Chapter 8 . Sequence Control
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
1 Control Flow Aaron Bloomfield CS 415 Fall 2005.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
Structure of Programming Language
C H A P T E R S E V E N Expressions and Assignment Statements.
Chapter 1 - Introduction
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
1 Introduction Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
1 Control Flow: Expressions, Sequencing & Selection (Section ) A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
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.
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.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement.
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.
Expressions and Assignment Statements
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
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.
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.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
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.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Lecture 18 Control Flow.
Def: A control structure is a control statement and
Expressions and Assignment
CS 326 Programming Languages, Concepts and Implementation
Lecture 14: Iteration and Recursion (Section 6.5 – 6.6)
Chapter 6:: Control Flow
Chapter 8: Control Structures
Chap. 6 :: Control Flow Michael L. Scott.
Control Flow.
Control Flow Chapter 6.
Control statements Simple statements Basic structured statements
Chap. 6 :: Control Flow Michael L. Scott.
Control Structures Programs utilize 4 basic control structures
Statement-Level Control Structures
Chapter 6:: Control Flow
Chapter8: Statement-Level Control Structures April 9, 2019
Chapter 6:: Control Flow
Controlling Program Flow
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
PZ07B - 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
CSE 3302 Programming Languages
Presentation transcript:

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

Adapted from Scott, Control Flow Basic paradigms for control flow: –Sequencing –Selection –Iteration –Subroutines, recursion (and related control abstractions, e.g. iterators) –Nondeterminacy – ordering unspecified –Concurrency – parallel or interleaved execution

Adapted from Scott, Expression Evaluation Infix, prefix operators Precedence, associativity (see Figure 6.1) –C has 15 levels - too many to remember –Pascal has 3 levels - too few for good semantics if A < B and C < D then…  A < (B and C) < D –Fortran has 8 –Ada has 6 –Lesson: The programmer working in several languages will use parentheses! Usually: mult/div>add/sub>relational>logical

Adapted from Scott, Expression Evaluation

Adapted from Scott, Expression Evaluation Short-circuiting –Consider (a < b) && (b < c) If a >= b there is no point evaluating whether b < c because (a < b) && (b < c) is automatically false –Other similar situations if( b != 0 && a/b == c )... if( *p && p->foo )... if( f || messy() )...

Adapted from Scott, Expression Evaluation Variables as values vs. variables as references –value-oriented languages A variable is a named container for a value (r-value) C, Pascal, Ada –reference-oriented languages Every variable is an l-value (denotes location) most functional languages (Lisp, Scheme, ML) Clu, Smalltalk –Java deliberately in-between built-in types are values user-defined types are objects - references

Adapted from Scott, Expression Evaluation Expression-oriented vs. statement-oriented languages –expression-oriented: functional languages (Lisp, Scheme, ML) Algol-68 –statement-oriented: most imperative languages –C kinda halfway in-between (distinguishes) allows expression to appear instead of statement

Adapted from Scott, Expression Evaluation Orthogonality –Features that can be used in any combination –All combinations of features make sense –Meaning of a feature is consistent, regardless of the context in which it is used How about this? if (if b != 0 then a/b == c else false) then... if (if f then true else messy()) then...

Adapted from Scott, Expression Evaluation Assignment –statement (or expression) executed for its side effect –assignment operators (+=, -=, etc) handy avoid redundant work (or need for optimization) perform side effects exactly once A[index(f-4*j)] = A[index(f-4*j)] + 1  A[index(f-4*j)] += 1 –C --, ++ Prefix vs. postfix form Array[k++] vs. Array[++k]

Adapted from Scott, Expression Evaluation Assignment (cont.) –Multiway assignment (e.g., Perl, Python, Ruby) a, b = c, d tuple = tuple –Multiple function return values (e.g., Matlab, G2) a, b, c = funct( x, y );

Adapted from Scott, Expression Evaluation Side Effects –often discussed in the context of functions –a side effect is some permanent state change caused by execution of function some noticeable effect of call other than return value in a more general sense, assignment statements provide the ultimate example of side effects –they change the value of a variable

Adapted from Scott, Expression Evaluation Side effects are fundamental to the whole VonNeuman computing approach In (pure) functional and logic languages, there are no such changes –These languages are called SINGLE- ASSIGNMENT languages

Adapted from Scott, Sequencing –specifies a linear ordering on statements one statement follows another –very imperative, Von-Neuman Sequencing

Adapted from Scott, Selection –sequential if statements if... then... else if... then... elsif... else (cond (C1) (E1) (C2) (E2)... (Cn) (En) (T) (Et) ) Selection

Adapted from Scott, –Fortran computed gotos goto( 15, 20, 30, 100 ) n –Case/switch statements Switch( expression ) { Case 1: 1 st arm of code Case 2: 2 nd arm of code Default: default arm of code } Default clause? List or range of values for each case? Integral values or general values for expression? "Fall through" semantics or not? What if expression evaluates to missing value? Selection

Adapted from Scott, jump code generation for selection and logically-controlled loops no point in computing a Boolean value into a register, then testing it instead of: –passing register containing Boolean out –pass inherited attributes INTO expression indicating where to jump to if true, and where to jump to if false Example (follows): if ((A > B) and (C > D)) or (E <> F)) then then_clause else else_clause Selection

17 Code generated w/o short-circuiting (Pascal) if ((A > B) and (C > D)) or (E <> F)) r1 := A-- load r2 := B r1 := r1 > r2 r2 := C r3 := D r2 := r2 > r3 r1 := r1 & r2 r2 := E r3 := F r2 := r2 $<>$ r3 r1 := r1 $|$ r2 if r1 = 0 goto L2 L1: then_clause-- label not actually used goto L3 L2: else_clause L3: Selection

Adapted from Scott, Code generated w/ short-circuiting (C) if ((A > B) and (C > D)) or (E <> F)) r1 := A r2 := B if r1 r2 goto L1 L4: r1 := E r2 := F if r1 = r2 goto L2 L1: then_clause goto L3 L2: else_clause L3: Selection

Adapted from Scott, Enumeration-controlled do/for loops –Pascal or Fortran-style do/for loops do 20 i = continue scope of control variable? –Local to for loop in C, C++, Java; global in Fortran changes to loop variable or bounds within loop? –Prohibited by Pascal, Fortran 77; permitted by Fortran IV value of i after the loop? –Undefined by Pascal, Fortran IV; most recent for Fortran 77 always executes at least once? –Yes in Fortran, Pascal; no in C, C++, Java Iteration

Adapted from Scott, Iteration The goto controversy –assertion: gotos are needed almost exclusively to cope with 1.lack of one-and-a-half loops –mid-loop exit –multi-level return from loop 2.early return from procedure 3.error returns –Scott: "In many years of programming, I can't remember using one for any other purpose." Usually there is a structured alternative in modern languages 1.break/continue 2.multiple returns 3.exception handling mechanisms

Adapted from Scott, Recursion –equally powerful to iteration –mechanical transformations back and forth –often more intuitive (sometimes less) –naïve implementation less efficient no special syntax required fundamental to functional languages like Scheme

Adapted from Scott, Recursion Tail recursion –No computation follows recursive call /* assume a, b > 0 */ 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); }