SlideShare a Scribd company logo
ICS 313 - Fundamentals of Programming Languages 1
7. Expressions and Assignment Statements
7.2 Arithmetic Expressions
Their evaluation was one of the motivations for the
development of the first programming languages
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?
ICS 313 - Fundamentals of Programming Languages 2
7.2 Arithmetic Expressions (continued)
A unary operator has one operand
A binary operator has two operands
A ternary operator has three operands
The operator precedence rules for expression evaluation define the
order in which “adjacent” operators of different precedence levels are
evaluated (“adjacent” means they are separated by at most one
operand)
Typical precedence levels
parentheses
unary operators
** (if the language supports it)
*, /
+, -
7.2 Arithmetic Expressions (continued)
The operator associativity rules for expression evaluation define the order in
which adjacent operators with the same precedence level are evaluated
Typical associativity rules:
Left to right, except **, which is right to left
Sometimes unary operators associate right to left (e.g., FORTRAN)
APL is different; all operators have equal precedence and all operators
associate right to left
Precedence and associativity rules can be overriden with parentheses
Operand evaluation order
The process:
Variables: just fetch the value
Constants: sometimes a fetch from memory; sometimes the constant is in the
machine language instruction
Parenthesized expressions: evaluate all operands and operators first
Function references: The case of most interest!
Order of evaluation is crucial
ICS 313 - Fundamentals of Programming Languages 3
7.2 Arithmetic Expressions (continued)
Functional side effects - when a function changes a
two-way parameter or a nonlocal variable
The problem with functional side effects:
When a function referenced in an expression alters
another operand of the expression e.g., for a parameter
change:
a = 10;
b = a + fun(&a);
/* Assume that fun changes its parameter */
Same problem with global variables
7.2 Arithmetic Expressions (continued)
Two Possible Solutions to the Problem:
Write the language definition to disallow functional side effects
No two-way parameters in functions
No nonlocal references in functions
Advantage: it works!
Disadvantage: Programmers want the flexibility of two-way parameters (what about
C?) and nonlocal references
Write the language definition to demand that operand
evaluation order be fixed
Disadvantage: limits some compiler optimizations
Conditional Expressions
C, C++, and Java (?:) e.g.
average = (count == 0)? 0 : sum / count;
ICS 313 - Fundamentals of Programming Languages 4
7.3 Overloaded Operators
Some is common (e.g., + for int and float)
Some is potential trouble (e.g., * in C and C++)
Loss of compiler error detection (omission of an operand
should be a detectable error)
Some loss of readability
Can be avoided by introduction of new symbols (e.g., Pascal’s
div)
C++ and Ada allow user-defined overloaded operators
Potential problems:
Users can define nonsense operations
Readability may suffer, even when the operators make sense
7.4 Type Conversions
A narrowing conversion is one that converts an object to a type that
cannot include all of the values of the original type e.g., float to int
A widening conversion is one in which an object is converted to a
type that can include at least approximations to all of the values of
the original type e.g., int to float
A mixed-mode expression is one that has operands of different types
A coercion is an implicit type conversion
The disadvantage of coercions:
They decrease in the type error detection ability of the compiler
In most languages, all numeric types are coerced in expressions,
using widening conversions
In Ada, there are virtually no coercions in expressions
ICS 313 - Fundamentals of Programming Languages 5
7.4 Type Conversions (continued)
Explicit Type Conversions
Often called casts e.g.
Ada:
FLOAT(INDEX) -- INDEX is INTEGER type
Java:
(int)speed /* speed is float type */
Errors in Expressions
Caused by:
Inherent limitations of arithmetic e.g. division by zero
Limitations of computer arithmetic e.g. overflow
Such errors are often ignored by the run-time system
7.5 Relational and Boolean Expressions
Relational Expressions:
Use relational operators and operands of various types
Evaluate to some Boolean representation
Operator symbols used vary somewhat among languages (!=, /=, .NE., <>,
#)
Boolean Expressions
Operands are Boolean and the result is Boolean
Operators:
FORTRAN 77 FORTRAN 90 C Ada
.AND. and && and
.OR. or || or
.NOT. not ! not
xor
C 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
ICS 313 - Fundamentals of Programming Languages 6
7.5 Relational and Boolean Expressions (continued)
Precedence of all Ada Operators:
**, abs, not
*, /, mod, rem
unary -, +
binary +, -, &
relops, in, not in
and, or, xor, and then, or else
C, C++, and Java have over 40 operators and least
15 different levels of precedence
7.6 Short Circuit Evaluation
Suppose Java did not use short-circuit evaluation
Problem: table look-up
index = 1;
while (index <= length) && (LIST[index] != value)
index++;
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)
FORTRAN 77: short circuit, but any side-affected place must be set
to undefined
Short-circuit evaluation exposes the potential problem of side effects
in expressions e.g. (a > b) || (b++ / 3)
ICS 313 - Fundamentals of Programming Languages 7
7.7 Assignment Statements
The operator symbol:
= FORTRAN, BASIC, PL/I, C, C++, Java
:= ALGOLs, Pascal, Ada
= Can be bad if it is overloaded for the relational operator
for equality
e.g. (PL/I) A = B = C;
Note difference from C
7.7 Assignment Statements (continued)
More complicated assignments:
Multiple targets (PL/I)
A, B = 10
Conditional targets (C, C++, and Java)
(first == true) ? total : subtotal = 0
Compound assignment operators (C, C++, and Java)
sum += next;
Unary assignment operators (C, C++, and Java)
a++;
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
ICS 313 - Fundamentals of Programming Languages 8
7.7 Assignment Statements (continued)
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
7.8 Mixed-Mode Assignment
In FORTRAN, C, and C++, any numeric value can
be assigned to any numeric scalar variable;
whatever conversion is necessary is done
In Pascal, integers can be assigned to reals, but
reals cannot be assigned to integers (the
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

More Related Content

What's hot

Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
hari2010
 
Fundamentals of Language Processing
Fundamentals of Language ProcessingFundamentals of Language Processing
Fundamentals of Language Processing
Hemant Sharma
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
Abhishek Soni
 
Toy compiler
Toy compilerToy compiler
Toy compiler
home
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
The Smartpath Information Systems,Bhilai,Durg,Chhattisgarh.
 
C programming language
C programming languageC programming language
C programming language
Maha lakshmi
 
Compiler construction
Compiler constructionCompiler construction
Compiler construction
Muhammed Afsal Villan
 
C programming
C programmingC programming
C programming
Jigarthacker
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
FellowBuddy.com
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
Iffat Anjum
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programming
Rumman Ansari
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)
guest91cc85
 
