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

PPSX
Chapter 07
wantedwahab
 
DOCX
Compiler design important questions
akila viji
 
PDF
Compilers Design
Akshaya Arunan
 
PPTX
Structure of the compiler
Sudhaa Ravi
 
PDF
Lecture2 general structure of a compiler
Mahesh Kumar Chelimilla
 
PPT
Chap 1-language processor
shindept123
 
PPT
Lexical Analysis
Munni28
 
PPT
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Bhavin Darji
 
Chapter 07
wantedwahab
 
Compiler design important questions
akila viji
 
Compilers Design
Akshaya Arunan
 
Structure of the compiler
Sudhaa Ravi
 
Lecture2 general structure of a compiler
Mahesh Kumar Chelimilla
 
Chap 1-language processor
shindept123
 
Lexical Analysis
Munni28
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Bhavin Darji
 

What's hot (20)

PDF
Cs6660 compiler design
hari2010
 
PPTX
Fundamentals of Language Processing
Hemant Sharma
 
PPTX
C languaGE UNIT-1
Malikireddy Bramhananda Reddy
 
ODP
Basic C Programming language
Abhishek Soni
 
PPTX
Toy compiler
home
 
PPT
The smartpath information systems c pro
The Smartpath Information Systems,Bhilai,Durg,Chhattisgarh.
 
PPTX
C programming language
Maha lakshmi
 
PPTX
Compiler construction
Muhammed Afsal Villan
 
PPTX
C programming
Jigarthacker
 
PDF
Compiler Design Lecture Notes
FellowBuddy.com
 
PDF
Lecture 01 introduction to compiler
Iffat Anjum
 
PPTX
What is keyword in c programming
Rumman Ansari
 
DOC
Compiler Design(Nanthu)
guest91cc85
 
PPTX
Msc prev updated
mshoaib15
 
PPT
C programming for Computing Techniques
Appili Vamsi Krishna
 
PPTX
Msc prev completed
mshoaib15
 
PPTX
Introduction to c programming
Manoj Tyagi
 
KEY
Unit 1 cd
codereplugd
 
PPTX
Introduction to C programming
Rutvik Pensionwar
 
PPT
Principles of compiler design
Janani Parthiban
 
Cs6660 compiler design
hari2010
 
Fundamentals of Language Processing
Hemant Sharma
 
Basic C Programming language
Abhishek Soni
 
Toy compiler
home
 
The smartpath information systems c pro
The Smartpath Information Systems,Bhilai,Durg,Chhattisgarh.
 
C programming language
Maha lakshmi
 
Compiler construction
Muhammed Afsal Villan
 
C programming
Jigarthacker
 
Compiler Design Lecture Notes
FellowBuddy.com
 
Lecture 01 introduction to compiler
Iffat Anjum
 
What is keyword in c programming
Rumman Ansari
 
Compiler Design(Nanthu)
guest91cc85
 
Msc prev updated
mshoaib15
 
C programming for Computing Techniques
Appili Vamsi Krishna
 
Msc prev completed
mshoaib15
 
Introduction to c programming
Manoj Tyagi
 
Unit 1 cd
codereplugd
 
Introduction to C programming
Rutvik Pensionwar
 
Principles of compiler design
Janani Parthiban
 
Ad

Viewers also liked (9)

DOCX
Chapter 7 review questions
loayshabaneh
 
PPTX
Mirrors
Mahmoud Sheko
 
PPTX
Grecia julio 2011
alba lobera
 
PDF
New york city opera poster, philip morris
mspenner
 
DOCX
Actividad 2 jose_alava_vergara
Alava_Jose
 
PPT
Introduccion a la asignatura
topografiaunefm
 
PPTX
Bayi tabung
sicua050896
 
PDF
Revista abelha rainha cosméticos campanha 01/2017
Daniele Lopes
 
PDF
ACS-Brochure
Sales Avhan
 
Chapter 7 review questions
loayshabaneh
 
Mirrors
Mahmoud Sheko
 
Grecia julio 2011
alba lobera
 
New york city opera poster, philip morris
mspenner
 
Actividad 2 jose_alava_vergara
Alava_Jose
 
Introduccion a la asignatura
topografiaunefm
 
Bayi tabung
sicua050896
 
Revista abelha rainha cosméticos campanha 01/2017
Daniele Lopes
 
ACS-Brochure
Sales Avhan
 
Ad

