Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
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.
Intermediate Code Generation
Statement-Level Control Structures
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:
1 Compiler Construction Intermediate Code Generation.
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.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
COEN Expressions and Assignment
Compiler Construction
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.
Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ISBN Chapter 8 Statement-Level Control Structures.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Chapter 8 . Sequence Control
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
ISBN Chapter 7 Expressions and Assignment Statements.
Compiler Construction
Compiler Chapter# 5 Intermediate code generation.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Flow of Control Part 1: Selection
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
ISBN Chapter 8 Statement-Level Control Structures.
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
Exam Delay Exam II will be held during the first fifty (50) minutes of class on 13 November 2003.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Introduction to Java Java Translation Program Structure
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Decision Making and Branching (cont.)
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
CS 36 – February 2 Control structures –Sequence √ –Loops √ –Selection If-statement Case-statement Finding loops.
Machine-Level Programming II Control Flow Sept. 10, 1998 Topics Control Flow –Varieties of Loops –Switch Statements class06.ppt “The course that.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
© Kenneth C. Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Programmazione I a.a. 2017/2018.
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
Chapter 6 Intermediate-Code Generation
Control Structures In Text: Chapter 8.
Compiler Construction
Chap 7. Advanced Control Statements in Java
Chapter 8: Statement Level Control Structures
Compiler Construction
CSE 3302 Programming Languages
Presentation transcript:

Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution paths through code: what gets executed, when, and in what order.

Chapter 7Louden, Programming Languages2 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. If expressions could have arbitrary evaluation order, programs would become non-deterministic: any of a number of different outcomes might be possible.

Chapter 7Louden, Programming Languages3 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! Why?

Chapter 7Louden, Programming Languages4 Strictness An evaluation order for expressions is strict (eager) if all subexpressions of an expression are evaluated, whether or not they are needed to determine the value of the result, non-strict (lazy) otherwise. Arithmetic is almost always strict. Every language has at least a few non-strict expressions (?:, &&, ||). A form of strict evaluation called applicative-order is common: "bottom-up" or "inside-out". Expressions are evaluated in the order they are encountered in evaluating the expression tree.

Applicative Order Consider: (a+b)/(c+d)+((e-f)-g)/(h+j) – which operation is evaluated first? Still leaves open whether left-to-right or not. Chapter 7Louden, Programming Languages5

Chapter 7Louden, Programming Languages6 Short Circuit Evaluation Suppose Java did not use short-circuit evaluation Problem: table look-up i = 1; while(i<=length)&&(LIST[i]!= value) i++;

Chapter 7Louden, Programming Languages7 Short Circuit Evaluation C, C++, and Java: use short-circuit evaluation for the usual Boolean operators ( && and || ), but also provide bitwise Boolean operators that are not short circuit ( & and | ) Ada: programmer can specify either (short-circuit is specified with and then and or else) Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) || (b++ / 3)

Chapter 7Louden, Programming Languages8 Function calls Obey evaluation rules like other expressions. With side effects, order makes a difference.

Chapter 7Louden, Programming Languages9 Case Statements Rules cases can be constants, constant expressions, constant ranges no overlapping cases error if unspecified case occurs (or may decide to do nothing if unspecfied case) Implemented via jump table: vector stored sequentially in memory - each of whose components is an unconditional jump Need one location in jump table for every value between range of possible values

Chapter 7Louden, Programming Languages10 Switch Statements Implementation Options –Series of conditionals Good if few cases Slow if many –Jump Table Lookup branch target Avoids conditionals Possible when cases are small integer constants –Flexible Picks one based on case structure typedef enum {ADD, MULT, MINUS, DIV, MOD, BAD} op_type; char unparse_symbol(op_type op) { switch (op) { case ADD : return '+'; case MULT: return '*'; case MINUS: return '-'; case DIV: return '/'; case MOD: return '%'; case BAD: return '?'; }

Chapter 7Louden, Programming Languages11 Jump Table Structure Code Block 0 Targ0: Code Block 1 Targ1: Code Block 2 Targ2: Code Block n–1 Targn-1: Targ0 Targ1 Targ2 Targn-1 jtab: target = JTab[op]; goto *target; switch(op) { case 0: Block 0 case 1: Block 1 case n-1: Block n–1 } Switch Form Code Generated Jump Table Jump Targets

Chapter 7Louden, Programming Languages12 A jump table is an array of code addresses: –Tbl[ i ] is the address of the code to execute if the expression evaluates to i. –if the set of case labels have “ holes ”, the correspond jump table entries point to the default case. Bounds checks: –Before indexing into a jump table, we must check that the expression value is within the proper bounds (if not, jump to the default case). –The check lower_bound  exp_value  upper bound can be implemented using a single unsigned comparison.

Chapter 7Louden, Programming Languages13 Jump Table Space Costs jump tables best for large no. of case labels (  8) may take a large amount of space if the labels are not well- clustered. A jump table with max. and min. case labels c max and c min needs  c max – c min entries. This can be wasteful if the entries aren’t “dense enough”, e.g.: switch (x) { case 1: … case 1000: … case : … } Define the density of a set of case labels as density = number of case labels/(c max – c min ) Compilers will not generate a jump table if density below some threshold (typically, 0.5).

Chapter 7Louden, Programming Languages14 Use of Switch Statements if number of case labels is small (  ~ 8), use linear or binary search. –use number of case labels to decide between the two. if density  threshold (~ 0.5) : generate a jump table; else : divide the set of case labels into sub-ranges such that each sub-range has density  threshold; generate code to use binary search to choose amongst the sub-ranges; handle each sub-range recursively.

Chapter 7Louden, Programming Languages15 Guarded Commands Selection: if -> [] ->... [] -> fi Semantics: when this construct is reached, –Evaluate all boolean expressions –If more than one are true, choose one nondeterministically –If none are true, it is a runtime error

Chapter 7Louden, Programming Languages16 Guarded Commands Idea: if the order of evaluation is not important, the program should not specify one In Haskell, first one that matches is used.

Chapter 7Louden, Programming Languages17 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.