Msc prev updated
Msc prev updatedMsc prev updated
Msc prev updated
mshoaib15
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
Appili Vamsi Krishna
 
Msc prev completed
Msc prev completedMsc prev completed
Msc prev completed
mshoaib15
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Manoj Tyagi
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Rutvik Pensionwar
 
Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler designJanani Parthiban
 

What's hot (20)

Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
 
Fundamentals of Language Processing
Fundamentals of Language ProcessingFundamentals of Language Processing
Fundamentals of Language Processing
 
C languaGE UNIT-1
C languaGE UNIT-1C languaGE UNIT-1
C languaGE UNIT-1
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
 
Toy compiler
Toy compilerToy compiler
Toy compiler
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
 
C programming language
C programming languageC programming language
C programming language
 
Compiler construction
Compiler constructionCompiler construction
Compiler construction
 
C programming
C programmingC programming
C programming
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programming
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)
 
Msc prev updated
Msc prev updatedMsc prev updated
Msc prev updated
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 
Msc prev completed
Msc prev completedMsc prev completed
Msc prev completed
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler design
 

Viewers also liked

Chapter 7 review questions
Chapter 7 review questionsChapter 7 review questions
Chapter 7 review questionsloayshabaneh
 
Mirrors
MirrorsMirrors
Mirrors
Mahmoud Sheko
 
Grecia julio 2011
Grecia julio 2011Grecia julio 2011
Grecia julio 2011alba lobera
 
New york city opera poster, philip morris
New york city opera poster, philip morrisNew york city opera poster, philip morris
New york city opera poster, philip morrismspenner
 
Actividad 2 jose_alava_vergara
Actividad 2 jose_alava_vergaraActividad 2 jose_alava_vergara
Actividad 2 jose_alava_vergaraAlava_Jose
 
Real-time данные на фронтенде
Real-time данные на фронтендеReal-time данные на фронтенде
Real-time данные на фронтенде
EXANTE
 
Introduccion a la asignatura
Introduccion a la asignaturaIntroduccion a la asignatura
Introduccion a la asignatura
topografiaunefm
 
Bayi tabung
Bayi tabungBayi tabung
Bayi tabung
sicua050896
 
Revista abelha rainha cosméticos campanha 01/2017
Revista abelha rainha cosméticos campanha 01/2017Revista abelha rainha cosméticos campanha 01/2017
Revista abelha rainha cosméticos campanha 01/2017
Daniele Lopes
 

