And other languages….  puts “yes” // global, method of Kernel  Math.sqrt 2 // object Math, method sqrt  message.length // object message, method length,

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

COMPILER CONSTRUCTION WEEK-2: LANGUAGE DESCRIPTION- SYNTACTIC STRUCTURE:
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
ISBN Chapter 7 Expressions and Assignment Statements.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
ISBN Chapter 7 Expressions and Assignment Statements.
CS2403 Programming Languages Expressions and Assignment Statements Chung-Ta King Department of Computer Science National Tsing Hua University (Slides are.
Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Lecture 07 Expressions and Assignment Statements.
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
ISBN Chapter 7 Expressions and Assignment Statements.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Chapter 7 Expressions and Assignment Statements. Chapter 7 Topics 1-2 Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
C H A P T E R S E V E N Expressions and Assignment Statements.
CPS 506 Comparative Programming Languages Names, Scope, Expression.
1 Chapter 7 Expressions and Assignment statements.
C HAPTER 7 Expressions and Assignment Statements.
Arithmetic Expressions
Expressions and Assignment Statements
Chapter 7 Expressions and Assignment Statements. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
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.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Chapter Seven: Expressions and Assignment Statements Lesson 07.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
7-1 boolean Data Type George Boole ( ) boolean variables may have only two values, true or false You define boolean fields or boolean local variables.
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Types CSCE 314 Spring 2016.
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Operators And Expressions
Expressions and Assignment Statements
CS2403 Programming Languages Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Arithmetic Expressions
Expressions and Assignment Statements
Expression Review what is the result and type of these expressions?
Expressions and Assignment Statements
Expressions and Assignment Statements
Expression and Asignment Statements
Introduction to Programming – 4 Operators
Expressions.
Chapter 7 Expressions and Assignment Statements.
Chapter 7: Expressions and Assignment Statements Sangho Ha
Chap 7. Advanced Control Statements in Java
Expressions and Assignment Statements
Expressions and Assignment Statements
Presentation transcript:

and other languages…

 puts “yes” // global, method of Kernel  Math.sqrt 2 // object Math, method sqrt  message.length // object message, method length, no args  a.each {|x| puts x} // object a, method each, associated block  a[0] = a[1] // a.[](0), object a, method []=, arg 0 // a.[](1), object a,method [], arg 1  x + y // x.+(y) Compare to other languages overloaded operators

 lvalue = rvalue  lvalue must be address, rvalue must be value  x = 1  x += 1 # but not x++. Abbreviated assignment pseudooperators  x,y,z = 1,2,3 # parallel assignment  x=y=0 # chained assignment, right assoc  MAX = 5; MAX = 6 # constant not really constant… but Ruby gives warning

 Goal: return array if it exists, or a new one  results = results || []  results ||= []  Example of a Ruby idiom English idioms: pulling my leg, spill the beans Programming: means of expressing a recurring construct or simple task. Knowing the idioms associated with a programming language and how to use them is an important part of gaining fluency in that language (wikipedia) How does this work? Remember short-circuit evaluation? Caveat: lvalue must be nil or false

 x,y = y,x # swap  x = y; y = x # sequential, both now same value  x = 1,2,3 # x = [1,2,3]  x,y = [1,2] # same as x=1; y=2  a,b,c = 1,2 # a=1; b=2; c=nil  x, *y = 1,2,3 # x=1; y=[2,3] * is called splat, can only have one per assignment  *x, y = 1,2,3 # x=[1,2]; y=3

1-6 operators unary (1 operand) binary (2 operands) ternary (3 operands) infix - most prefix - Scheme Typical associativity rules Left to right, except assignment and **, which are right to left Sometimes unary operators associate right to left APL is different; all operators have equal precedence and all operators associate right to left, precedence and associativity rules can be overridden with parentheses would this be more or less confusing? typical precedence: parentheses unary operators ** (if the language supports it) *, /, % +, -

 local variables and method names both start with lower case letter  So how does Ruby know?  If prior assignment, it’s a variable. Otherwise, method invocation.

 Conditional Expressions C-based languages (e.g., C, C++) Also in Perl, JavaScript and Ruby An example: average = (count == 0)? 0 : sum / count Evaluates as if written like if (count == 0) average = 0 else average = sum /count

 Language Concepts overloaded operators associativity infix vs prefix arity precedence lvalue/rvalue parallel assignment constants short-circuit evaluation conditional expressions  Ruby Abbreviated assignment pseudooperators ||= idiom parallel assignment options splat