CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Similar documents
Expressions & Assignment Statements

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7. Expressions and Assignment Statements

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Expressions and Assignment Statements

Software II: Principles of Programming Languages. Why Expressions?

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7. Expressions and Assignment Statements

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

Expression Evaluation and Control Flow. Outline

Chapter 7 Expressions and Assignment statements

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Chapter7 Expression and Assignment Statement. Introduction

Concepts of Programming Languages

Expressions and Assignment

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Chapter 8. Statement-Level Control Structures

Chapter 8 Statement-Level Control Structures

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Statement level control structures

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures

Motivation for typed languages

Chapter 8. Statement-Level Control Structures ISBN

LECTURE 17. Expressions and Assignment

Review of the C Programming Language

Ch. 7: Control Structures

Flow Control. CSC215 Lecture

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

G Programming Languages - Fall 2012

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

ECE 122 Engineering Problem Solving with Java

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

Ruby: Introduction, Basics

LECTURE 18. Control Flow

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

C++ Programming: From Problem Analysis to Program Design, Third Edition

Review of the C Programming Language for Principles of Operating Systems

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 7. Additional Control Structures

Introduction to Programming Using Java (98-388)

Programming for Engineers Iteration

Chapter 8 Statement-Level Control Structure

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

MIDTERM EXAMINATION - CS130 - Spring 2003

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Operators in C. Staff Incharge: S.Sasirekha

Java enum, casts, and others (Select portions of Chapters 4 & 5)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Principles of Programming Languages. Lecture Outline

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Chapter 5 Names, Binding, Type Checking and Scopes

Operators and Expressions

Unit 3. Operators. School of Science and Technology INTRODUCTION

Full file at

Operators and Expressions:

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

5. Control Statements

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

More Programming Constructs -- Introduction

Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

Java Primer 1: Types, Classes and Operators

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

1.3b Type Conversion

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

CSC 533: Organization of Programming Languages. Spring 2005

Operators & Expressions

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Ruby: Introduction, Basics

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Continued from previous lecture

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Types. What is a type?

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 3: Operators, Expressions and Type Conversion

The PCAT Programming Language Reference Manual

LECTURE 3 C++ Basics Part 2

Type Conversion. and. Statements

Arithmetic Operators. Portability: Printing Numbers

The SPL Programming Language Reference Manual

CSE 431S Type Checking. Washington University Spring 2013

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Expressions and Statements. Department of CSE, MIT, Manipal

Flow of Control Execution Sequence

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Transcription:

CSE 452: Programming Languages Expressions and Control Flow Outline of Today s Lecture Expressions and Assignment Statements Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-circuit evaluation Assignment Statements Mixed mode assignment 2 Expressions Expressions are the fundamental means of specifying computations in a programming language Types: Arithmetic Relational/Boolean 3 1

Arithmetic Expressions Consist of operators, operands, parentheses, and function calls Design issues for arithmetic expressions: What are the operator precedence rules? What are the operator associativity rules? What is the order of operand evaluation? Are there restrictions on operand evaluation side effects? Does the language allow user-defined operator overloading? What mode mixing is allowed in expressions? 4 Arithmetic Expressions Types of operators A unary operator has one operand: - x A binary operator has two operands: x + y Infix: operator appears between two operands Prefix: operator precede their operands A ternary operator has three operands: (x > 10)? 0 : 1 Evaluation Order Operator evaluation order Operand evaluation order 5 Operator Evaluation Order Four rules to specify order of evaluation for operators 1. Operator precedence rules Define the order in which the operators of different precedence levels are evaluated (e.g., + vs * ) 2. Operator associativity rules Define the order in which adjacent operators with the same precedence level are evaluated (e.g., left/right associative) 3. Parentheses Precedence and associativityrules can be overriden with parentheses 4. Conditional Expressions (?: operator in C/C++/Perl) Equivalent to if-then-else statement 6 2

Operand Evaluation Order When do we evaluate operand? Variables are evaluated by fetching their values from memory Constants Sometimes, constants are evaluated by fetching its value from memory; At other times, it is part of the machine language instruction Parenthesized expressions If operand is a parenthesized expression, all operators it contains must be evaluated before its value can be used as an operand Function calls Must be evaluated before its value can be used as an operand 7 Operand Evaluation Order Functional Side Effects When function changes one of its params/global variable a + fun(a) If fun does not have the side effect of changing a, then the order evaluation of the two operands, a and fun(a), does not matter If fun does have the side effect of changing a, order of evaluation matters Two Possible Solutions : Disallow functional side effects in the language definition No two-way parameters in functions No non-local references in functions Advantage: it works! Disadvantage: No more flexibility Write language definition to demand fixed operand evaluation order Disadvantage: limits some compiler optimizations 8 Overloaded Operators Multiple use of an operator E.g., use + for integer addition and floating-point addition Some drawbacks of operator overloading May affect readability E.g., the ampersand (&) operator in C is used to specify bitwise logical AND operation Address of a variable May affect reliability Program does not behave the way we want int x, y; float z; z = x / y Problem can be avoided by introducing new symbols (e.g., Pascal s div for integer division and / for floating point division) C++ and Ada allow user-defined overloaded operators Potential problems: Users can define nonsense operations Readability may suffer, even when the operators make sense E.g., use + to mean multiplication 9 3