Viewers also liked (10)

Chapter 7 review questions
Chapter 7 review questionsChapter 7 review questions
Chapter 7 review questions
 
Mirrors
MirrorsMirrors
Mirrors
 
Grecia julio 2011
Grecia julio 2011Grecia julio 2011
Grecia julio 2011
 
New york city opera poster, philip morris
New york city opera poster, philip morrisNew york city opera poster, philip morris
New york city opera poster, philip morris
 
Actividad 2 jose_alava_vergara
Actividad 2 jose_alava_vergaraActividad 2 jose_alava_vergara
Actividad 2 jose_alava_vergara
 
Real-time данные на фронтенде
Real-time данные на фронтендеReal-time данные на фронтенде
Real-time данные на фронтенде
 
Introduccion a la asignatura
Introduccion a la asignaturaIntroduccion a la asignatura
Introduccion a la asignatura
 
Bayi tabung
Bayi tabungBayi tabung
Bayi tabung
 
Revista abelha rainha cosméticos campanha 01/2017
Revista abelha rainha cosméticos campanha 01/2017Revista abelha rainha cosméticos campanha 01/2017
Revista abelha rainha cosméticos campanha 01/2017
 
ACS-Brochure
ACS-BrochureACS-Brochure
ACS-Brochure
 

Similar to 7 expressions and assignment statements

7 expressions and assignment statements
7 expressions and assignment statements7 expressions and assignment statements
7 expressions and assignment statements
Munawar Ahmed
 
Computer programming and utilization
Computer programming and utilizationComputer programming and utilization
Computer programming and utilization
Digvijaysinh Gohil
 
C programming.pdf
C programming.pdfC programming.pdf
C programming.pdf
JitendraYadav351971
 
Ppl
PplPpl
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
Rasan Samarasinghe
 
C basics
C basicsC basics
C basics
sridevi5983
 
C basics
C basicsC basics
C basics
sridevi5983
 
Introduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoIntroduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John Lado
Mark John Lado, MIT
 
Compiler gate question key
Compiler gate question keyCompiler gate question key
Compiler gate question key
ArthyR3
 
CP c++ programing project Unit 1 intro.pdf
CP c++ programing project  Unit 1 intro.pdfCP c++ programing project  Unit 1 intro.pdf
CP c++ programing project Unit 1 intro.pdf
ShivamYadav886008
 
Introduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
SPOS UNIT1 PPTS (1).pptx
SPOS UNIT1 PPTS (1).pptxSPOS UNIT1 PPTS (1).pptx
SPOS UNIT1 PPTS (1).pptx
RavishankarBhaganaga
 
C programming course material
C programming course materialC programming course material
C programming course material
Ranjitha Murthy
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
Unfold UI
 
C intro
C introC intro
C intro
SHIKHA GAUTAM
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
zeeshan turi
 
Apple’s New Swift Programming Language Takes Flight With New Enhancements And...
Apple’s New Swift Programming Language Takes Flight With New Enhancements And...Apple’s New Swift Programming Language Takes Flight With New Enhancements And...
Apple’s New Swift Programming Language Takes Flight With New Enhancements And...
Azilen Technologies Pvt. Ltd.
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdf
Home
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
i i
 

Similar to 7 expressions and assignment statements (20)

7 expressions and assignment statements
7 expressions and assignment statements7 expressions and assignment statements
7 expressions and assignment statements
 
Computer programming and utilization
Computer programming and utilizationComputer programming and utilization
Computer programming and utilization
 
C programming.pdf
C programming.pdfC programming.pdf
C programming.pdf
 
Ppl
PplPpl
Ppl
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
C basics
C basicsC basics
C basics
 
C basics
C basicsC basics
C basics
 
Introduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoIntroduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John Lado
 
Compiler gate question key
Compiler gate question keyCompiler gate question key
Compiler gate question key
 
CP c++ programing project Unit 1 intro.pdf
CP c++ programing project  Unit 1 intro.pdfCP c++ programing project  Unit 1 intro.pdf
CP c++ programing project Unit 1 intro.pdf
 
Introduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan Thakur
 
SPOS UNIT1 PPTS (1).pptx
SPOS UNIT1 PPTS (1).pptxSPOS UNIT1 PPTS (1).pptx
SPOS UNIT1 PPTS (1).pptx
 
C programming course material
C programming course materialC programming course material
C programming course material
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
 
