The assignement statement
Download
Report
Transcript The assignement statement
The Assignment
Operator and
Statement
The Most Common Statement you
will use
Copyright © 2004-2007 Curt Hill
Changing variables
• What can change in a variable?
– Only the value
– The type and name are set only at
compile time
• What statements can change the
value?
–
–
–
–
Declaration might or might not initialize
An assignment
Some types of method calls
Input statements are a type of method
call
Copyright © 2004-2007 Curt Hill
Assignment Statement
• Simple form:
variable = expression ;
• Simple meaning:
the value computed from the
expression is stored in the variable
• Similar to the assignment in very
many languages
Copyright © 2004-2007 Curt Hill
What is an expression?
•
•
•
•
•
A legal combination of the following:
Constants
Variables
Operators
The legal combinations are usually
intuitive
Copyright © 2004-2007 Curt Hill
Legal Examples
• Suppose the following declarations:
int a,b,c;
double x,y,z;
• The following are legal:
a = 5;
// constant
x = y;
// variable
y = 2*z – x/y; // expression
Copyright © 2004-2007 Curt Hill
General rules
• Item on left must be a variable
• The two sides should be of the same
type with a few exceptions
– A weaker type may be converted to a
stronger type
– A float is weaker than a double
– A double is stronger than an int
• The right hand side items are not
changed
• The old value of the left hand side is
lost when a new value is stored
Copyright © 2004-2007 Curt Hill
More Examples
• Suppose the following declarations:
int a,b,c; double x,y,z;
• The following are not legal:
5 = -c;
// cannot change
a = 5.8;
// loses precision
x = false;
// incompatible
x = y
// no semicolon
x = y + * z;
// malformed expr
• The following are legal:
x = a;
// Stronger type
Copyright © 2004-2007 Curt Hill
Expression Values
• What is the value of:
2 + 3*2 + 1
• Two reasonable candidates:
• 11 = ((2+3) * 2) + 1
– Left to right evaluation
– Some calculators do it this way
• 9 = 2 + (3*2) + 1
– The multiplication precedes addition
– Algebra uses this way
• We must choose one of these
Copyright © 2004-2007 Curt Hill
Precedence
• Clearly we cannot have both 11 and
9 correct or we never know what a
computation produces
• Java like most (but not all)
programming languages follows
Algebra
• Multiplication precedes addition
• This gives rise to a precedence
chart
– Shows the level of importance for
operators
Copyright © 2004-2007 Curt Hill
Precedence Chart
• Highest to lowest
–
–
–
–
()
- + (Unary sign change)
*/%
+ - (Binary arithmetic)
• There are several others which we
will encounter soon enough
Copyright © 2004-2007 Curt Hill
Algebra and the Assignment
• There is often confusion about the
assignment because it looks like an
equation
• An equation is a true or false
statement
• An assignment is a command to
perform some action
Copyright © 2004-2007 Curt Hill
Equations and Assignments
• The equation:
x=y+5
– States that the value of x is five larger than y’s
value
– This may be true or false depending on the
value of x and y
– Usually our job is to find an x and y that makes
it true – this is called a solution
• The assignment:
x = y + 5;
– Commands that the value of x is computed to
be five plus y’s value
Copyright © 2004-2007 Curt Hill
Another Example
• The equation:
x=x+1
has no solution
– It can never be true
– No value may equal itself plus one
• The assignment:
x = x + 1;
increments x
– It is a command and will be carried out
Copyright © 2004-2007 Curt Hill
Another Example
• Suppose:
int a=2, b=5, c=-7;
• What happens to the variable values
when:
c = a*b - b%c+1;
b = a-c/(1+a*2);
is executed?
• The value of c becomes 6 and the value of
b becomes 1
Copyright © 2004-2007 Curt Hill
How does this work?
• int
c =
•c =
•c =
•c =
•b =
•b =
•b =
•b =
•b =
•b =
a=2, b=5, c=-7;
a*b - b%c+1;
2*5 – 5%-7+1
10 –
5 +1
6
a – c/(1+a*2);
2 – 6/(1+2*2)
2 – 6/(1+4)
2 – 6/5
2 – 1
1
Copyright © 2004-2007 Curt Hill
The assignment operator
• Some languages have an
assignment statement
– Pascal
– BASIC, Visual BASIC
• The C family (C, C++, Java) treats =
as a side-effect operator
• What is the difference?
Copyright © 2004-2007 Curt Hill
= as operator
• The following is legal:
a = b = 5;
• The 5 is first assigned to b
• Next b is assigned to a
• This is known as a multiple
assignment and some other
languages support this
• There is more
Copyright © 2004-2007 Curt Hill
= really is an operator
• The following statement is also
legal:
a = (b = 2*b) - (c=2);
• It is equivalent to the following
sequence:
c=2;
b = 2*b;
a = b – c;
Copyright © 2004-2007 Curt Hill
Side-Effect Operator
• Any operator that changes one of its
operands is called a side-effect operator
• Most operators produce a value but do
not change their operands
• For now the assignment operator is the
only one, but there are more
• Method calls may also have side-effects
• An input statement must have a sideeffect while an output will usually not
Copyright © 2004-2007 Curt Hill
Another Property of Operators
• What is the value:
8–4–3
2
• Is it?
(8 – 4) – 3 = 1
8 – (4 – 3) = 7
• How about exponentiation:
2
3
2
• Is it (2^3)^2 = 64 or 2^(3^2)=512?
32
Copyright © 2004-2007 Curt Hill
Associativity
• The concept of associativity is the
property that determines the order
of execution when the same
operator is used twice in a row
• Subtraction is left to right
• Exponentiation is right to left
• Most operators in Java are left to
right
• The exception is the assignment
operator
Copyright © 2004-2007 Curt Hill
Assignment Operator
Properties
• The associativity of = is right to left
• Thus
a = b = c;
is the same as
a = (b = c);
• The assignment operator also has low
precedence
• If it did not, then it would not allow all
computations on the right to complete
• This is why this needs parentheses:
a = (b = 2*b) - (c=2);
Copyright © 2004-2007 Curt Hill
Precedence Chart Again
• Highest to lowest
–
–
–
–
–
()
- + (Unary – sign change)
*/%
+ - (Binary arithmetic)
=
• Yes, there are quite a few more!
Copyright © 2004-2007 Curt Hill