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

Similar documents
Chapter 7. Expressions and Assignment Statements

Expressions and Assignment Statements

Expressions & Assignment Statements

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 8. Statement-Level Control Structures

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

Software II: Principles of Programming Languages. Why Expressions?

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7. Expressions and Assignment Statements

Chapter 8. Statement-Level Control Structures ISBN

Chapter 8 Statement-Level Control Structures

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

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures

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

Chapter 7 Expressions and Assignment statements

Concepts of Programming Languages

Expressions and Assignment

Chapter7 Expression and Assignment Statement. Introduction

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

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Statement level control structures

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Array Initialization. Rectangular and Jagged Arrays. Arrays Operations. ICOM 4036 Programming Languages. Data Types

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

Ch. 7: Control Structures

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Statement-Level Control Structures

Statement-Level Control Structures

Operators. Java operators are classified into three categories:

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

G Programming Languages - Fall 2012

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

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

15.1 Origins and Uses of Ruby

Example Scheme Function: equal

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

The Arithmetic Operators

Chapter 8 Statement-Level Control Structure

Programming Languages, Summary CSC419; Odelia Schwartz

CT 229 Java Syntax Continued

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

LECTURE 18. Control Flow

Java Primer 1: Types, Classes and Operators

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

5. Control Statements

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Lecture 5 Tao Wang 1

Flow Control. CSC215 Lecture

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

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

(Not Quite) Minijava

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction. C provides two styles of flow control:

COMP Primitive and Class Types. Yi Hong May 14, 2015

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

A control expression must evaluate to a value that can be interpreted as true or false.

Programming for Engineers Iteration

Introduction to Programming Using Java (98-388)

Review of the C Programming Language

Lecture Set 4: More About Methods and More About Operators

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

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Chapter 4. Flow of Control

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

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

Chapter 7. Additional Control Structures

Lecture Set 4: More About Methods and More About Operators

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

Computer Programming I - Unit 5 Lecture page 1 of 14

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

CS302: Self Check Quiz 2

Expression Evaluation and Control Flow. Outline

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Flow Control. So Far: Writing simple statements that get executed one after another.

Conditionals and Loops

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

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Chapter 4: Control Structures I (Selection)

Expressions and Statements. Department of CSE, MIT, Manipal

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Logical Operators and if/else statement. If Statement. If/Else (4.3)

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

Control Structures. CIS 118 Intro to LINUX

CS111: PROGRAMMING LANGUAGE II

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Operators and Expressions

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Review of the C Programming Language for Principles of Operating Systems

C-LANGUAGE CURRICULAM

LECTURE 17. Expressions and Assignment

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

9/18/11. Type casting, logical operators and if/else statement. Explicit Type Conversion. Example of Type Casting

1 Lexical Considerations

Transcription:

ICOM 4036 Programming Languages Ch7. Expressions & Assignment Statements Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment Statements Mixed-Mode Assignment This lecture covers review questions 7-16 of Chapter 7 Dr. Amirhossein Chinaei, ECE, UPRM Spring 2010 ***Some slides are adapted from the Sebesta s textbook Relational Expressions Relational Expressions Use relational operators and operands of various types Evaluate to some Boolean representation Operator symbols used vary somewhat among languages (!=, /=, ~=,.NE., <>, #) JavaScript and PHP have two additional relational operator, === and!== Similar to their cousins, == and!=, except that they do not coerce their operands ICOM 4036. Ch. 7. 280 Boolean Expressions Boolean Expressions Operands are Boolean and the result is Boolean Example operators FORTRAN 77 FORTRAN 90 C Ada.AND. and && and.or. or or.not. not! not xor ICOM 4036. Ch. 7. 281 Boolean Expressions C89 has no Boolean type it uses int type with 0 for false and nonzero for true One odd characteristic of C s expressions: a < b < c is a legal expression, but the result is not what you might expect: Left operator is evaluated, producing 0 or 1 The evaluation result is then compared with the third operand (i.e., c) ICOM 4036. Ch. 7. 282 1

Short Circuit Evaluation An expression in which the result is determined without evaluating all of the operands and/or operators Example: (13*a) * (b/13 1) If a is zero, there is no need to evaluate (b/13-1) Problem with non-short-circuit evaluation index = 1; while (index <= length) && (LIST[index]!= value) index++; When index=length, LIST [index] will cause an indexing problem (assuming LIST has length -1 elements) Short Circuit Evaluation (cont d) 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) ICOM 4036. Ch. 7. 283 ICOM 4036. Ch. 7. 284 Assignment Statements The general syntax <target_var> <assign_operator> <expression> The assignment operator = FORTRAN, BASIC, the C-based languages := ALGOLs, Pascal, Ada = can be bad when it is overloaded for the relational operator for equality (that s why the C-based languages use == as the relational operator) Assignment Statements Conditional targets (Perl) ($flag? $total : $subtotal) = 0 Which is equivalent to if ($flag){ $total = 0 } else { } $subtotal = 0 ICOM 4036. Ch. 7. 285 ICOM 4036. Ch. 7. 286 2

Assignment Statements: Compound Operators A shorthand method of specifying a commonly needed form of assignment Introduced in ALGOL Example a = a + b is written as a += b Assignment Statements Unary assignment operators in C-based count++ (count incremented) languages combine increment and decrement operations with assignment Examples sum = ++count sum = count++ -count++ (count incremented then negated) ICOM 4036. Ch. 7. 287 ICOM 4036. Ch. 7. 288 Assignment as an Expression In C, C++, and Java, the assignment statement produces a result and can be used as operands An example: while ((ch = getchar())!= EOF){ } ch = getchar() is carried out; the result (assigned to ch) is used as a conditional value for the while statement List Assignments E.g. in Perl and Ruby ($first, $second, $third) = (20, 30, 40); ICOM 4036. Ch. 7. 289 Mixed-Mode Assignment Assignment statements can also be mixed-mode In Fortran, C, and C++, any numeric type value can be assigned to any numeric type variable In Java, only widening assignment coercions are done In Ada, there is no assignment coercion ICOM 4036. Ch. 7. 290 3

Summary Expressions Operator precedence and associativity Operator overloading Mixed-type expressions Various forms of assignment ICOM 4036 Programming Languages Ch8. Statement-Level Control Structure Selection Statement Iterative Statements Unconditional Branching Guarded Commands Conclusions This lecture covers review questions 1-7 of Chapter 8 ICOM 4036. Ch. 7. 291 Dr. Amirhossein Chinaei, ECE, UPRM Spring 2010 ***Some slides are adapted from the Sebesta s textbook Levels of Control Flow Within expressions (Chapter 7) Among program units (Chapter 9) Among program statements (this chapter) Control Statements: Evolution FORTRAN I control statements were based directly on IBM 704 hardware Much research and argument in the 1960s about the issue One important result: It was proven that all algorithms represented by flowcharts can be coded with only two-way selection and pretest logical loops ICOM 4036. Ch. 8. 293 ICOM 4036. Ch. 8. 294 4

Control Structure A control structure is a control statement and the statements whose execution it controls Design question Should a control structure have multiple entries? A selection statement is a control statement that provides the means of choosing between two or more paths of execution Two general categories: Two-way selectors Multiple-way selectors Two-Way Selection Statements General form: if control_expression then clause else clause Design Issues: What is the form and type of the control expression? How are the then and else clauses specified? How should the meaning of nested selectors be specified? ICOM 4036. Ch. 8. 295 ICOM 4036. Ch. 8. 296 The Control Expression If the then reserved word or some other syntactic marker is not used to introduce the then clause, the control expression is placed in parentheses In C89, C99, Python, and C++, the control expression can be arithmetic In languages such as Ada, Java, Ruby, and C#, the control expression must be Boolean Clause Form In many contemporary languages, the then and else clauses can be single statements or compound statements In Perl, all clauses must be delimited by braces (they must be compound) In Fortran 95, Ada, and Ruby, clauses are statement sequences Python uses indentation to define clauses if x > y : x = y print "case 1" ICOM 4036. Ch. 8. 297 ICOM 4036. Ch. 8. 298 5