C intro
C introC intro
C intro
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
 
Apple’s New Swift Programming Language Takes Flight With New Enhancements And...
Apple’s New Swift Programming Language Takes Flight With New Enhancements And...Apple’s New Swift Programming Language Takes Flight With New Enhancements And...
Apple’s New Swift Programming Language Takes Flight With New Enhancements And...
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdf
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
 

More from jigeno

Access2007 part1
Access2007 part1Access2007 part1
Access2007 part1
jigeno
 
Basic introduction to ms access
Basic introduction to ms accessBasic introduction to ms access
Basic introduction to ms access
jigeno
 
16 logical programming
16 logical programming16 logical programming
16 logical programmingjigeno
 
15 functional programming
15 functional programming15 functional programming
15 functional programmingjigeno
 
15 functional programming
15 functional programming15 functional programming
15 functional programmingjigeno
 
14 exception handling
14 exception handling14 exception handling
14 exception handlingjigeno
 
13 concurrency
13 concurrency13 concurrency
13 concurrencyjigeno
 
12 object oriented programming
12 object oriented programming12 object oriented programming
12 object oriented programmingjigeno
 
11 abstract data types
11 abstract data types11 abstract data types
11 abstract data typesjigeno
 
9 subprograms
9 subprograms9 subprograms
9 subprogramsjigeno
 
8 statement-level control structure
8 statement-level control structure8 statement-level control structure
8 statement-level control structurejigeno
 
6 data types
6 data types6 data types
6 data typesjigeno
 
5 names
5 names5 names
5 namesjigeno
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysisjigeno
 
3 describing syntax and semantics
3 describing syntax and semantics3 describing syntax and semantics
3 describing syntax and semanticsjigeno
 
2 evolution of the major programming languages
2 evolution of the major programming languages2 evolution of the major programming languages
2 evolution of the major programming languagesjigeno
 
1 preliminaries
1 preliminaries1 preliminaries
1 preliminariesjigeno
 
Access2007 m2
Access2007 m2Access2007 m2
Access2007 m2jigeno
 
Access2007 m1
Access2007 m1Access2007 m1
Access2007 m1jigeno
 

More from jigeno (20)

Access2007 part1
Access2007 part1Access2007 part1
Access2007 part1
 
Basic introduction to ms access
Basic introduction to ms accessBasic introduction to ms access
Basic introduction to ms access
 
Bsit1
Bsit1Bsit1
Bsit1
 
16 logical programming
16 logical programming16 logical programming
16 logical programming
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
 
14 exception handling
14 exception handling14 exception handling
14 exception handling
 
13 concurrency
13 concurrency13 concurrency
13 concurrency
 
12 object oriented programming
12 object oriented programming12 object oriented programming
12 object oriented programming
 
11 abstract data types
11 abstract data types11 abstract data types
11 abstract data types
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
 
8 statement-level control structure
8 statement-level control structure8 statement-level control structure
8 statement-level control structure
 
6 data types
6 data types6 data types
6 data types
 
5 names
5 names5 names
5 names
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysis
 
3 describing syntax and semantics
3 describing syntax and semantics3 describing syntax and semantics
3 describing syntax and semantics
 
2 evolution of the major programming languages
2 evolution of the major programming languages2 evolution of the major programming languages
2 evolution of the major programming languages
 
1 preliminaries
1 preliminaries1 preliminaries
1 preliminaries
 
Access2007 m2
Access2007 m2Access2007 m2
Access2007 m2
 
Access2007 m1
Access2007 m1Access2007 m1
Access2007 m1
 

Recently uploaded

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 

Recently uploaded (20)

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 

