CH 7
CH 7
GLOBAL EDITION
Chapter 7
Expressions and
Assignment
Statements
• Introduction
• Arithmetic Expressions
• Overloaded Operators
• Type Conversions
• Relational and Boolean Expressions
• Short-Circuit Evaluation
• Assignment Statements
• Mixed-Mode Assignment
• Conditional Expressions
– C-based languages (e.g., C, C++)
– An example:
average = (count == 0)? 0 : sum / count
• Causes
– Inherent limitations of arithmetic
e.g., division by zero
– Limitations of computer arithmetic
e.g. overflow
• Often ignored by the run-time system
• Boolean Expressions
– Operands are Boolean and the result is Boolean
– Example operators
• 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)
Copyright © 2023 Pearson Education Ltd. All Rights Reserved. 1-23
Short Circuit Evaluation
Which is equivalent to
if ($flag){
$total = 0
} else {
$subtotal = 0
}
a = a + b
can be written as
a += b
• Expressions
• Operator precedence and associativity
• Operator overloading
• Mixed-type expressions
• Various forms of assignment