Nesting Selectors Java example if (sum == 0) if (count == 0) result = 0; else result = 1; Which if gets the else? Java's static semantics rule: else matches with the nearest unmatched if ICOM 4036. Ch. 8. 299 Nesting Selectors (cont d) To force an alternative semantics, compound statements may be used: if (sum == 0) { if (count == 0) result = 0; } else result = 1; The above solution is used in C, C++, and C# Perl requires that all then and else clauses to be compound ICOM 4036. Ch. 8. 300 Nesting Selectors (cont d) Multiple-Way Selection Statements Statement sequences as clauses: Ruby if sum == 0 then if count == 0 then result = 0 else result = 1 Indentation: Python if sum == 0 : if count == 0: result = 0 else : result = 1 Allow the selection of one of any number of statements or statement groups Design Issues: 1. What is the form and type of the control expression? 2. How are the selectable segments specified? 3. Is execution flow through the structure restricted to include just a single selectable segment? 4. How are case values specified? 5. What is done about unrepresented expression values? ICOM 4036. Ch. 8. 301 ICOM 4036. Ch. 8. 302 6

Multiple-Way Selection: Examples C, C++, and Java switch (expression) { case const_expr_1: stmt_1; } case const_expr_n: stmt_n; [default: stmt_n+1] Multiple-Way Selection: Examples Design choices for C s switch statement 1. Control expression can be only an integer type 2. Selectable segments can be statement sequences, compound statements, or blocks 3. Any number of segments can be executed in one execution of the construct (there is no implicit branch at the of selectable segments) 4. default clause is for unrepresented values (if there is no default, the whole statement does nothing for unrepresented values) ICOM 4036. Ch. 8. 303 ICOM 4036. Ch. 8. 304 Multiple-Way Selection: Examples C# Differs from C in that it has a static semantics rule that disallows the implicit execution of more than one segment Each selectable segment must with an unconditional branch (goto or break) Also, in C# the control expression and the case constants can be strings Multiple-Way Selection: Examples Ada case expression is when choice list => stmt_sequence; when choice list => stmt_sequence; when others => stmt_sequence;] case; More reliable than C s switch (once a stmt_sequence execution is completed, control is passed to the first statement after the case statement ICOM 4036. Ch. 8. 305 ICOM 4036. Ch. 8. 306 7

Multiple-Way Selection: Examples Ada design choices: 1. Expression can be any ordinal type 2. Segments can be single or compound 3. Only one segment can be executed per execution of the construct Constant List Forms: 1. A list of constants 2. Can include: - Subranges - Boolean OR operators ( ) ICOM 4036. Ch. 8. 307 Multiple-Way Selection: Examples Ruby has two forms of case statements 1. One form is a compound assignment leap = case when year % 400 == 0 then true when year % 100 == 0 then false else year % 4 == 0 2. The other uses a case value and when values case in_val when -1 then neg_count++ when 0 then zero_count++ when 1 then pos_count++ else puts "Error in_val is out of range" ICOM 4036. Ch. 8. 308 Multiple-Way Selection Using if Multiple Selectors can appear as direct extensions to two-way selectors, using else-if clauses, for example in Python: if count < 10 : bag1 = True elif count < 100 : bag2 = True elif count < 1000 : bag3 = True Multiple-Way Selection Using if The Python example can be written as a Ruby case case when count < 10 then bag1 = true when count < 100 then bag2 = true when count < 1000 then bag3 = true ICOM 4036. Ch. 8. 309 ICOM 4036. Ch. 8. 310 8