Type Conversions Narrowing conversion converts the value of a type to another type that cannot store all the values of the original type e.g., convert double to float Widening conversion converts the value to a type that include at least approximations to all of the values of the original type e.g., convert integers to float 10 Type Conversions Implict/Explicit type conversion Coercion is an implicit type conversion Useful for mixed-mode expression, which contains operands of different types Disadvantage: decreases type error detection ability of compilers In most languages, all numeric types are coerced in expressions, using widening conversions In Ada, there are virtually no coercions in expressions Explicit Type Conversions Often called type casts Ada: Float(Index) -- Index is originally an integer type Java: (int) speed /* speed is float type */ 11 Relational Expressions Relational operator is an operator that compares the values of its two operands Relational expression has two operands and one relational operator Operator symbols used vary somewhat among languages Ada: /= (not equal operator) C-based language:!= Fortran.NE. or <> Javascript and PHP has two additional relational operators: === and!== similar to == and!=, except it is used to prevent coercion E.g., 7 == 7 is true in Javascript but 7 ===7 is false 12 4

Boolean Expressions Consist of Boolean variables, Boolean constants, relational expressions, and Boolean operators Boolean Operators: FORTRAN77 FORTRAN90 C Ada.AND..OR..NOT. and or not &&! and or not C has no Boolean type it uses int type with 0 for false and nonzero for true a > b > c is a legal expression 13 Short Circuit Evaluation Short-circuit evaluation of an expression result is determined without evaluating all operands & operators int a = -1, b = 4; if ((a > 0) && (b < 10)) { } Problem: suppose Java did not use short-circuit evaluation index = 1; while (index <= length) && (LIST[index]!= value) index++; C, C++, and Java: use short-circuit evaluation for usual Boolean operators (&& and ), also provide bitwise Boolean operators that are not short circuit (& and ) 14 Short Circuit Evaluation Ada: Non-short-circuit: AND OR short-circuit: AND THEN OR ELSE Index = 1; while (Index <= Listlen) and then (List(Index) /= Key) loop Index = Index + 1; end loop; Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) (b++ / 3) (b is changed only when a <= b) 15 5

Assignment Statements The assignment operator symbol: = FORTRAN, BASIC, PL/I, C, C++, Java := ALGOLs, Pascal, Modula-2, Ada = can be bad if it is overloaded for the relational operator for equality e.g. (PL/I) A = B = C; 16 Assignment Statements More complicated assignments: Multiple targets (PL/I) A, B = 10 Conditional targets (C, C++, and Java) x = flag? count1 : count2 = 0; Compound assignment operators (C, C++, and Java) sum += next; Unary assignment operators (C, C++, and Java) a++; - count ++; 17 Assignment Statements C, C++, and Java treat = as an arithmetic binary operator e.g. a = b * (c = d * 2 + 1) + 1 This is inherited from ALGOL 68 Assignment as an Expression In C, C++, and Java, the assignment statement produces a result So, they can be used as operands in expressions e.g. while ((ch = getchar()!= EOF) {... } Disadvantage Another kind of expression side effect Exercise: a=1, b=2, c=3, d=4 a = b + (c = d / b++) 1 cout << a <<, << b <<, << c <<, << d << endl 18 6

Mixed Mode Assignment In FORTRAN, C, and C++ any numeric value can be assigned to any numeric scalar variable; whatever conversion that is necessary is done In Pascal integers can be assigned to reals, but reals cannot be assigned to integers programmer must specify whether the conversion from real to integer is truncated or rounded In Java, only widening assignment coercions are done In Ada, there is no assignment coercion 19 Control Structures A control structure is a control statement and the statements whose execution it controls Types of control statements: Selection statements Iterative statements Unconditional branching statement Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements 20 Design Issues What control statements should a language have? Can a control structure have multiple entries? Single entry: execution of the code segment begins with the first statement in the segment Multiple entries are possible in languages that include gotos and statement labels Multiple entries may add flexibility to a control construct Can a control structure have multiple exits? 21 7

Selection Statements Provides the means for choosing between two or more execution paths in a program Types of Selection Statements: One-way selection statements Two-way selection statements N-way (multiple) selection statements Nested selection statements? 22 Selection Statements Single-Way Examples FORTRAN IF: IF (boolean_expr) statement Problem: can select only a single statement; to select more, a GOTO must be used, as in the following example IF (.NOT. condition) GOTO 20...... 20 CONTINUE 23 Selection Statements Two-way selection statements if control_expression then clause else clause Control_expression arithmetic/boolean expressions Clause form Can be single statements or compound statements (statements in a program block) 24 8

Selection Statements Nested Selectors if (sum == 0) if (count == 0) result = 0; else result = 1; Which if gets the else? Java's static semantics rule: else goes with the nearest if To force alternative semantics, use compound statement if (sum == 0) { if (count == 0) result = 0; } else result = 1; 25 Selection Statements FORTRAN 90 and Ada solution use special words to resolve semantics of nested selectors e.g. (Ada) if.. then if then else end if end if if.. then if then end if else end if Advantage: flexibility and readability 26 Selection Statements Multiple Selection Constructs C: switch (expression) { case const_expr_1: statement_1; case const_expr_k: statement_k; [default: def_statement;] (optional) } 27 9

Selection Statements Early Multiple Selectors: FORTRAN arithmetic IF (a three-way selector) IF (arithmetic expression) N1, N2, N3 Multiple Selection using if: if Expr1 then statement_1 elsif Expr2 then statement_2 else statement_k end if; 28 10