Professional Documents
Culture Documents
Unit 2 Part 2
Unit 2 Part 2
ASSIGNMENT STATEMENTS
1-1
Introduction
1-2
Arithmetic Expressions
1-3
Design Issues for Arithmetic Expressions
1-4
Arithmetic Expressions: Operators
1-5
Operator Precedence Rules
1-7
Conditional Expressions
• Conditional Expressions
– C-based languages (e.g., C, C++)
– An example:
average = (count == 0)? 0 : sum / count
1-8
Operand Evaluation Order
1-9
Overloaded Operators
1-10
User-Defined Overloaded Operators
1-11
Type Conversions
1-12
Explicit Type Conversions
1-13
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
• Causes
– Inherent limitations of arithmetic
e.g., division by zero
– Limitations of computer
arithmetic
e.g. overflow
• Often ignored by the run-
time system
1-15
Relational Expressions
• Relational Expressions
– Use relational operators and operands of various
types (<, > , = = , !=, etc.)
– 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
1-16
Boolean Expressions
• Boolean Expressions
– Operands are Boolean and the result is Boolean
– Example operators
1-17
Short Circuit Evaluation
1-18
Short Circuit Evaluation
1-19
Assignment Statements
1-20
Conditional Targets
Which is equivalent to
if ($flag){
$total = 0
} else {
$subtotal = 0
}
1-21
Compound Operators
a = a + b
is written
as a += b
1-22
Unary Assignment Operators
while statement
List Assignments
1-25