Similar to 7 expressions and assignment statements (20)

PPT
7 expressions and assignment statements
Munawar Ahmed
 
PPTX
Introduction to C Programming: History, Applications, Variables, and Operator...
Mahmud Hasan Tanvir
 
PPTX
Computer programming and utilization
Digvijaysinh Gohil
 
PDF
C programming.pdf
JitendraYadav351971
 
PPSX
Esoft Metro Campus - Programming with C++
Rasan Samarasinghe
 
PPTX
C basics
sridevi5983
 
PPTX
C basics
sridevi5983
 
PDF
Introduction to C Language - Version 1.0 by Mark John Lado
Mark John Lado, MIT
 
PDF
Compiler gate question key
ArthyR3
 
PDF
CP c++ programing project Unit 1 intro.pdf
ShivamYadav886008
 
PDF
Introduction of C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
PPTX
SPOS UNIT1 PPTS (1).pptx
RavishankarBhaganaga
 
PPTX
Introduction to C Programming fjhjhjh.pptx
RoselinLourd
 
PDF
C programming course material
Ranjitha Murthy
 
PPTX
Functional Programming in JavaScript & ESNext
Unfold UI
 
PDF
C intro
SHIKHA GAUTAM
 
PPTX
6 assembly language computer organization
wewiv47743
 
PPTX
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
PPT
Expressions in c++
zeeshan turi
 
7 expressions and assignment statements
Munawar Ahmed
 
Introduction to C Programming: History, Applications, Variables, and Operator...
Mahmud Hasan Tanvir
 
Computer programming and utilization
Digvijaysinh Gohil
 
C programming.pdf
JitendraYadav351971
 
Esoft Metro Campus - Programming with C++
Rasan Samarasinghe
 
C basics
sridevi5983
 
C basics
sridevi5983
 
Introduction to C Language - Version 1.0 by Mark John Lado
Mark John Lado, MIT
 
Compiler gate question key
ArthyR3
 
CP c++ programing project Unit 1 intro.pdf
ShivamYadav886008
 
Introduction of C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
SPOS UNIT1 PPTS (1).pptx
RavishankarBhaganaga
 
Introduction to C Programming fjhjhjh.pptx
RoselinLourd
 
C programming course material
Ranjitha Murthy
 
Functional Programming in JavaScript & ESNext
Unfold UI
 
C intro
SHIKHA GAUTAM
 
6 assembly language computer organization
wewiv47743
 
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
Expressions in c++
zeeshan turi
 

More from jigeno (20)

PDF
Access2007 part1
jigeno
 
PDF
Basic introduction to ms access
jigeno
 
PPSX
Bsit1
jigeno
 
PDF
16 logical programming
jigeno
 
PDF
15 functional programming
jigeno
 
PDF
15 functional programming
jigeno
 
PDF
14 exception handling
jigeno
 
PDF
13 concurrency
jigeno
 
PDF
12 object oriented programming
jigeno
 
PDF
11 abstract data types
jigeno
 
PDF
9 subprograms
jigeno
 
PDF
8 statement-level control structure
jigeno
 
PDF
6 data types
jigeno
 
PDF
5 names
jigeno
 
PDF
4 lexical and syntax analysis
jigeno
 
PDF
3 describing syntax and semantics
jigeno
 
PDF
2 evolution of the major programming languages
jigeno
 
PDF
1 preliminaries
jigeno
 
PDF
Access2007 m2
jigeno
 
PDF
Access2007 m1
jigeno
 
Access2007 part1
jigeno
 
Basic introduction to ms access
jigeno
 
Bsit1
jigeno
 
16 logical programming
jigeno
 
15 functional programming
jigeno
 
15 functional programming
jigeno
 
14 exception handling
jigeno
 
13 concurrency
jigeno
 
12 object oriented programming
jigeno
 
11 abstract data types
jigeno
 
9 subprograms
jigeno
 
8 statement-level control structure
jigeno
 
6 data types
jigeno
 
5 names
jigeno
 
4 lexical and syntax analysis
jigeno
 
3 describing syntax and semantics
jigeno
 
2 evolution of the major programming languages
jigeno
 
1 preliminaries
jigeno
 
Access2007 m2
jigeno
 
Access2007 m1
jigeno
 

Recently uploaded (20)

PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Software Development Methodologies in 2025
KodekX
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
The Future of Artificial Intelligence (AI)
Mukul
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 

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