7 expressions and assignment statements

  • 1. ICS 313 - Fundamentals of Programming Languages 1 7. Expressions and Assignment Statements 7.2 Arithmetic Expressions Their evaluation was one of the motivations for the development of the first programming languages 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?
  • 2. ICS 313 - Fundamentals of Programming Languages 2 7.2 Arithmetic Expressions (continued) A unary operator has one operand A binary operator has two operands A ternary operator has three operands The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated (“adjacent” means they are separated by at most one operand) Typical precedence levels parentheses unary operators ** (if the language supports it) *, / +, - 7.2 Arithmetic Expressions (continued) The operator associativity rules for expression evaluation define the order in which adjacent operators with the same precedence level are evaluated Typical associativity rules: Left to right, except **, which is right to left Sometimes unary operators associate right to left (e.g., FORTRAN) APL is different; all operators have equal precedence and all operators associate right to left Precedence and associativity rules can be overriden with parentheses Operand evaluation order The process: Variables: just fetch the value Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction Parenthesized expressions: evaluate all operands and operators first Function references: The case of most interest! Order of evaluation is crucial
  • 3. ICS 313 - Fundamentals of Programming Languages 3 7.2 Arithmetic Expressions (continued) Functional side effects - when a function changes a two-way parameter or a nonlocal variable The problem with functional side effects: When a function referenced in an expression alters another operand of the expression e.g., for a parameter change: a = 10; b = a + fun(&a); /* Assume that fun changes its parameter */ Same problem with global variables 7.2 Arithmetic Expressions (continued) Two Possible Solutions to the Problem: Write the language definition to disallow functional side effects No two-way parameters in functions No nonlocal references in functions Advantage: it works! Disadvantage: Programmers want the flexibility of two-way parameters (what about C?) and nonlocal references Write the language definition to demand that operand evaluation order be fixed Disadvantage: limits some compiler optimizations Conditional Expressions C, C++, and Java (?:) e.g. average = (count == 0)? 0 : sum / count;
  • 4. ICS 313 - Fundamentals of Programming Languages 4 7.3 Overloaded Operators Some is common (e.g., + for int and float) Some is potential trouble (e.g., * in C and C++) Loss of compiler error detection (omission of an operand should be a detectable error) Some loss of readability Can be avoided by introduction of new symbols (e.g., Pascal’s div) C++ and Ada allow user-defined overloaded operators Potential problems: Users can define nonsense operations Readability may suffer, even when the operators make sense 7.4 Type Conversions A narrowing conversion is one that converts an object to a type that cannot include all of the values of the original type e.g., float to int A widening conversion is one in which an object is converted to a type that can include at least approximations to all of the values of the original type e.g., int to float A mixed-mode expression is one that has operands of different types A coercion is an implicit type conversion The disadvantage of coercions: They decrease in the type error detection ability of the compiler In most languages, all numeric types are coerced in expressions, using widening conversions In Ada, there are virtually no coercions in expressions
  • 5. ICS 313 - Fundamentals of Programming Languages 5 7.4 Type Conversions (continued) Explicit Type Conversions Often called casts e.g. Ada: FLOAT(INDEX) -- INDEX is INTEGER type Java: (int)speed /* speed is float type */ Errors in Expressions Caused by: Inherent limitations of arithmetic e.g. division by zero Limitations of computer arithmetic e.g. overflow Such errors are often ignored by the run-time system 7.5 Relational and Boolean Expressions Relational Expressions: Use relational operators and operands of various types Evaluate to some Boolean representation Operator symbols used vary somewhat among languages (!=, /=, .NE., <>, #) Boolean Expressions Operands are Boolean and the result is Boolean Operators: FORTRAN 77 FORTRAN 90 C Ada .AND. and && and .OR. or || or .NOT. not ! not xor C 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
  • 6. ICS 313 - Fundamentals of Programming Languages 6 7.5 Relational and Boolean Expressions (continued) Precedence of all Ada Operators: **, abs, not *, /, mod, rem unary -, + binary +, -, & relops, in, not in and, or, xor, and then, or else C, C++, and Java have over 40 operators and least 15 different levels of precedence 7.6 Short Circuit Evaluation Suppose Java did not use short-circuit evaluation Problem: table look-up index = 1; while (index <= length) && (LIST[index] != value) index++; 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) FORTRAN 77: short circuit, but any side-affected place must be set to undefined Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) || (b++ / 3)
  • 7. ICS 313 - Fundamentals of Programming Languages 7 7.7 Assignment Statements The operator symbol: = FORTRAN, BASIC, PL/I, C, C++, Java := ALGOLs, Pascal, Ada = Can be bad if it is overloaded for the relational operator for equality e.g. (PL/I) A = B = C; Note difference from C 7.7 Assignment Statements (continued) More complicated assignments: Multiple targets (PL/I) A, B = 10 Conditional targets (C, C++, and Java) (first == true) ? total : subtotal = 0 Compound assignment operators (C, C++, and Java) sum += next; Unary assignment operators (C, C++, and Java) a++; 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
  • 8. ICS 313 - Fundamentals of Programming Languages 8 7.7 Assignment Statements (continued) 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 7.8 Mixed-Mode Assignment In FORTRAN, C, and C++, any numeric value can be assigned to any numeric scalar variable; whatever conversion is necessary is done In Pascal, integers can be assigned to reals, but reals cannot be assigned to integers (the 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